View Full Version : displaying data from database into dropdown menus

08-30-2006, 02:39 PM
can somebody please have a look at this code for me please. I'm trying to display data from a bus and train table in the database. When a user selects a source from the the fordt drop down menu, the destainations available from this source should be displayed in the second drop down and the same in the last drop down. The purpose of this is to allow for connecting journeys...if the user needs to take 2 buses/trains etc to get to the final destination. I am using 2 pages for this...the first one called bustrain. html. The code for this is below:

<table width="100%" border="0" align="right"><!--p
if (isset($message_new))
echo "<tr><td colspan='2'>$message_new</td></tr>";
<td align="right"><font color="#000080">destination:</font></td>
<td><select name="source">
<option value="Select Departure" selected="selected">Select Departure</option>
<option value="Waterford">Waterford</option>
<option value="Carlow">Carlow</option>
<option value="Cavan">Cavan</option>
<option value="Clare">Clare</option>
<option value="Cork">Cork</option>
<option value="Donegal">Donegal</option>
<option value="Dublin">Dublin</option>
<option value="Galway">Galway</option>
<option value="Kerry">Kerry</option>
<option value="Kildare">Kildare</option>
<option value="Kilkenny">Kilkenny</option>
<option value="Laois">Laois</option>
<option value="Leitrim">Leitrim</option>
<option value="Limerick">Limerick</option>
<option value="Longford">Longford</option>
<option value="Louth">Louth</option>
<option value="Mayo">Mayo</option>
<option value="Meath">Meath</option>
<option value="Monaghan">Monaghan</option>
<option value="Offaly">Offaly</option>
<option value="Roscommon">Roscommon</option>
<option value="Sligo">Sligo</option>
<option value="Tipperary">Tipperary</option>
<option value="Westmeath">Westmeath</option>
<option value="Wexford">Wexford</option>
<option value="Wicklow">Wicklow</option>
<option value="">None of the Above</option></select></td></tr>

include "db.php";

<select name="source" id="source">
$SQL = mysql_query("SELECT destination FROM bus WHERE source = ’$source’ UNION SELECT destination FROM train WHERE source = ’$source’);
while ( $row = mysql_fetch_object($SQL) ):
<option value="<? echo $row->id?>"><? echo $row->destination?></option>
<? endwhile?>

<td align="right"><font color="#000080">Travelling:</font></td>
<td><select name="travelday">
<option value="Select" selected="selected">Select Day</option>
<option value="weekday">Weekday</option>
<option value="weekend">Weekend</option></select></td></tr>
<input type="submit" value="search" name="submit">
<input type="reset" value="Reset" name="reset"> </td></tr></tbody></table></p></form>
<img height="44" alt="" src="hr_bot.gif" width="680" border="0"> </p></center>

<a href="contactus.html" target="mainFrame">Contact US | [/url]
<a href="bus.html" target="mainFrame">Bus Search Search | [/url]
<a href="train.html" target="mainFrame">Train Search | [/url]
<a href="timetable.html" target="mainFrame">Timetables[/url]


The code for the PHP page is below:

include "db.php";


$source =$HTTP_POST_VARS["source"];

$query1 = "SELECT destination FROM bus WHERE source = "$source"
SELECT destination FROM train WHERE source = "$source";"

$query2 = "SELECT destination FROM bus WHERE source = "$destination"
SELECT destination FROM train WHERE source = "Wexford";"

//$query1 = "SELECT * FROM bus where bus.source = "$source" and bus.destination = "$destination"";

//$query1 = "SELECT * FROM train where train.source = "$source" and train.destination = "$destination"";

$result = @mysql_query($query1,$query2, $conn);
<table align="center" cellspacing="0" cellpadding="5" bgcolor="white" border=0 bordercolor="#0099CC" font="white">

<td align="left" bgcolor="#0099CC"><center><font color="white">Source</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Destination</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Departure Time</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Arrival Time</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Travel Day</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Operator</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Price</td>

<td align="left" bgcolor="#0099CC"><center><font color="white">Purchase</td></tr>';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
<td align="center">'.$row['source'].'</td>
<td align="center">'.$row['destination'].'</td>
<td align="center">'.$row['dept_time'].'</td>
<td align="center">'.$row['arr_time'].'</td>
<td align="center">'.$row['travelday'].'</td>
<td align="center">'.$row['operator_name'].'</td>
<td align="center">'.$row['price'].'</td></tr>';



echo'<h1> System Error </h1> table ';


I'm not sure whether i should be using 2 pages or have everything in a single .php file instead. Should the SQL statements to populate the drop down menus be placed in the html page or the php page??

Please Help :confused:

08-30-2006, 03:39 PM
dont understand really, but is this what your looking for: