Jump to content

Get data from same row as check box value


perky416

Recommended Posts

Hi Guys,

 

Im using the code below to display the value of checked checkboxes on form submission, however im also trying to pull the price from the database for each checked value.

 

		foreach ($_POST['check'] as $product)
		{
			echo $row['price'] . " " . $row['currency'] . " for " . $product . "<br />";	
		}

 

Please could somebody point me in the right direction as to what id need to do. As iv only been able to get it to display the price of the first checked value.

 

Thanks

Link to comment
Share on other sites

Iv managed to figure it out,

 

It always seems that just after i post asking for help i get some inspiration that helps me figure it out lol.

 

If anybody else needs this answer in the future i simply put a while loop inside the foreach with the query selecting results where product=$product.

Link to comment
Share on other sites

Nested loops are almost always a bad idea.  What you should do is pre-process your query data so that you get a nicely formatted "lookup array," where the key to the array is the product ID you're getting from this $_POST['check'] array, and the value of the array is itself an array that features the data you need.  Your echo would then look like:

 

echo $data[$product]['price'] . " " . $data[$product]['currency'] . " for " . $product . "<br />";

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.