October 10th, 2011, 08:52 AM
#1
[RESOLVED] foreach loop
Hello. I have this loop :
Code:
foreach ($_POST['ordered'] as $key=>$id) {
echo '<p/>
ProdName: ' , $prod[$id]['name'] , '<br/>
ProdPrice: ' , $prod[$id]['price'] , '<br/>
ProdQty: ' , $prod[$id]['qty'] , '
<hr/>';
}
Now I am trying to create a string variable to hold all this results, so, the variable must get appended the whole time, and then stored inside a Hidden field. Simple. NO!
I have tried this :
Code:
$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;
Nothing gets stored inside strOrder.
I created the hidden fields like this :
Code:
$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 . '">';
But NOTHING gets stored in any of them either.....
What am I doing wrong now??
October 10th, 2011, 09:40 AM
#2
Re: foreach loop
OK, progress :
Code:
$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="TOTAL" value ="'. $TOT .'">';
echo '<input type="hidden" name="TOTAL2" value ="'. $TOT2 .'">';
echo '<input type="hidden" name="Order" value="'. $strOrder . '">';
}
echo "strOrder String == $strOrder";
Gives me :
Code:
strOrder String == 0,2;1,11;2,20;
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
October 10th, 2011, 05:53 PM
#3
Re: foreach loop
You're making this way too difficult on yourself. Use PHP's built-in implode() method for making strings out of arrays.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
October 11th, 2011, 03:25 AM
#4
Re: foreach loop
Thanx Peej, but I got it working.
I AM SO STUPID!! It was so simple!
Code:
foreach ($_POST['ordered'] as $key=>$id) {
echo '<p/>
ProdName: ' , $prod[$id]['name'] , '<br/>
ProdPrice: ' , $prod[$id]['price'] , '<br/>
ProdQty: ' , $prod[$id]['qty'] , '
<hr/>';
$strOrder2 .= "<p/>ProdName: " . $prod[$id]['name'] . "<br/>ProdPrice: " . $prod[$id]['price'] . "ProdQty: " . $prod[$id]['qty'];
$TOT += $prod[$id]['price'] * $prod[$id]['qty'];
}
$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 . '">';
Thanx anyways!
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks