c_pattle Posted August 18, 2010 Share Posted August 18, 2010 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++; } Quote Link to comment Share on other sites More sharing options...
btherl Posted August 18, 2010 Share Posted August 18, 2010 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" Quote Link to comment Share on other sites More sharing options...
c_pattle Posted August 18, 2010 Author Share Posted August 18, 2010 Thanks. Yeah I was going to do it that way but then I was wondering if there was a way to use $i to cycle through the array. For example what about if I just wanted to select the 6th row in the array? Quote Link to comment Share on other sites More sharing options...
c_pattle Posted August 18, 2010 Author Share Posted August 18, 2010 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++; } Quote Link to comment Share on other sites More sharing options...
btherl Posted August 18, 2010 Share Posted August 18, 2010 mysql_fetch_array() gets only one product, not all products. You could use mysql_data_seek() to move to the row you want, but it makes more sense to use the SQL offset and limit commands to get only the rows you need. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.