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++; } Link to comment https://forums.phpfreaks.com/topic/211112-print-array-of-results/ 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" Link to comment https://forums.phpfreaks.com/topic/211112-print-array-of-results/#findComment-1100970 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? Link to comment https://forums.phpfreaks.com/topic/211112-print-array-of-results/#findComment-1100974 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++; } Link to comment https://forums.phpfreaks.com/topic/211112-print-array-of-results/#findComment-1100984 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. Link to comment https://forums.phpfreaks.com/topic/211112-print-array-of-results/#findComment-1101010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.