Jump to content

using an array and mySQL to populate a table


Merdok

Recommended Posts

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.

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.

 

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.