Link to home
Start Free TrialLog in
Avatar of max7
max7

asked on

Dynamic Data for dropdown menus will not appear

Greetings Experts,

I am current going through the CosmoFarmer tutorial found in the CS3 Missing Manual Book by McFarland and I'm hitting a wall with the Edit Record tutorial (starts on page 877).  The part I'm having trouble with starts with number 17 at the bottom of page 879.

After creating three new recordsets (rsProduct, rsVendors, & rsCategories), it then says to use the Record Update Form Wizard.  I've completed this part of the tutorial now twice (I deleted my first attempt and started again) and in each instance when I test the finished edit page, neither the Vendor dropdown menu nor the Category dropdown menu displays any dynamic data when I try to update a product from the product page.  Both dropdown menus are blank.

I've checked all recordsets for this page and I checked the dynamic properties on each dropdown menu and as far as I can tell, everything looks fine and it should work.  Unfortunately it does not and I don't know where to go from here.
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Max,

Please post the code involved and we can get started.
Avatar of max7
max7

ASKER

I am at work right so I will post the cost when I arrive home at around 6pm PST, thanks.
Okay.

I'm out tonight until 10 or 11, but I will check in when I get home.
Avatar of max7

ASKER

As promised, here's the code generated by dreamweaver for the dynamic dropdown menus on my edit records page.  The problem again is that no value (i.e. the vendor or category for the particular record selected selected for editing) appears in these menus.

You should know that I was able to correctly create an add record page which also had dynamic dropdown menus for both vendors and categories and they worked, the only difference here that I can see between the two is that in the case of the add records page, the dropdown menu shows all vendors and all categories whereas for the edit page it is selecting only the vendor and category for one record chosen for editing.

Let me know if you need more information and thanks for the help.
For the Vendor dropdown menu:
 
<select name="vendorID">
              <?php
do {  
?><option value="<?php echo $row_rsVendors['vendorID']?>"<?php if (!(strcmp($row_rsVendors['vendorID'], $row_rsProduct['vendorID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsVendors['vendorName']?></option>
              <?php
} while ($row_rsVendors = mysql_fetch_assoc($rsVendors));
  $rows = mysql_num_rows($rsVendors);
  if($rows > 0) {
      mysql_data_seek($rsVendors, 0);
	  $row_rsVendors = mysql_fetch_assoc($rsVendors);
  }
?>
            </select>
 
For the Category dropdown menu:
 
<select name="categoryID">
              <?php
do {  
?><option value="<?php echo $row_rsCategories['categoryID']?>"<?php if (!(strcmp($row_rsCategories['categoryID'], htmlentities($row_rsProduct['categoryID'], ENT_COMPAT, 'UTF-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsCategories['categoryName']?></option>
              <?php
} while ($row_rsCategories = mysql_fetch_assoc($rsCategories));
  $rows = mysql_num_rows($rsCategories);
  if($rows > 0) {
      mysql_data_seek($rsCategories, 0);
	  $row_rsCategories = mysql_fetch_assoc($rsCategories);
  }
?>
            </select>

Open in new window

Please also post the recordset code. I think part of the problem may be up there.  
Avatar of max7

ASKER

Ok here is what I believe to be the recordset code for this page.  I created three separate recordsets for this page (rsProduct, rsVendors, & rsCategories) so please let me know if I didn't post all of it.
<?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;
}
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE products SET productName=%s, price=%s, `description`=%s, inventory=%s, vendorID=%s, categoryID=%s, image=%s, onSale=%s WHERE productID=%s",
                       GetSQLValueString($_POST['productName'], "text"),
                       GetSQLValueString($_POST['price'], "double"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['inventory'], "text"),
                       GetSQLValueString($_POST['vendorID'], "int"),
                       GetSQLValueString($_POST['categoryID'], "int"),
                       GetSQLValueString($_POST['image'], "text"),
                       GetSQLValueString($_POST['onSale'], "int"),
                       GetSQLValueString($_POST['productID'], "int"));
 
  mysql_select_db($database_connCosmo, $connCosmo);
  $Result1 = mysql_query($updateSQL, $connCosmo) or die(mysql_error());
 
  $updateGoTo = "../product.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
 
$colname_rsProduct = "-1";
if (isset($_GET['productID'])) {
  $colname_rsProduct = $_GET['productID'];
}
mysql_select_db($database_connCosmo, $connCosmo);
$query_rsProduct = sprintf("SELECT * FROM products WHERE productID = %s", GetSQLValueString($colname_rsProduct, "int"));
$rsProduct = mysql_query($query_rsProduct, $connCosmo) or die(mysql_error());
$row_rsProduct = mysql_fetch_assoc($rsProduct);
$totalRows_rsProduct = mysql_num_rows($rsProduct);
 
$colname_rsVendors = "-1";
if (isset($_GET['vendorID'])) {
  $colname_rsVendors = $_GET['vendorID'];
}
mysql_select_db($database_connCosmo, $connCosmo);
$query_rsVendors = sprintf("SELECT * FROM vendors WHERE vendorID = %s", GetSQLValueString($colname_rsVendors, "int"));
$rsVendors = mysql_query($query_rsVendors, $connCosmo) or die(mysql_error());
$row_rsVendors = mysql_fetch_assoc($rsVendors);
$totalRows_rsVendors = mysql_num_rows($rsVendors);
 
$colname_rsCategories = "-1";
if (isset($_GET['categoryID'])) {
  $colname_rsCategories = $_GET['categoryID'];
}
mysql_select_db($database_connCosmo, $connCosmo);
$query_rsCategories = sprintf("SELECT * FROM categories WHERE categoryID = %s", GetSQLValueString($colname_rsCategories, "int"));
$rsCategories = mysql_query($query_rsCategories, $connCosmo) or die(mysql_error());
$row_rsCategories = mysql_fetch_assoc($rsCategories);
$totalRows_rsCategories = mysql_num_rows($rsCategories);
?>

Open in new window

Okay, so from that post above, both vendorID and categoryID must be present in the query string (URL) when you access the page, otherwise no records will return for one or the other.

So your URL must look like:

http://somesite.com/somepage.php?vendorID=X&categoryID=Y

Does it?
Avatar of max7

ASKER

My URL looks like this:

http://localhost/cosmo_shop/admin/edit.php?productID=19

Based on your example, I can't help but wonder if I'm not communicating effectively the type of page I'm building.  The edit records page is an entire form that contains information on one product from my sample db; the vendor and category dropdown menus are but two fields of information out of ten total available to alter (combination of text fields, textarea, radio buttons, etc.).  

Considering this, do you still think my URL should look as you suggested?

I think I understand what you want.  Does the following make sense?

1) You want to pull out a record from a table via the productID.

2) The values in that record filter out at least two other recordsets which are then used to populate your drops downs in the form.

This is what I get from your statement plus how you created the code above.  If the above is correct, then you do need to reformat the URL the way I have showed...so with the third condition applied it will work like this:

http://somesite.com/somepage.php?vendorID=X&categoryID=Y&proudctID=19

That makes $_GET variables available to the code as written.

If the pull down lists are supposed to be populated from tables without any filters applied at all, then you did build the recordsets the wrong way...you added a filter condition (URL Parameter)

To make simple dynamic drop down lists, just create recordsets without any filters.   That will return all records in the table and populate the drop downs with them.  You then use the data binding option in the Properties Panel to set the initially selected option to match the column in the Product recordset that contains matching data.
Avatar of max7

ASKER

Hi Jason,

My apologies for not replying any sooner than now -- my cable internet at home has been down since 3pm yesterday (still down even now) so I was unable to work on this.  I'm at work now but I'm going to read over your response and I'll be posting again in the next few hours as time permits.
cable sucks.  I used to go down every week with it. Finally switched to DSL and satellite tv
Avatar of max7

ASKER

I've actually considered that!  I have to check into pricing . . .
Avatar of max7

ASKER

Disclaimer: if I seem to be repeating things, it's only because I want to make sure I'm understanding your posts as well as making myself clear to get the right answers.

>>>To make simple dynamic drop down lists, just create recordsets without any filters.   That will return all records in the table and populate the drop downs with them.

I believe I did this correctly when I created an add records page.  Both the vendor and category menus are populated as they should be.

>>>I think I understand what you want.  Does the following make sense?

1) You want to pull out a record from a table via the productID.

2) The values in that record filter out at least two other recordsets which are then used to populate your drops downs in the form.

This is what I get from your statement plus how you created the code above.  If the above is correct, then you do need to reformat the URL the way I have showed...so with the third condition applied it will work like this:

http://somesite.com/somepage.php?vendorID=X&categoryID=Y&proudctID=19

I think this is correct; the tutorial I'm following wants me to create an edit record page so that a user will be able to change data on any particular product in the db.  When viewing a product detail page, they can select a link that says "edit this information".  When they do that, the edit record page appears.  When it first appears the Vendor and Categories dropdown menus should default  to the current values for that particular product -- then of course they can select a new vendor or category from the resepctive dropdown menus.

The next question is: how to correct this?  What I would really like is to determine where I messed up using the Record Update Form Wizard (unless I messed up elsewhere?).  What gets me is that I believe I did this tutorial correctly so part of me wonders if there isnt an error in the book itself  

To that end, I've attached 4 pages from the book I'm using for your review so that you can see firsthand what it says.  With your amount of experience I'm sure you'll be able to spot where I went wrong pretty quickly.

Keep in mind that while I'm not adverse to working with the code directly, I'm about as new as they get with PHP; my long term goal is to learn PHP but my immediate goal is to learn the dreamweaver system.

Thanks again for your time.





page1.jpg
page2.jpg
page3.jpg
page4.jpg
Okay, you misinterpreted the stuff on page 2 of the tutorial and I think this statement of mine from above applies:

"If the pull down lists are supposed to be populated from tables without any filters applied at all, then you did build the recordsets the wrong way...you added a filter condition (URL Parameter)"

These instructions are meant for you to create one recordset that is filtered (rsProduct) and two that are not (rsVendors, rsCategories)

You created three recordsets that are all filtered.  Products is fine, so leave it alone.

For rsVendors and rsCategories, double click them to open the Recordset Wizard and set the filter to None.  Publish the page and test and you should see the pull downs working correctly.
Avatar of max7

ASKER

>>>Okay, you misinterpreted the stuff on page 2 of the tutorial and I think this statement of mine from above applies:

"If the pull down lists are supposed to be populated from tables without any filters applied at all, then you did build the recordsets the wrong way...you added a filter condition (URL Parameter)"

These instructions are meant for you to create one recordset that is filtered (rsProduct) and two that are not (rsVendors, rsCategories)

Ok you were right; I made the corrections as you suggested and it's working as it should now.
I think I understand where I made my mistake but I want to cofirm this: reading on page 1, it clearly says rsProducts needs to be filtered, so no problem there.

When I read the instructions on page 2 to create rsVendors & rsCategories I don't see any explicit instruction to leave these last two recordsets unfiltered --however--  is this implied in the sentence "You'll create two more recordsets . . . listing all vendors and a listing of product categories" i.e. they can't be filtered if they are listing all vendors and all categories. Did I get that right?



ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of max7

ASKER

Yes that does make sense and I hope to tackle those more advanced techiques one day soon.  For now I will celebrate getting my hands around this issue -- thanks very much for your excellent guidance!