Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/248807-appending-a-string-in-a-foreach-loop/
Share on other sites

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";

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

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.

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

 

 

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.

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.