I really dont understand the output of my array. In the start it seams to be very simple but I am already some hours in this problem. Look the code:
public function getAllProducts($limit){
$result = mysql_query("SELECT * FROM products ORDER BY RAND() LIMIT $limit") or die(mysql_error());
$productArray = array();
$count = 0;
while($row = mysql_fetch_row($result)){
$this->id = $row[0];
$this->category_id = $row[1];
$this->title = $row[2];
$this->short_description = $row[3];
$this->long_description = $row[4];
$this->tag = $row[5];
$this->price = $row[6];
$this->weight = $row[7];
$this->stock = $row[8];
$productArray[$count] = $this;
$count++;
}
echo'<pre>';
print_r($productArray);
echo'</pre>';
return $productArray;
}
The output:
Array
(
[0] => Product Object
(
[id] => 2
[category_id] => 2
[title] => Cart�o de Pascoa
[short_description] => Short description bout this product
[long_description] => Long description about thi product but we don
[tag] => Pascoa Coelho Cart�o Cartao Card
[price] => 60,00
[weight] => 1
[stock] => 1
)
[1] => Product Object
(
[id] => 2
[category_id] => 2
[title] => Cart�o de Pascoa
[short_description] => Short description bout this product
[long_description] => Long description about thi product but we don
[tag] => Pascoa Coelho Cart�o Cartao Card
[price] => 60,00
[weight] => 1
[stock] => 1
)
)
Now, I will do only an ajustment in the print_r function:
echo'<pre>';
print_r($productArray[0]);
echo'</pre>';
The New Output:
Product Object
(
[id] => 1
[category_id] => 1
[title] => Cart�o de Natal
[short_description] => Short descroption about this product
[long_description] => Long description of this product. Nor used ri
[tag] => Cart�o de Natal Natal Presente de Natal
[price] => 55,00
[weight] => 1
[stock] => 1
)
One more adjust:
echo'<pre>';
print_r($productArray[1]);
echo'</pre>';
The OutPut:
Product Object
(
[id] => 2
[category_id] => 2
[title] => Cart�o de Pascoa
[short_description] => Short description bout this product
[long_description] => Long description about thi product but we don
[tag] => Pascoa Coelho Cart�o Cartao Card
[price] => 60,00
[weight] => 1
[stock] => 1
)
The DataBase:
1 1 Cartão de Natal Short descroption about this product Long description of this product. Nor used ri Cartão de Natal Natal Presente de Natal 55,00 1 1
2 2 Cartão de Pascoa Short description bout this product Long description about thi product but we don Pascoa Coelho Cartão Cartao Card 60,00 1 1
Have you noticed that when I do a print_r or a var_dump in the $productArray we have an wrong output and when we do $productArray[0] or $productArray[1] we get the correct one. Have anyone noticed something wrong in my code?
Thanks a lot in advanced!
In PHP5, objects are not copied when they are inserted into arrays or passed to functions. So you are only inserting one object multiple times into the array, and then overwriting its properties for each loop iteration.
You should either:
1) Create a new instance of the class:
2) Or clone the object when you insert it into the array: