Log in

View Full Version : Help with dynamic drop down list/php



cpageone
09-05-2011, 09:45 AM
Hi, I have a dynamically populated list from a mysql database in dreamweaver cs3. I am trying to achieve having a user select an item in the list and retain its value when displayed on the items results page.

Currently, all results from the database are displayed when an item from the list is selected. I appreciate that this might be something simple, but I this is my first attempt at mysql/php and despite trawling online, I think because my knowledge is so limited, I don't understand where I'm going wrong!

Any help (in very basic terms please!) would be much appreciated.



<?php
mysql_select_db($database_AllNewspapersConn, $AllNewspapersConn);
$query_Recordset1 = "SELECT Newspapers.ID, Newspapers.Title FROM Newspapers";
$Recordset1 = mysql_query($query_Recordset1, $AllNewspapersConn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
$colname_DetailRS1 = $_GET['recordID'];
}
?>

FORM:
<form id="form1" name="form1" method="get" action="lasttryresults.php">
<label for="select"></label>
<select name="select" id="select">
<?php
do {
?>
<option value="<?php echo $row_Recordset1['ID']?>"><?php echo $row_Recordset1['Title']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select>
<label for="button"></label>
<input type="submit" name="button" id="button" value="Submit" />
</form>

Results page:
mysql_select_db($database_AllNewspapersConn, $AllNewspapersConn);
$query_Recordset1 = "SELECT Newspapers.ID, Newspapers.Title FROM Newspapers";
$Recordset1 = mysql_query($query_Recordset1, $AllNewspapersConn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
$colname_DetailRS1 = $_GET['recordID'];
}
?>

<!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" xmlns:spry="http://ns.adobe.com/spry">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link href="Newspapers.css" rel="stylesheet" type="text/css" />
<link href="SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />

<form id="form1" name="form1" method="get" action="lasttryresults.php">
<label for="select"></label>
<select name="select" id="select">
<?php
do {
?>
<option value="<?php echo $row_Recordset1['ID']?>"><?php echo $row_Recordset1['Title']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select>
<label for="button"></label>
<input type="submit" name="button" id="button" value="Submit" />
</form>
<?php

Butterfly
09-21-2011, 05:57 PM
This line:


<option value="<?php echo $row_Recordset1['ID']?>"><?php echo $row_Recordset1['Title']?></option>

should contain a check to see if $row_Recordset1['ID'] is the same as the ID field in your item's table.

Like this:


<option value="<?php echo $row_Recordset1['ID']?>" <?php if($row_Recordset1['ID'] == $yourItemID) echo 'checked="checked"';?>><?php echo $row_Recordset1['Title']?></option>

$yourItemID should be extracted from your item's table.

Good luck!