Jump to content

Print array of results


c_pattle

Recommended Posts

What I'm trying to achieve is to get a set of data from a database and then cycle through the array and add the contents of each row to a variable.  I'm trying to use $i to cycle through the array but at the moment it's just printing the first character of each set.  Any help would be great. 

 

$fav_products_sql = "select fav_products.product_id, fav_products.product_order, product_list.product_name from fav_products, product_list where fav_products.product_id = product_list.id";
$fav_products_rs = mysql_query($fav_products_sql, $conn);
$fav_products = mysql_fetch_array ($fav_products_rs);

while ($i < 11) {
$_SESSION['fav_products'] = "<li>" . $fav_products['product_order']['$i'] . ": " . $fav_products['product_name']['$i'] . "</li>";
$i++;
}

Link to comment
https://forums.phpfreaks.com/topic/211112-print-array-of-results/
Share on other sites

Try this:

 

while ($fav_products = mysql_fetch_array($fav_products_rs)) {
  var_dump($fav_products);
}

 

If it shows all your rows, then replace the var_dump() with the code you want to have in there.

 

BTW your code as it is will replace $_SESSION['fav_products'] each time through the loop.  Probably you want something like this:

 

$_SESSION['fav_products'] = '';
while ($fav_products = mysql_fetch_array($fav_products_rs)) {
  $_SESSION['fav_products'] .= "<li>" . $fav_products['product_order'] . ": " . $fav_products['product_name'] . "</li>";
}

 

The ".=" instead of the "=" means "Add on to the end".  Also known as "append" or "concatenate"

yeah so basically I just want to start of with this code which adds all of the products into variable called $_SESSION['fav_product_array'].  However when I did print_r on that variable I think it only contains the first line of the dataset. 

 

$fav_products_sql = "select fav_products.product_id, fav_products.product_order, product_list.product_name from fav_products, product_list where fav_products.product_id = product_list.id";
$fav_products_rs = mysql_query($fav_products_sql, $conn);
$_SESSION['fav_products_array'] = mysql_fetch_array ($fav_products_rs);

 

Then I want to cycle through the array and print out all of the rows however I don't know how to do it as this code isn't working. 

 

while ($i < 11) {
$_SESSION['fav_products'] .= "<li>" . $_SESSION['fav_products_array']['product_order'][$i] . ": " . $_SESSION['fav_products_array']['product_name'][$i] . "</li>";
$i++;
}

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.