So I have 2 tables:
1. Products - id, title, description, price, category
2. Reviews with id, user_id, rating, product_id
I'm trying to develop a shop. So, in each product page I have a form where users can submit a review, giving 1-5 stars. On the index page, where all products are displayed with title, description, category, price, I want to display the average rating of each product. I already setup the relationships as follows:
Products -> hasMany(review)
Review -> belongsTo(user)
Review -> belongsTo(product)
User -> hasMany(review)
How do I interogate the database and display the average rating for each product on index? I found some solutions around here but I couldn't make it work.
P.S: Everyting that is inserted in the Review table is correct, I just need to display it for each product. Thank you in advance!
Without Eloquent, it will be something like this, can you make sure it outputs something?
$reviews = Product::find($id)->reviews;
$avg = 0;
foreach($reviews as $rev){
$avg += $rev->rating;
}
return round($avg/count($reviews));
With Eloquent, it will be like:
$product = Product::find($id);
$avg = $product->reviews()->avg('rating');
return $avg;
Related
Using php and mysqli, I have a categories table that group products and control the order they are displayed in my catalog. The field "ord" is an int with the value that controls the order these product categories are displayed. When inserting a new category, I would like to reorder the remaining categories from the new category down and update the table then insert the new category without duplicating the "ord" value.
There are 11 categories. I want to insert my new category with it's "ord" value as 5. Clearly I need to add 1 to each "ord" values 6 - 11 to reorder my catalog. Not sure how to do this. I'm not good with arrays but I would think some sort of foreach array[$key][] + 1 with an UPDATE to the table would work. I'm not clear on how to create the array.
Don't really have any working ideas on this...
Category ord=1: Site Licensing
Category ord=2: PICC Insertion Training
Category ord=3: Ultrasound Training
New Category: ord 2: Membership Subscriptions
Category ord=1: Site Licensing
Category ord=2: Membership Subscriptions
Category ord=3: PICC Insertion Training
Category ord=4: Ultrasound Training
You can simply update the table before doing the INSERT. By updating the existing data first, you leave a space for your new entry.
$order = $_POST['order'];
$stmt = $mysqli->prepare("UPDATE `categories` set order = order + 1 where order >= ?"));
$stmt->bind_param("i", $order);
$stmt->execute();
/** Now perform INSERT statement */
following code is about getting the products from my db table using codeigniter sql queries.i am getting products of a row by limiting up to 4 products and by applying where condition of products less than cost of 900 but i am not getting how to get the products less than 900 but with different prices in each item. this means if once product 0f 500 is fetched it should not fetch 500 again it should go for another product by the help of product_id DESC. Explain me the logic of query how i should write
public function byPrice()
{
$query = $this->db->limit(4)
->where('pro_cost<', 900)
->get('products');
return $query;
}
$Q="SELECT DISTINCT `pro_cost` FROM `products` WHERE `pro_cost` < 900";
$result=$this->db->query($Q)->result();
$this->db->where('cost < 900', NULL)->group_by('id')->group_by('cost')->get('product')
i would like to know how to get the unique quantity of products in a order
i have tried these (in /renderer/default.phtml):
$_order = $this->getOrder();
$qnt = round($_order->getData('total_qty_ordered'), 0);
but this returns the total number of products, and i need only total of different products.
thanks!
The problem is that an order may contain many products. Hence qty of a product is not assigned at order level but is is assigned at item level. You can get that as below:
$_order = $this->getOrder();
foreach ($_order->getAllItems() as $items){
$qty = $items->getQty();
}
You can get the number of different products by loading the visible items of an order (since Magento stores two order items for configurable products, one for the parent and one for the child, this method provides only one item in such cases) and count the number of array entries.
For example in this way:
$_order = $this->getOrder();
$qnt = count($_order->getAllVisibleItems());
I use the following code to get information from my MySql data base in order to display items :
$sql = "SELECT product,size1,size2,prdname,price
FROM multisize
WHERE online= 'online' AND category = 'backcovers' AND campaign = '".$brand."'
ORDER BY size1 DESC, size2 DESC ";
Some times I have the same item that needs to be displayed in two campaigns. (ex. iphone4, iphone5)
is there any way to tell my code that this item should be displayed in those two campaigns or should i just create duplicate items ?
You may use GROUP BY clause in your query.
I was wondering if there was a way to take one value from a column in a Table and add it to a column in another table.
The Scenario: I have a table (Shopping_Cart). It stores all of my customers shopping cart items in the columns CustomerID, PID, Price, Status (Status can be "IN_CART" which means it was added and "OPEN" means that it has been paid for and ready to be processed and shipped). Then I have a table (Products). It stores product information in columns PID, Price,Weight`. When My customers place an item in the shopping cart I do not capture the price. Prices can change daily so if they add something today, when they come back tomorrow the price may increase or decrease in their shopping cart. When my customer checks out and payment is confirmed, I am trying to grab the Price in the (Products) table where Products.PID = Shopping_Cart.PID and then apply that price to the Shopping_Cart.Price to lock in that final price since the customer has paid for it. Any Ideas? I got it set up to change the items for that customers cart to "OPEN" but I am a little lost on getting my script to grab a value from one table and applying it to another table. Below is my PHP Script for changing the Lines from "IN_CART" to "OPEN".
if($The_Function=="CLOSE_LINES"){
$cart_close_session_id = $_GET['close_session_id'];
$response = array();
require_once __DIR__ . '/db_connect.php';
//connect to my db
$db = new DB_CONNECT();
$result = mysql_query("UPDATE shopping_cart SET Status = 'OPEN' WHERE SessionID LIKE '$cart_close_session_id' AND Status LIKE 'IN_CART'");
if ($result) {
$response["close_success"] = 1;
//GRAB PRICE FROM "PRODUCTS TABLE" AND UPDATE SHOPPING_CART
}else{
$response["close_success"] = 0;
}
echo json_encode($response);
}
Assuming that you have a *shopping_cart* table with a price and a product_id column, you can update it with the price of the products table, like this:
UPDATE shopping_cart SET price =
(SELECT price FROM products WHERE id = shopping_cart.product_id)
You can join against the products table and update the shopping_cart upon purchase as seen below:
UPDATE shopping_cart c
JOIN products p
ON p.pid = c.pid
SET c.price = p.price
WHERE c.customer_id = ?