Solarpitch Posted August 15, 2008 Share Posted August 15, 2008 Hey, I have the following array that will run the function for each value of val[]. This part is fine and works grand. It will call the function inserting both values into it. $k being the id and $v being the value of the textfield. The only problem is I have another array that I need to include ... $_POST['total']... and pass the values into the function also. <?php //First function and array foreach ($_POST['val'] as $k => $v) { if ($v != '' && $k != 'submit') { echo confirm_order($k, $v); } } //Second array foreach ($_POST['total'] as $k => $v) { if ($v != '' && $k != 'submit') { } } //Here's what I was trying but its wrong... but you can see the logic of what I'm trying to do foreach (($_POST['val'] as $k => $v) && foreach ($_POST['total'] as $a => $b)) { if ($v != '' && $k != 'submit') { echo confirm_order($k, $v, $a, $b); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/ Share on other sites More sharing options...
thebadbad Posted August 15, 2008 Share Posted August 15, 2008 Let us see an example of the arrays ($_POST['val'] and $_POST['total']). And does confirm_order() take 2 or 4 parameters (or a variable number)? Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617239 Share on other sites More sharing options...
friedemann_bach Posted August 15, 2008 Share Posted August 15, 2008 Do the arrays $_POST['val'] and $_POST['total'] have the same length? Maybe it would help to rearrange your form data (assuming that 'val' and 'total' make up one row), structuring it into rows instead of columns. An example would help. Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617246 Share on other sites More sharing options...
Solarpitch Posted August 15, 2008 Author Share Posted August 15, 2008 Ok <?php //This is a snippet of where the textfields are being first called. Their dynamic based on the results being returned from the databse. Just in the image below .... <td><input type='text' name='val[".$row[0]."]' id='quantity' /></td> <td><input type='text' name='total[".$row[0]."]' id='total'/></td> ?> This is what the form looks like.. Confirm_Order() takes 2 parameters at the min, but I need it to take 4 just like in my efforts on the first post. So basically as in the image above I want to get all the values of the quantity and total fields and pop them into the function like I have already done with just the quantity. Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617249 Share on other sites More sharing options...
Solarpitch Posted August 15, 2008 Author Share Posted August 15, 2008 Yes they have the same amount of values. As you can see I will store all the quantities of each product and all the totals of each products in the array I have... $_POST['val'] and $_POST['total'] I then want to loop through them like I'm doing for $_POST['val'] and insert the values into the function. But now, I need to also get the values from $_POST['total'] and include them into the function. Hope this makes sence Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617252 Share on other sites More sharing options...
friedemann_bach Posted August 15, 2008 Share Posted August 15, 2008 Good. Try to make the form appear like this: <input name="rows[1][value]" id='value_1' type="text"/> <input name="rows[1][total]" id='total_1' type="text"/> Your $_POST will then look like this: Array ( [rows] => Array ( [1] => Array ( [value] => 10.00 [total] => 55.00 ) [2] => Array ( [value] => ... ... ) ) ) Then it would be easier to loop through all elements: foreach ($_POST['rows'] as $row) { echo confirm_order($row[value],$row[total],...); } Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617263 Share on other sites More sharing options...
Solarpitch Posted August 15, 2008 Author Share Posted August 15, 2008 Ok cool... I'll give it a shot... but I will need to leave the id on the textfields the way I had it, as I use JavaScript getlementById to calculate the totals when a value is inserted into a field. Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617267 Share on other sites More sharing options...
Solarpitch Posted August 15, 2008 Author Share Posted August 15, 2008 How can I pass the id of the array in also with the values... as the id of the array will be the product id <input name="rows[1][value]" id='value_1' type="text"/> <input name="rows[1][total]" id='total_1' type="text"/> ... name='rows[$id][value]' name='rows[$id][total]' so needs to be something like foreach ($_POST['rows'] as $row) { echo confirm_order($row[id_here][value],$row[id_here][total],...); } is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617270 Share on other sites More sharing options...
friedemann_bach Posted August 15, 2008 Share Posted August 15, 2008 Yup. Try foreach ($_POST['rows'] as $article_id => $row) { echo confirm_order($article_id,$row[value],$row[total],...); } Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617285 Share on other sites More sharing options...
Solarpitch Posted August 15, 2008 Author Share Posted August 15, 2008 Thanks for you help... I'm still working at it, almost have it... the total wont work but I'll see how I get on. Quote Link to comment https://forums.phpfreaks.com/topic/119811-array-and-looping-question/#findComment-617295 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.