Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple Textarea's in php loop

    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:
    PHP Code:
    $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 $quantity2);
                
    $total number_format($total $line_cost2); 
                
                if (
    $total "10.00") {
                    
    $delivery number_format("2.00"2);
                    
    $total number_format($total $delivery2);
                    }
                else if (
    $total "10.00") {
                    
    $delivery number_format("0.00"2);
                    
    $total number_format($total $delivery2);
                    }

                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.

    PHP Code:
    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

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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

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

    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, this is my basket:

    PHP Code:
    <?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 $quantity2);
                
    $total number_format($total $line_cost2); 
                
                if (
    $total "10.00") {
                    
    $delivery number_format("2.00"2);
                    
    $total number_format($total $delivery2);
                    }
                else if (
    $total "10.00") {
                    
    $delivery number_format("0.00"2);
                    
    $total number_format($total $delivery2);
                    }

                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 Code:
    <?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 $quantity2);
                
    $total number_format($total $line_cost2); 
                if (
    $total "10.00") {
                    
    $delivery number_format("2.00"2);
                    
    $total number_format($total $delivery2);
                    }
                    else if (
    $total "10.00") {
                    
    $delivery number_format("0.00"2);
                    
    $total number_format($total $delivery2);
                    }
                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($total2);
    $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?
    Last edited by php_no_idea; 11-19-2010 at 03:31 AM.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    OK well ideally you would be storing the instructions in you cart session, however if you change
    PHP Code:
      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 ..

    PHP Code:
     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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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 ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •