Merdok Posted June 8, 2009 Share Posted June 8, 2009 Ok apologies if I explain this in a convoluted manner, its been a very long day. I have been building a shopping cart program and I have just added a module to allow 'sets' of products to be made out of a number of existing products. I've made it so the database is written to perfectly now, the products contained int he set are stored in a serialized array in a db column. My problem lies when trying to display the set itself. I've managed to make it so it will show all the information from the set, this is a simple SELECT statement, however I want to list the products from the set at the bottom of the page, this is the code which does that: <div class="extra_content"> <h1>Products contained in this set</h1> <?php $prodData=unserialize($prodArray); if (is_array($prodData) == false) { echo 'It is not an array!'; exit; } $itemcount = count($prodData); if ($itemcount == 0) { echo 'Array is empty!'; } for ($i=0;$i<$itemcount;$i++) { echo $prodData[$i] . ' '; // Selects the products within the selected category $result = mysql_query("SELECT COUNT(*) AS total_entries FROM bolt_shop_prod WHERE prodID = $prodData[$i]") or die(mysql_error()); $row = mysql_fetch_row($result); $total_entries = $row[0]; if(isset($_GET['page_number'])) { $page_number = $_GET['page_number']; } else { $page_number = 1; } $total_pages = ceil($total_entries / 5); $offset = ($page_number - 1) * 5; $prodlookup = "SELECT * FROM bolt_shop_prod WHERE prodID = $prodData[$i] ORDER BY price ASC LIMIT $offset, 5"; $data2 = mysql_query($prodlookup) or die('Failed to return data: ' . mysql_error()); The rest of the code just outputs the results fromt he select. As you can see, it is supposed to pick all the products from bolt_shop_prod which are in the array, however it can only see the last product. Is there a way I can effectivley loop through the sql statement to select ALL the products from the array? Obviously I don't really want to have to do the query too many times (there can be as many as 25 products in a set) as that might have a detrimental effect on the server. I hope that was clear enough, if not then I'll try to clarify what I mean tomorrow. Thanks in advance. Alex. Link to comment https://forums.phpfreaks.com/topic/161386-using-an-array-and-mysql-to-populate-a-table/ Share on other sites More sharing options...
Merdok Posted June 8, 2009 Author Share Posted June 8, 2009 BTW this code makes it look as if $prodArray is undefined, its not, it takes is values from a select statement further up the page, I know it contains all the values from the array as I have echoed it out perfectly. Link to comment https://forums.phpfreaks.com/topic/161386-using-an-array-and-mysql-to-populate-a-table/#findComment-851694 Share on other sites More sharing options...
alco19357 Posted June 8, 2009 Share Posted June 8, 2009 not quite sure what you're asking... but try using a loop: while($row = mysql_fetch_array($queriedstring)){ // code here } Link to comment https://forums.phpfreaks.com/topic/161386-using-an-array-and-mysql-to-populate-a-table/#findComment-851857 Share on other sites More sharing options...
Merdok Posted June 10, 2009 Author Share Posted June 10, 2009 no i'm not sure that would work. Basically what i'm trying to do is pull specific rows from the bolt_shop_prod database, the products that I want are stored in an array called $prodData. The problem i'm having is that the query can only see the LAST product in the array, I basically need a way to split the array up so the query can return ALL products in the array. (there can be as few as 2 products and as many as 25 in the array) the query currently looks like this: SELECT COUNT(*) AS total_entries FROM bolt_shop_prod WHERE prodID = $prodData[$i]" but that did not work at all. It still only returns the last product. Link to comment https://forums.phpfreaks.com/topic/161386-using-an-array-and-mysql-to-populate-a-table/#findComment-852939 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.