Dynamic select list with more than one value?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
9600
Forum Newbie
Posts: 1
Joined: Thu Apr 14, 2011 1:40 pm

Dynamic select list with more than one value?

Post by 9600 »

I am creating a simple CRUD component for our company's Joomla site. This component allows our users to create discount bundles for our cart according to shopper group and products. I've been using Dreamweaver to author the administrator forms and have run into a bit of a problem. All of my forms work properly, but I need to be able to collect two values from each chosen multi-select listing.

Right now, when a user chooses an item under 'products', the site pushes back product_id, one of the variables I need. I would also like product_parent_id to return with each result as well. I need to be able to insert product_parent_id after the user has chosen their desired products. product_parent_id is located in the same table as product_id.

In the end, I need those two values to be pushed to another table, essentially creating a new entry for each product in the bundle. Referenced by ID and parent ID.

Pasted below is the administrator form code. Any suggestions would be wonderful.

Thanks!

Code: Select all

<?php require_once('Connections/SQLTest.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $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;
}
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO jos_vm_related_products (product_id, related_product_id) VALUES (%s, %s)",
                       GetSQLValueString($_POST['products'], "int"),
                       GetSQLValueString($_POST['products'], "int"));
 
  mysql_select_db($database_SQLTest, $SQLTest);
  $Result1 = mysql_query($insertSQL, $SQLTest) or die(mysql_error());
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO jos_vm_relations (discount_type, discount, shopper_group_id) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['amount_off_total'], "text"),
                       GetSQLValueString($_POST['dollar_amount'], "int"),
                       GetSQLValueString($_POST['shoper_group'], "int"));
 
  mysql_select_db($database_SQLTest, $SQLTest);
  $Result1 = mysql_query($insertSQL, $SQLTest) or die(mysql_error());
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO jos_vm_relations_messages (discount_message) VALUES (%s)",
                       GetSQLValueString($_POST['discount_message'], "text"));
 
  mysql_select_db($database_SQLTest, $SQLTest);
  $Result1 = mysql_query($insertSQL, $SQLTest) or die(mysql_error());
}
 
$maxRows_Products = 10;
$pageNum_Products = 0;
if (isset($_GET['pageNum_Products'])) {
  $pageNum_Products = $_GET['pageNum_Products'];
}
$startRow_Products = $pageNum_Products * $maxRows_Products;
 
mysql_select_db($database_SQLTest, $SQLTest);
$query_Products = "SELECT * FROM jos_vm_product";
$query_limit_Products = sprintf("%s LIMIT %d, %d", $query_Products, $startRow_Products, $maxRows_Products);
$Products = mysql_query($query_limit_Products, $SQLTest) or die(mysql_error());
$row_Products = mysql_fetch_assoc($Products);
 
if (isset($_GET['totalRows_Products'])) {
  $totalRows_Products = $_GET['totalRows_Products'];
} else {
  $all_Products = mysql_query($query_Products);
  $totalRows_Products = mysql_num_rows($all_Products);
}
$totalPages_Products = ceil($totalRows_Products/$maxRows_Products)-1;
 
mysql_select_db($database_SQLTest, $SQLTest);
$query_shopper_group = "SELECT * FROM jos_vm_shopper_group";
$shopper_group = mysql_query($query_shopper_group, $SQLTest) or die(mysql_error());
$row_shopper_group = mysql_fetch_assoc($shopper_group);
$totalRows_shopper_group = mysql_num_rows($shopper_group);
?>
<!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 id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <p>
    <label for="products"></label>
  Choose the product(s) you would like to add to your new discount bundle.</p>
  <p>
    <select name="products" size="1" multiple="MULTIPLE" id="products">
      <option value=""></option>
      <?php
do { 
?>
      <option value="<?php echo $row_Products['product_id']?>"><?php echo $row_Products['product_name']?></option>
      <?php
} while ($row_Products = mysql_fetch_assoc($Products));
  $rows = mysql_num_rows($Products);
  if($rows > 0) {
      mysql_data_seek($Products, 0);
  $row_Products = mysql_fetch_assoc($Products);
  }
?>
    </select>
  </p>
<p>
  <input type="hidden" name="hiddenField" id="hiddenField" />
  </p>
  <p>Choose the subscriber group that you would like this discount to apply to.</p>
  <p>
    <select name="shoper_group">
      <?php
do { 
?>
      <option value="<?php echo $row_shopper_group['shopper_group_id']?>"><?php echo $row_shopper_group['shopper_group_name']?></option>
      <?php
} while ($row_shopper_group = mysql_fetch_assoc($shopper_group));
  $rows = mysql_num_rows($shopper_group);
  if($rows > 0) {
      mysql_data_seek($shopper_group, 0);
  $row_shopper_group = mysql_fetch_assoc($shopper_group);
  }
?>
    </select>
  </p>
  <p>Please enter the dollar amount that you would like to adjust.
    <label for="dollar_amount"><br />
    </label>
    <input type="text" name="dollar_amount" id="dollar_amount" />
  </p>
  <p>
    <input name="amount_off_total" type="hidden" id="amount_off_total" value="amount_off_total" />
  </p>
  <p>Enter any message you would like the shopper to view during shopping and checkout when this discount is applied.</p>
  <p>
    <label for="discount_message"></label>
    <textarea name="discount_message" id="discount_message" cols="45" rows="5"></textarea>
  </p>
  <p>
    <input type="hidden" name="MM_insert" value="form1" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>
<br />
</body>
</html>
<?php
mysql_free_result($Products);
 
mysql_free_result($shopper_group);
?>
Post Reply