Jump to content

help in setting selected="selected" list box with php and mysql data


garethhall

Recommended Posts

Hi guys,

Hopefully one of yol know the solution as it has been eluding me so far .

When my from value is calculated (update button is pressed) I want the form to remember previously entered values for my list boxes (venue and ticketType). The code in red is where i need help or just someone to point me in the right direction.

 

<?php require_once('../Connections/connect.php'); ?> <?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
   ########################################################################################################################
    //DB query list
    ########################################################################################################################-->
mysql_select_db($database_connect, $connect);
$query_concertName = "SELECT concertName FROM concertTb";
$concertName = mysql_query($query_concertName, $connect) or die(mysql_error());
$row_concertName = mysql_fetch_assoc($concertName);
$totalRows_concertName = mysql_num_rows($concertName);

mysql_select_db($database_connect, $connect);
$query_venue = "SELECT venueName FROM venueTb ORDER BY venueName";
$venue = mysql_query($query_venue, $connect) or die(mysql_error());
$row_venue = mysql_fetch_assoc($venue);
$totalRows_venue = mysql_num_rows($venue);

mysql_select_db($database_connect, $connect);
$query_ticketType = "SELECT ticketType, price FROM ticketTypeTb";
$ticketType = mysql_query($query_ticketType, $connect) or die(mysql_error());
$row_ticketType = mysql_fetch_assoc($ticketType);
$totalRows_ticketType = mysql_num_rows($ticketType);


  
  ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body><form action=<?php print $PHP_SHELF; ?>>
<table width="860" border="0">
  <tr>
    <td width="250" bgcolor="#999999">2008 concerts</td>
    <td width="120" bgcolor="#999999">Number of tickets</td>
    <td width="130" bgcolor="#999999">Concert Venue</td>
    <td width="270" bgcolor="#999999">Type of ticket</td>
    <td bgcolor="#999999">Total price</td>
  </tr>
  <?php do {
  ########################################################################################################################
  // var "i" is counting howmany items in the form from DB
  ########################################################################################################################
  $i++
  ?>
    <tr>
      <td bgcolor="#CCCCCC"><?php echo $row_concertName['concertName']; ?></td>
      <?php
     ########################################################################################################################
     // This part is used to populate the number of tickets field with a value when from is submitted so value is not cleared
     ########################################################################################################################
      $popNumTickets = $_REQUEST["noTickets$i"]  ?>
       <!--########################################################################################################################
       var "i" is added to to imput name for each loop of table data td tag
       ########################################################################################################################-->
      <td align="center" bgcolor="#CCCCCC"><input name="noTickets<? echo $i ?>" type="text" id="noTickets" size="3" value="<? echo $popNumTickets ?>"  /></td>
      <td bgcolor="#CCCCCC">
      <!--[color=red]########################################################################################################################
       var "i" is added to to imput name for each loop of table data td tag
       ########################################################################################################################-->
      <select name="venue<? echo $i ?>" id="venue">
        <option value="0">------</option>
        <?php
do {  
?>
      <!--########################################################################################################################
       data from DB
       ########################################################################################################################-->
        <option value="<?php echo $row_venue['venueName'] ?>"><?php echo $row_venue['venueName']?></option>
          <?php
} while ($row_venue = mysql_fetch_assoc($venue));
  $rows = mysql_num_rows($venue);
  if($rows > 0) {
      mysql_data_seek($venue, 0);
     $row_venue = mysql_fetch_assoc($venue);
  }
?>
      </select></td>
      <!--########################################################################################################################
       var "i" is added to to imput name for each loop of table data td tag
       ########################################################################################################################-->
      <td bgcolor="#CCCCCC"><select name="ticketType<? echo $i ?>" id="ticketType<? echo $i ?>">
          <option value="none">-----</option>
          <?php
       
do {  
?>
      <!--########################################################################################################################
       data from DB
       ########################################################################################################################-->
<option value="<?php echo $row_ticketType['price']?>"><?php echo $row_ticketType['ticketType']?></option>
          <?php
} while ($row_ticketType = mysql_fetch_assoc($ticketType));
  $rows = mysql_num_rows($ticketType);
  if($rows > 0) {
      mysql_data_seek($ticketType, 0);
     $row_ticketType = mysql_fetch_assoc($ticketType);
  }
?>
        </select>
      </td>
      <td bgcolor="#CCCCCC">
      <?
   
   $dynamicSelectTicketType[$i] = $_REQUEST["ticketType$i"]; 
   // math to calculate subtotal 
   $concertSubTotal[$i] = $_REQUEST["noTickets$i"] * $dynamicSelectTicketType[$i];
   // output subtotal per field to table td
   echo "$".$concertSubTotal[$i];
   
   $popvenue[$i] = $_REQUEST["venue$i"];
   // add all subtotals togerther
   $total += $concertSubTotal[$i]; 
    
     ?>[/color]
      </td>
    </tr>
    <?php } while ($row_concertName = mysql_fetch_assoc($concertName)); ?>
</table>
<table width="860" border="0">
  <tr>
    <td width="250" bgcolor="#CCCCCC">Season Tickets $156</td>
    <td width="120" bgcolor="#CCCCCC"> </td>
    <td width="130" bgcolor="#CCCCCC"> </td>
    <td width="270" bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
  </tr>
  <tr>
    <td width="246" height="9" bgcolor="#CCCCCC">C.D Vol 1 - $25</td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
  </tr>
  <tr>
    <td width="246" height="9" bgcolor="#CCCCCC">C.D Vol 2 - $25</td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
  </tr>
  <tr>
    <td width="246" height="9" bgcolor="#CCCCCC">C.D Vol 3 - $25</td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
  </tr>
  <tr>
    <td width="246" height="9" bgcolor="#CCCCCC">C.D only packaging - $3</td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
  </tr>
  <tr>
    <td width="246" height="9" bgcolor="#999999">Totals</td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <td bgcolor="#CCCCCC"> </td>
    <!-- output total from calculated subtotals-->
    <td bgcolor="#CCCCCC"><?php echo "$".$total ?></td>
  </tr>
</table>
<p>
  <label>
  <input type="submit" name="button" id="button" value="Update" />
  </label>
</p>
<p> </p>
</form>
</body>
</html>
<?php
mysql_free_result($concertName);

mysql_free_result($venue);

mysql_free_result($ticketType);
?>

 

EDIT:

 

Added code tags

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.