HanneSThEGreaT Posted October 10, 2011 Share Posted October 10, 2011 Hello. I'm trying to append the loop keys and values into a string and store it into a hidden field. This is the code I have : if (isset($_POST['ordered'])) { // We can assign $_POST['product'] to a variable to make it easier to type out $prod = $_POST['product']; // Throw a little intro to see how many they checked echo 'You selected ' , count($_POST['ordered']) , ' products.'; // Now we loop through it and get the IDS that were selected // Foreach lets you select the $key and the $value for each iteration of the loop // $strOrder = ""; foreach ($_POST['ordered'] as $key=>$id) { echo '<p/> ProdName: ' , $prod[$id]['name'] , '<br/> ProdPrice: ' , $prod[$id]['price'] , '<br/> ProdQty: ' , $prod[$id]['qty'] , ' <hr/>'; $strOrder .= $key . " " . $id; $TOT += $prod[$id]['price'] * $prod[$id]['qty']; } $TOT2 = $TOT + 55; echo '<input type="hidden" Name="TOT" value ="'. $TOT .'">'; echo '<input type="hidden" name="TOT2" value =". $TOT2 .'">'; echo '<input type="hidden" name="Order" value=". $strOrder . '">'; } strOrder does not get set to a value. Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/ Share on other sites More sharing options...
titan21 Posted October 10, 2011 Share Posted October 10, 2011 check the output of print_r($_POST) to make sure data is being passed first of all. Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277766 Share on other sites More sharing options...
HanneSThEGreaT Posted October 10, 2011 Author Share Posted October 10, 2011 It does return everything posted Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277767 Share on other sites More sharing options...
Muddy_Funster Posted October 10, 2011 Share Posted October 10, 2011 I'm suprised you're not getting any errors. what happens if you change it to this? foreach ($_POST['ordered'] as $key=>$id) { echo '<p/>'; echo "ProdName: {$prod[$id]['name']}<br/>"; echo "ProdPrice: {$prod[$id]['price']}<br/>"; echo "ProdQty: {$prod[$id]['qty']}<hr/>"; //not sire what <hr/> is but i'm sure you have a reason for it $strOrder .= $key . "," . $id.";"; } echo "strOrder String == $strOrder"; Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277770 Share on other sites More sharing options...
HanneSThEGreaT Posted October 10, 2011 Author Share Posted October 10, 2011 I get this echo 'ed : strOrder String == 0,2;1,11;2,20; Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277777 Share on other sites More sharing options...
Muddy_Funster Posted October 10, 2011 Share Posted October 10, 2011 well $strOrder is getting assigned values from the array using that, so just change the echo "strOrder String == $strOrder"; to your original code and you should be good to go. Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277778 Share on other sites More sharing options...
HanneSThEGreaT Posted October 10, 2011 Author Share Posted October 10, 2011 I'm afraid I don't understand. It seems as if the array indexes are stored into strOrder in stead of those indexes' values. I need the values inside the Order hidden field. I am very dumb, and very new to PHP, but you guys have made my learning experience fun Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277783 Share on other sites More sharing options...
Muddy_Funster Posted October 10, 2011 Share Posted October 10, 2011 it's storing both: strOrder String == 0,2;1,11;2,20; the array index is the number before the , and the value of that index is the number between the , and the ; what I think you want is $strOrder .= $id." ".$prod['$id']['name']; If I'm wrong show me an example of what you expect $strOrder to look like. Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1277789 Share on other sites More sharing options...
HanneSThEGreaT Posted October 11, 2011 Author Share Posted October 11, 2011 Hello again. That gives me the indexes as well. I am stupid, way past stupid actually. I'm looking for something similar to : ProdName: Anti Stretchmark Cream ProdPrice: 40.00 ProdQty: 3 ProdName: The New Me Combo Pack 6 ProdPrice: 160.00 ProdQty: 3 I also tried the impldoe method : $strOrder = ""; foreach ($_POST['ordered'] as $key=>$id) { echo '<p/> ProdName: ' , $prod[$id]['name'] , '<br/> ProdPrice: ' , $prod[$id]['price'] , '<br/> ProdQty: ' , $prod[$id]['qty'] , ' <hr/>'; // $strOrder .= $key . "," . $id.";"; $strOrder .= $id." ".$prod['$id']['name']; $strOrder .= $id." ".$prod['$id']['price']; $strOrder .= $id." ".$prod['$id']['qty']; $TOT += $prod[$id]['price'] * $prod[$id]['qty']; } $strOrder2 = implode(",",$strOrder); $TOT2 = $TOT + 55; echo '<input type="hidden" Name="TOTALH" value ="'. $TOT .'">'; echo '<input type="hidden" name="TOTAL2H" value ="'. $TOT2 .'">'; echo '<input type="hidden" name="Order" value="'. $strOrder2 . '">'; } But it gives me an empty string. I just need a way to get the orders displayed. I get the indexes, but I do not know how to get those indexes' values Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1278044 Share on other sites More sharing options...
Buddski Posted October 11, 2011 Share Posted October 11, 2011 If you are trying to post the string to the email form (from the other thread), you can always just send exactly what is displayed on this page. if (isset($_POST['ordered'])) { $prod = $_POST['product']; echo 'You selected ' , count($_POST['ordered']) , ' products.'; $strOrder = ''; foreach ($_POST['ordered'] as $key=>$id) { $strOrder .= '<p/> ProdName: ' . $prod[$id]['name'] . '<br/> ProdPrice: ' . $prod[$id]['price'] . '<br/> ProdQty: ' . $prod[$id]['qty'] . ' <hr/>'; $TOT += $prod[$id]['price'] * $prod[$id]['qty']; } // After the loop just echo the string and also send it in the form // echo $strOrder; $TOT2 = $TOT + 55; echo '<input type="hidden" Name="TOT" value ="'. $TOT .'">'; echo '<input type="hidden" name="TOT2" value ="'. $TOT2 .'">'; echo '<input type="hidden" name="Order" value="'. $strOrder . '">'; } Remember that those hidden fields MUST be inside a form for them to be sent across. Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1278058 Share on other sites More sharing options...
HanneSThEGreaT Posted October 11, 2011 Author Share Posted October 11, 2011 Just did that now and it works! Thanks, now I have a lot of info to study! You guys are the best! Hopefully one day, I'll get there with PHP Quote Link to comment https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/#findComment-1278062 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.