views:

459

answers:

1

I'm new to PHP... I want to know how to populate a form from mySQL. I'm stumped at the input section where I am trying to allow the user to select choices for radio button /checkbox for each record. thanks in advance for anyone's help!

TC

Here's a snippet of my code:

<?php

$mQuantity = 1;

$con = mysql_connect("localhost","t","c");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tc", $con);

/*
    if request.form("itemname")<>" " then
     cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%'  ORDER BY FOODTYPE, ITEM"
    else
     cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' ORDER BY FOODTYPE, ITEM"
    end if
*/

// retrieve form data 
$input = $_POST['itemname'];
echo $_POST['ITEM'];
$mItem = $_POST['ITEM'];
$mPrice = $_POST['PRICE'];
$mQuantity = $_POST['QUANTITY'];
$mOrderTotal = 0;
$mSide0 = '1';
$mOil = '1';
$mStarch = '1';
$mSalt = '1';

// use it 
echo "You searched for: <i>$input</i>"; 
echo $mSessionID;

mysql_query("INSERT INTO OrderQueue (SessionID,item,quantity,price,no_oil,no_starch,no_salt,side0) VALUES ('$mSessionID','$mItem','$mQuantity','$mPrice','$mOil','$mStarch','$mSalt','$mSide0')");

/*
$sql="SELECT * FROM Restaurant_Menus
WHERE
Item like '%$input%'";
*/


$sql="SELECT * FROM OrderQueue
WHERE
SessionID = '$mSessionID'";

$result = mysql_query($sql); 

//('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

//echo $input;
//echo $sql;

/*
if ($input!=" "){
    $result = mysql_query($sql); 
    }

//    cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%'  ORDER BY 
else
{   $result = mysql_query("SELECT * FROM Restaurant_Menus");
}
*/






$c=1;
$class[1] = 'odd';
$class[2] = '';
$array_no_oil = array();
$array_no_salt = array();
$array_no_starch = array();
$array_rice = array();




echo "<table border='1' width=500px>
<tr>
<th></th>
<th align=left>Item</th>
<th align=right>Price</th>
<th align=right>Quantity</th>
</tr>";

//<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">

while($row = mysql_fetch_array($result))
    {
    //echo '<tr class="'.$class[$c].'" onMouseOver='#F3EB49' onMouseOut ='#DDDDDD' bgcolor="#DDDDDD" >';
    //echo "<tr class='odd'>";
    //echo '<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">'
?>

    <TR onMouseover="this.bgColor='#FFC000'"onMouseout="this.bgColor='#DDDDDD'">

  <?php
/*
        <form action="Orders.asp" method="post" target="_top" name="LogonForm">
        <td><font size = 2>

        <!--<%= mQuantity %>-->        
        <INPUT TYPE="TEXT" NAME="QUAN" VALUE="1" SIZE=2>
        </font></td>
        <td width=50px><font size = 2>
     <INPUT TYPE=HIDDEN NAME=ITEM VALUE="<% =mItem %>">
     <INPUT TYPE=HIDDEN NAME=PRICE VALUE=<% =mPrice %>>
     <INPUT TYPE=HIDDEN NAME=RID VALUE= <% =mRestaurant_ID %>>  
     <INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
     <input id="Choices" class="findit" type="submit" value ="Order" />
     </form>
*/
?>


<?php   



// Obtain list of images from directory 
//$img = getRandomFromArray($imgList);
}

?>



    <?php

    if ($row['Picture']!=" "){
     echo "<td><a><img src='images/".$row['Picture'].".JPG' height=50px></a></td>";
    }
    else{
     echo "<td></td>";
    }

    echo "<td width=200><b>" . $row['Item'] . "</b><br>


    <input class='dropwidth' type='radio' name='$array_rice' value='1' selected>White Rice<br>
    <input type='radio' name='$array_rice' value='2'>Pork Fried Rice<br>
    <input type='radio' name='$array_rice' value='3'>Brown Rice<br>
    <input type='checkbox' name='$array_no_oil' value='1' />No Oil
    <input type='checkbox' name='starch' value='no oil' />No Starch
    <input type='checkbox' name='salt' value='no salt' />No Salt
    </td>";

    echo "<td width=50 align=right>" . number_format($row['Price'],2) . "</td>";
    $mQuantity = "'" . number_format($row['Quantity'],0) . "'";
    $mPrice = "'" . number_format($row['Price'],2) . "'";
    $mLineItemTotal = $row['Quantity'] * $row['Price'];
    $mOrderTotal = (number_format($mOrderTotal,2) + number_format($mLineItemTotal,2));
    echo $mOrderTotal;
    $mLineItemTotal2 = "'". number_format($mLineItemTotal,2) . "'";
    //echo "<td>" . $mQuantity. "</td>";
?>        
      <form action="orders.php" method="post" target="_top" name="LogonForm">
        <td width="50" align=right><font size = 2>

          <!--<%= mQuantity %>-->

          <!--<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php $mQuantity; ?>>-->
          <INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php echo $mQuantity.";" ?>/>
        </font></td>



        <?php echo "<td width=50 align=right>" . $mLineItemTotal . "</td>";?>

        <!--<td width=50px><font size = 2>-->
        <!--<INPUT TYPE="TEXT" NAME="LINEITEMTOTAL" VALUE=<?php echo $mLineItemTotal.";" ?> WIDTH=10/>-->
          <INPUT TYPE=HIDDEN NAME=ITEM VALUE=<?php $mItem ?> />
          <INPUT TYPE=HIDDEN NAME=PRICE VALUE=<?php $mPrice ?>/>
          <INPUT TYPE=HIDDEN NAME=RID VALUE=<?php $mRestaurant_ID ?>/>   
          <INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
          <!--<input id="Choices" class="findit" type="submit" value ="Order" />-->
        </form>     



  <?php 

    echo "</tr>";
    if($c==2) $c=0;
    $c++;
    }
echo "</table>";



    echo "<div>".$mOrderTotal."</div>";


mysql_close($con);
?>
A: 

You should name your HTML form elements as such: MyElement[] when using them as arrays. For example:

<form method="post" name="myForm" action="myForm.php">
    <select multiple name="myFormSelectMultiple[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</form>

In this select multiple example, when the first two items are selected and then posted this form would produce similar output to the following:

array('myFormSelectMultiple' => array(1,2));

Hope this helps!

Jay