Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.