• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP Mysql

WebWatcher

New Member
I have to somehow make these arrays using php reading the values from a mysql database. Can you help.

The Array name isneeds to be fruits.

Then in the array it has the name of the fruit. Image, description and then a onclick link.

*********************************************************
$fruits = array(
'Apple' => array('image' => 'assets/images/fruits/apple.jpg', 'description' => 'One of America\'s favorite fruits.', 'onclick' => 'One of America\'s favorite fruits.'),
'Avocado' => array('image' => 'assets/images/fruits/avocado.jpg', 'description' => 'The avocado is a dense, evergreen tree, shedding many leaves in early spring.'),
);
*********************************************************

The php output has to look like the above.

Thanks in advance
 
This isn't your homework is it?

PHP:
$fruits = array(); // create the placeholder array for the fruits
$query = mysql_query("SELECT name,image,description,onclick FROM fruits"); // first you must "query" (ask) the database so it can give you what you want

while($row = mysql_fetch_array($query, MYSQL_ASSOC)) { // then for each result, assign it to $row until all results have been exhausted
    $fruits[$row["name"]] = array("image" => $row["image"], "description" => $row['description'], "onclick" => $row["onclick"]);
}


echo "<pre>" . print_r($fruits, true) . "</pre>"; // output the result
 
Back
Top