Jump to content

[SOLVED] DAO array


pedropedroc

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/49300-solved-dao-array/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/49300-solved-dao-array/#findComment-241609
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.