pedropedroc Posted April 30, 2007 Share Posted April 30, 2007 Hello, I have a function which returned the attributes for the Value Object 'products' Original function follows: function createProducts() { $this->products = array(); $product1 = new ProductVO(); $product1->setId( 1 ); $product1->setName( "A" ); $product1->setDescription( "AB" ); $product1->setPrice( 1 ); $product1->setImage( "assets/products/a.jpg" ); $product1->setThumbnail( "assets/products/a_sm.jpg" ); $this->products[] = $product1; $product2 = new ProductVO(); $product2->setId( 2 ); $product2->setName( "B" ); $product2->setDescription( "BC" ); $product2->setPrice( 2 ); $product2->setImage( "assets/products/b.jpg" ); $product2->setThumbnail( "assets/products/b.jpg" ); $this->products[] = $product2; //Note: Repeated for all 16 products return $this->products; } I am trying to alter the function, so that the data is pulled out from a MySQL dbase. I got so far, and could not think of the required PHP code - this is the first time I have worked with PHP. Current code as follows: function createProducts() { $result = mysql_query("SELECT * FROM Subscription"); while ($row = mysql_fetch_array($result)) { $this->products = array(); $product = new ProductVO(); $product->setId($row['Id']); $product->setName($row['Name']); $product->setDescription($row['Description']); $product->setPrice($row['Price']); $product->setImage( "../assets/products/" . ($row['Name']) . ".jpg" ); $product->setThumbnail( "../assets/products/" . ($row['Name']) . "_sm.jpg" ); $this->products[] = $product; } return $this->products; } Unsurprisingly this just returns one 'product': how can I get it to return every product? Thanks for any help in advance! Quote Link to comment https://forums.phpfreaks.com/topic/49300-solved-dao-array/ Share on other sites More sharing options...
monk.e.boy Posted April 30, 2007 Share Posted April 30, 2007 function createProducts() { $result = mysql_query("SELECT * FROM Subscription"); products = array(); while ($row = mysql_fetch_array($result)) { $product = new ProductVO(); $product->setId($row['Id']); $product->setName($row['Name']); $product->setDescription($row['Description']); $product->setPrice($row['Price']); $product->setImage( "../assets/products/" . ($row['Name']) . ".jpg" ); $product->setThumbnail( "../assets/products/" . ($row['Name']) . "_sm.jpg" ); products[] = $product; } return products; } monk.e.boy Quote Link to comment https://forums.phpfreaks.com/topic/49300-solved-dao-array/#findComment-241609 Share on other sites More sharing options...
pedropedroc Posted April 30, 2007 Author Share Posted April 30, 2007 Thankyou so much! If anyone else has the same problem, just add a couple of variable signs (i.e. $'s), in front of 'products' - so that 'products' reads $products. Make the additions on lines 4, and 17 (empty lines inclusive). Quote Link to comment https://forums.phpfreaks.com/topic/49300-solved-dao-array/#findComment-241618 Share on other sites More sharing options...
monk.e.boy Posted April 30, 2007 Share Posted April 30, 2007 Oh yeah :D I've been coding too much Python today :D my mistake monk.e.boy Quote Link to comment https://forums.phpfreaks.com/topic/49300-solved-dao-array/#findComment-241648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.