Hi All,

I am very new to PHP and still learning lots... :)
I am using the below code to populate a drop down list from my database.
But I am not sure how to display/send the results back to the same page.

The fields in my Database are:
Base, Owner, Cluster, Sector, Coordinates, Defenses, Notes

So based on the code below - when someone selects their Cluster I would like to display all records from database that are from that cluster. Any help is greatly appreciated.
Here is code I have:

<?php

//Populates drop down choice from database field

//Database connection string is here.

$query = "select DISTINCT Cluster FROM Starbase";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
echo '<option value="">Please Select..</option>';
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->record_id\">$row->Cluster</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
Member Avatar for diafol
<?php
$selectedItem = 0; //default
$select = '<select name="selectItem"><option value="">Please Select..</option>';
$output = "";

if(isset($_POST['submit']) && isset($_POST['selectItem'])){
	$selectedItem = intval($_POST['selectItem']);
	$results = mysql_query("SELECT ...fields you need... FROM `Starbase` WHERE `Cluster`=''")or die("Error performing query");
	if(mysql_num_rows($results) > 0){
		$output .= "<ul>";
		while($row = mysql_fetch_array($results)){
			$output .= "<li>{$row['field1']} - {$row['field1']}</li>";	
		}
		$output .= "</ul>";
	}else{
		$output = "<p>No records for this Cluster, which is very strange as it was chosen from the Database</p>";	
	}
}

//I'll keep the fetch_object here
$query = "SELECT `record_id`, `Cluster` FROM `Starbase` ORDER BY Cluster";
$results = mysql_query($query, $link) or die("Error performing query");
if(mysql_num_rows($results) > 0){
	while($row = mysql_fetch_object($results)){
		$sel = ($selectedItem == $row->record_id) ? ' selected="selected"' : '';
		$select .= "<option value=\"{$row->record_id}\"$sel>{$row->Cluster}</option>";
	}
}
$select .= "</select>";
?>

<form method="post">
	<?php echo $select;?>
	<input type="submit" name="submit" value="Submit Me" />
</form>

<?php echo $output;?>

Why are you using SELECT DISTINCT on the Cluster? I hope you haven't been inputting this field individually every time. If so, use a relational model to maintain the data easily. OK, this means using JOINS in your SQL, but that's a lot better than duplicating data.

Thank you for the help - I must still be doing something wrong as I am receiving the following error:

Parse error: syntax error, unexpected T_STRING, expecting ']' in /home/a5063282/public_html/test/test.php on line 20

Would you mind reviewing for my mistake?

<?php
//Database connection string is here

   $selectedItem = 0; //default
    $select = '<select name="selectItem"><option value="">Please Select..</option>';
    $output = "";
     
    if(isset($_POST['submit']) && isset($_POST['selectItem'])){
    $selectedItem = intval($_POST['selectItem']);
    $results = mysql_query("SELECT * FROM `Starbase` WHERE `Cluster`=''")or die("Error performing query");
    if(mysql_num_rows($results) > 0){
    $output .= "<ul>";
    while($row = mysql_fetch_array($results)){
    $output .= "<li>{$row['Base']} - {$row['Owner']} - {$row['Cluster]} - {$row['Sector']} - {$row['Coordinates']} - {$row['Defenses']} - {$row['Notes']}</li>";
    }
    $output .= "</ul>";
    }else{
    $output = "<p>No records for this Cluster, which is very strange as it was chosen from the Database</p>";
    }
    }
     
    //I'll keep the fetch_object here
    // I am NOT SURE whether to leave this or take out?

    $query = "SELECT `record_id`, `Cluster` FROM `Starbase` ORDER BY Cluster";
    $results = mysql_query($query, $link) or die("Error performing query");
    if(mysql_num_rows($results) > 0){
    while($row = mysql_fetch_object($results)){
    $sel = ($selectedItem == $row->record_id) ? ' selected="selected"' : '';
    $select .= "<option value=\"{$row->record_id}\"$sel>{$row->Cluster}</option>";
    }
    }
    $select .= "</select>";
    ?>
     
    <form method="post">
    <?php echo $select;?>
    <input type="submit" name="submit" value="Submit Me" />
    </form>
     
    <?php echo $output;?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.