perky416 Posted April 7, 2011 Share Posted April 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232995-get-data-from-same-row-as-check-box-value/ Share on other sites More sharing options...
perky416 Posted April 7, 2011 Author Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232995-get-data-from-same-row-as-check-box-value/#findComment-1198308 Share on other sites More sharing options...
ManiacDan Posted April 7, 2011 Share Posted April 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232995-get-data-from-same-row-as-check-box-value/#findComment-1198329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.