...

View Full Version : Multiple Textarea's in php loop



php_no_idea
11-19-2010, 02:35 AM
Hi Guys,

I am just in the middle of designing a shopping cart for a local company, but I have hit a brick wall.

The customers add their food items into the shopping cart and for each item added there is a textarea for any "special instructions" (It's a kebab house, so its for things like sauces, meal deal contents etc). Thing is, each loop that is formed, has a textarea with it. Problem I am having though, is because it is in a loop the textarea has the same name. Well I have sorted that bit, as you can see below. Only problem now is, I dont know how i can echo the data from the textarea with the variable name, into the order summary page?? Any ideas??


This is the end of the textarea loop:

$sql = sprintf("SELECT title, description, price FROM sub_menu WHERE id='$product_id'");

$result = mysql_query($sql);


if(mysql_num_rows($result) > 0) {

list($title, $description, $price) = mysql_fetch_row($result);



$line_cost = number_format($price * $quantity, 2);
$total = number_format($total + $line_cost, 2);

if ($total < "10.00") {
$delivery = number_format("2.00", 2);
$total = number_format($total + $delivery, 2);
}
else if ($total > "10.00") {
$delivery = number_format("0.00", 2);
$total = number_format($total + $delivery, 2);
}

echo "<tr class=cart_tr>";

echo "<td align=\"left\" class=cart_title>$title</td>";

echo "<td align=\"center\" class=cart_quantity>Quantity: $quantity</td>";
echo "<td align=\"right\">$line_cost</td>";
echo "<td align=\"right\"><a class=remove_link href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">Remove</a></td>";
echo "</tr>";
echo "<tr>";
echo "<td class=formfield><label>Special Instructions:</label><br>(e.g Kebab Sauce, Meal Deal instructions, Parking restrictions etc)</td><td><textarea name=special";
echo $product_id;
echo " rows=5></textarea></td></tr>";


}

This is where i am echo'n it: (please note that the $special_instruction is defined at $_POST['special'] at the top of the page.


if(mysql_num_rows($result) > 0) {
echo "<tr class=cart_tr>";
echo "<td align=\"left\" class=cart_title>$title</td>";
echo "<td align=\"center\" class=cart_quantity>Quantity: $quantity</td>";
echo "<td align=\"right\">$line_cost</td>";
echo "</tr>";
echo "<tr><td>";
echo $special_instruction;
echo $product_id;
echo "</td></tr>";
}


Anyone know of a workaround to this?

Thanks in advance

DrDOS
11-19-2010, 03:16 AM
You can use a loop to build the forms with all their elements, and give them consecutive names, you can use an array to contain all the data that goes into building the forms. You can use another loop to compile all the entries that the forms present. You have powerful ways available to simplify the whole process of coding the forms, and make it easier to make changes.

Would be nice to have a more clear idea of what exactly is needed.

php_no_idea
11-19-2010, 03:23 AM
Thank you for your reply.

Basically what it is is this:

A customer add's a food product to their basket (.ie kebab which they would like extra sauce on, or a meal deal which they have to define which kebab/pizza they want in it).

The food product gets echo'd in the basket and a <textarea> gets displayed next to it, so they can define what sauce/pizza/kebab they want.

Then they click the order button and the list of items, with individual <textarea> values gets emailed to the kebab shop so they know what to put on each kebab/meal deal etc

Does that make sense??

Thanks

firepages
11-19-2010, 03:29 AM
not enough of your code to help but the principle is ...

foreach($item as $item_id){
..blah
<textarea name="special[$item_id]"></textarea>
..blah
}

can on the receiving page be accessed as



foreach($_POST['special'] as $k=>$v){
echo "item $k = $v<br />";
}

php_no_idea
11-19-2010, 03:34 AM
Ok, this is my basket:


<?php
session_start();


if($_SESSION['cart']) {

echo "<table style=border: thin solid black; padding=\"3\" width=\"90%\" align=center cellspacing=2><form name=send action=member.php?order=1 method=post>";


foreach($_SESSION['cart'] as $product_id => $quantity) {

$db=mysql_connect('localhost','user','pass') or die(mysql_error());
mysql_select_db('db', $db);
$sql = sprintf("SELECT title, description, price FROM sub_menu WHERE id='$product_id'");

$result = mysql_query($sql);


if(mysql_num_rows($result) > 0) {

list($title, $description, $price) = mysql_fetch_row($result);



$line_cost = number_format($price * $quantity, 2);
$total = number_format($total + $line_cost, 2);

if ($total < "10.00") {
$delivery = number_format("2.00", 2);
$total = number_format($total + $delivery, 2);
}
else if ($total > "10.00") {
$delivery = number_format("0.00", 2);
$total = number_format($total + $delivery, 2);
}

echo "<tr class=cart_tr>";

echo "<td align=\"left\" class=cart_title>$title</td>";

echo "<td align=\"center\" class=cart_quantity>Quantity: $quantity</td>";
echo "<td align=\"right\">$line_cost</td>";
echo "<td align=\"right\"><a class=remove_link href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">Remove</a></td>";
echo "</tr>";



}

}

echo "<tr><td class=formfield><label>Special Instructions:</label><br>(e.g Kebab Sauce, Meal Deal instructions, Parking restrictions etc)</td><td><textarea name=special rows=5></textarea></td></tr>";
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Delivery:</td>";
echo "<td align=\"right\">$delivery</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
echo "<tr class=bottom_tr><td><label> When would you like it delivering?</label></td><td><select name=delivery_time><option>Select Time</option><option name=asap value=ASAP>A.S.A.P</option><option name=7pm value=7pm>7:00pm</option><option name=730pm value=7:30pm>7:30pm</option>";
echo "<option name=8pm value=8:00pm>8:00pm</option><option name=830pm value=8:30pm>8:30pm</option><option name=9pm value=9pm>9:00pm</option><option name=930pm value=9:30pm>9:30pm</option></select></td></tr>";


echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a style='font-family: Tahoma; font-size: 13px; color: gray;' href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";
echo "<div align=left class=continue_a><a href=member.php>Continue Shopping ></a></div>";
echo "<div align=left class=checkout><input type=submit name=submit value=Order></form></div>";



}else{

echo "You have no items in your shopping cart.";

}
?>

This is my place_order page:


<?php
session_start();

$email_login = $_SESSION['email'];
$special_instruction = $_POST['special'];

$time = $_POST['delivery_time'];

$db=mysql_connect('localhost','user','pass') or die(mysql_error());
mysql_select_db('db', $db);
$sql = sprintf("SELECT * FROM users WHERE email_address='$email_login'");

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {

list($id, $name, $address, $tel_number) = mysql_fetch_row($result);

echo "<div id=order_final>";
echo "<div class=order_head>Order Summary</div>";
echo "<div class=order_final_left>";
echo "<p><label>Name: </label></p>";
echo "<p><label>Address: </label></p>";
echo "<p><label>Tel Number: </label></p>";
echo "</div>";
echo "<div class=order_final_right>";
echo "<p>";
echo $name;
echo "</p><p>";
echo $address;
echo "</p><p>";
echo $tel_number;
echo "</p></div></div>";

}


if($_SESSION['cart']) {

echo "<table style=\"border: thin solid red;\" padding=\"3\" width=\"70%\" align=center cellspacing=2><form name=order_submit method=post action=sendmail.php>";


foreach($_SESSION['cart'] as $product_id => $quantity) {

include "db.php";
list($title, $description, $price) = mysql_fetch_row($result);

$line_cost = number_format($price * $quantity, 2);
$total = number_format($total + $line_cost, 2);
if ($total < "10.00") {
$delivery = number_format("2.00", 2);
$total = number_format($total + $delivery, 2);
}
else if ($total > "10.00") {
$delivery = number_format("0.00", 2);
$total = number_format($total + $delivery, 2);
}
if(mysql_num_rows($result) > 0) {


echo "<tr class=cart_tr>";
echo "<td align=\"left\" class=cart_title>$title</td>";
echo "<td align=\"center\" class=cart_quantity>Quantity: $quantity</td>";
echo "<td align=\"right\">$line_cost</td>";
echo "</tr>";

}

}

echo "<tr><td>Special Instructions:</td></tr><tr><td>";
echo $special_instruction;
echo "</td></tr>";
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Delivery:</td>";
echo "<td align=\"right\">$delivery</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total:</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
echo "<tr><td colspan=\"2\" align=\"right\">Delivery Time:</td>";
echo "<td align=\"right\">$time</td></tr>";
echo "</table>";


}else{

echo "You have no items in your shopping cart.";

}
?>
<?php

$ToEmail = 'me@here.comk';
$EmailSubject = 'Order ';
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$mailheader .= "From: us@here.com";
$mailheader .= "Cc:".$email_login."";
$MESSAGE_BODY = "<br>";
$MESSAGE_BODY .= "Name: ".$name."<br>";
$MESSAGE_BODY .= "Address: ".$address."<br>";
$MESSAGE_BODY .= "Tel Number: ".$tel_number."<br>";
$MESSAGE_BODY .= "<table width=500 style=border: red;>";
$MESSAGE_BODY .= "<tr><td>Order:</td></tr>";
foreach($_SESSION['cart'] as $product_id => $quantity) {
include "db.php";
list($title, $description, $price) = mysql_fetch_row($result);
if(mysql_num_rows($result) > 0) {
$MESSAGE_BODY .= "<tr><td class=cart_title>$title</td>";
$MESSAGE_BODY .= "<td class=cart_quantity>Quantity: $quantity</td>";
$MESSAGE_BODY .= "<td>$line_cost</td></tr>";
}
}
$MESSAGE_BODY .= "<tr><td>Special Instructions:</td></tr><tr><td>";
$MESSAGE_BODY .= $special_instruction;
$MESSAGE_BODY .= "</td></tr><tr>";
$MESSAGE_BODY .= "<td align=left>Total:</td><td align=right>";
$MESSAGE_BODY .= number_format($total, 2);
$MESSAGE_BODY .= "</td>";
$MESSAGE_BODY .= "</tr><tr><td align=left>Delivery Time:</td><td align=right>";
$MESSAGE_BODY .= $time;
$MESSAGE_BODY .= "</td></tr></table>";

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die (error);

unset($_SESSION['cart']);

?>



Does that help at all?

firepages
11-19-2010, 03:51 AM
OK well ideally you would be storing the instructions in you cart session, however if you change


echo "<tr><td class=formfield><label>Special Instructions:</label><br>(e.g Kebab Sauce, Meal Deal instructions, Parking restrictions etc)</td><td><textarea name=special rows=5></textarea></td></tr>";

to
echo "<tr><td class=formfield><label>Special Instructions:</label><br>(e.g Kebab Sauce, Meal Deal instructions, Parking restrictions etc)</td><td><textarea name=
\"special[$product_id]\" rows=5></textarea></td></tr>";


then in the display, change all instances of $special_instruction to ..



echo $_POST['special'][$product_id];


that might work as-is?
not that I think your cart is structured properly, anything individual to each item should IMO be part of the cart itself e.g. $cart->id, $cart->special_instructions,$cart->customer_id ... etc etc

php_no_idea
11-19-2010, 04:02 AM
Gave that a try and nothing. Doesnt display any of the values at all...

How do you mean part of the cart itself?

Not sure if i fully understand

firepages
11-19-2010, 04:09 AM
don't worry about the cart stuff at the moment, too far gone to change... on the display page what do you see when you <? echo '<pre>';print_r($_POST);echo '</pre>';?>, do you see the 'special' array ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum