Jump to content

display db query results based on values from an array


DarkJamie

Recommended Posts

Hi folks. I'm back with another question on displaying array results. My problem is pretty simple, I just haven't been successful in executing it. I have a page that displays a list of products with checkboxes and prices. The checked items go into an array called "reports". The data is then passed to the selection page, where I wish to display each product description and price based on the ID values from the checkbox array.

 

So what I am asking is how do I display the description and price of each product in the array (and then print a sum of their prices)?

 

here is my code for the selection page. It is incomplets, as I have cleaned out my erroneous code.

 

if (isset($_POST['reports']))
{
	foreach ($_POST['reports'] as $key => $rid)


echo "<h2>Individual Reports</h2>";
echo "<h4>You've selected the following reports:</h4>";


	while ($row=mysql_fetch_array($sql_result)) {
	    $id = $row["id"];
    	    $file_number=$row["file_number"];
      	    $filename=$row["filename"];
      	    $description=$row["description"];
	    $price=$row["price"];
	    $active=$row["active"];


		echo "<table border=0>";
		echo "<tr>";
		echo "<td width=320px>$description</td>";
		echo "<td><b>$price</b></td>";
		echo "</tr>";
		echo "</table>";

	}
  
  echo "</table>"; 

}

if you've got all the report IDs in the $_POST['reports'] array, you can collect them into one string and SELECT all of the report info from the db:

 

$rid_list = implode(',', $_POST['reports']);
$query = "SELECT * FROM reports WHERE ID IN ($rid_list) ORDER BY whatever";

 

to get the total price, you can simply add each individual report price to a total as you display them.

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.