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
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"

Link to comment
Share on other sites

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++;
}

Link to comment
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.