0

Basically I'm trying to build an object that repeats the values from a query into the keys.

 $obj = new stdClass(); $obj->Product = (object) array("Name" => "", "Month"=> "", "Price" =>""); $obj->Totals = 0.00; $r=$shop->get_all_products(0); while(mysql_num_rows($r) && $row=mysql_fetch_array($r)) { for ($i=$startMonth; $i<=$endMonth; $i++) { $thisDate=date("F", mktime(0, 0, 0, $i, 10)); $r1=$reports->get_sales_by_month($i,$row['product_id']); while(mysql_num_rows($r1) && $row1=mysql_fetch_array($r1)) { $totSales=$totSales + $row1['total_price']; $obj->Product->Name=$row['product_name']; $obj->Product->Month=$thisDate; $obj->Product->Price=$row1['total_price']; $obj->Totals=$totSales; } } } 

When I do var_dump($obj), I get only the last record in the query. I want the object to contain all records.

3
  • 4
    You're over-writing $obj's values every time the loop runs - you'll need to either use different names (Name1, etc), or move the object instantiation code inside the while loop, and generate yourself an array of objects that way. Commented Jun 5, 2013 at 14:51
  • Also using mysql_* set of functions is deprecated - you should be looking at using mysqli or pdo objects. Commented Jun 5, 2013 at 14:52
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jun 5, 2013 at 15:00

1 Answer 1

2

The Product Property needs to be an array if you want it to contain multiple values ...

Try this instead:

$obj = new stdClass(); $obj->Product = array(); $obj->Totals = 0.00; $r=$shop->get_all_products(0); while(mysql_num_rows($r) && $row=mysql_fetch_array($r)) { for ($i=$startMonth; $i<=$endMonth; $i++) { $thisDate=date("F", mktime(0, 0, 0, $i, 10)); $r1=$reports->get_sales_by_month($i,$row['product_id']); while(mysql_num_rows($r1) && $row1=mysql_fetch_array($r1)) { $totSales=$totSales + $row1['total_price']; $obj->Product[] = (object)array("Name"=>$row['product_name'], "Month"=>$thisDate, "Price"=>$row1['total_price']); $obj->Totals=$totSales; } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

nope... totals is an accumlation of the Price property on each product
Though this is correct, a common practice is to create an array of objects: $products[i]->name etc. In this way 1 record is always 1 object and 1 object always describes 1 thing/record/row.
@ChrisVisser this is an array of objects... i'm building as an array and casting to a stdClass object... the notation you use there will work in this use case
I know. I was just pointing it out. I upvoted and decided not to post my own version and instead I commented with another example
Thanks everyone. One last thing, to get to a product in January, I do something like $obj->Product["Month"]?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.