Jump to content

[SOLVED] Newbie - needs help populating multiple drop down boxes from mysql table


greencoin

Recommended Posts

Am having a problem with a dynamic, multiple drop down box query. If I run the code with only one select, it populates fine. If I run it with 2 selects then only the first drop down box populates - the second drop down box is empty. Am wondering if I need to put the query into an array and populate the boxes from there.

 

Each select represents a start and end date so when the user queries, it brings back table records entered in between those ranges. Thanks in advance ~Rich

 

......................................................................

$query = "SELECT date FROM `GC_Tracker`";  
$result = mysql_query($query)  
  
or die(mysql_error());   
?> 
Select<form method="POST" action="track_date_results.php">  
  <select size="1" name="min">  
<? 
while(list($name)= mysql_fetch_row($result))  
{  

?>  
<option value="<? echo $name; ?>"><?php echo "$name"; ?></option>  


<?php
}
?>
</select>
<select size="1" name="max">  
<? 
while(list($name)= mysql_fetch_row($result))  
{  

?>  
<option value="<? echo $name; ?>"><?php echo "$name"; ?></option>  


<?php
}
?>
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>  
</form> 

Try this:

 

<?php
$query = "SELECT date FROM `GC_Tracker`";  
$result = mysql_query($query)or die(mysql_error());  
  

?> 
Select<form method="POST" action="track_date_results.php">  
  <select size="1" name="min">  
<? 
while($row = mysql_fetch_row($result))  
{  

?>  
<option value="<?php echo $row['date']; ?>"><?php echo $row['date']; ?></option>  


<?php
}
?>
</select>
<select size="1" name="max">  
<?php
while($rows = mysql_fetch_row($result))  
{  

?>  
<option value="<? echo $rows['date']; ?>"><?php echo $rows['date']; ?></option>  


<?php
}
?>
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>  
</form> 

That is beacuse I did a small error I didn't see  :)

 

Try this:

<?php
$query = "SELECT date FROM `GC_Tracker`";  
$result = mysql_query($query)or die(mysql_error());  
  

?> 
Select<form method="POST" action="track_date_results.php">  
  <select size="1" name="min">  
<?php
while($row = mysql_fetch_array($result))  
{  

?>  
<option value="<?php echo $row['date']; ?>"><?php echo $row['date']; ?></option>  


<?php
}
?>
</select>
<select size="1" name="max">  
<?php
while($rows = mysql_fetch_array($result))  
{  

?>  
<option value="<? echo $rows['date']; ?>"><?php echo $rows['date']; ?></option>  


<?php
}
?>
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>  
</form> 

Sorry - still doesn't work... but I did get it to work with a double query... not preferred but I'll take it for now so I can move forward. here's the code;

$query = "SELECT date FROM `GC_Tracker`";  
$result = mysql_query($query) or die(mysql_error());   
$query2 = "SELECT date FROM `GC_Tracker`";  
$result2 = mysql_query($query2) or die(mysql_error());
?> 
Select<form method="POST" action="track_date_results.php">  
  <select size="1" name="min">  
<? 
while(list($row) = mysql_fetch_row($result))  
{  

?>  
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>  


<?php
}
?>
</select>
<select size="1" name="max">  
<?php
while(list($rows) = mysql_fetch_row($result2))  
{  

?>  
<option value="<? echo $rows; ?>"><?php echo $rows; ?></option>  


<?php
}
?>
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>  
</form>

 

I'm not gonna call this solved till someone can figure out a way to do it without a double hit to mysql... ~Rich

<?php
$query = 'SELECT date FROM GC_Tracker';  
$result = mysql_query($query) or die(mysql_error());
$select_options = array();
if ($result && mysql_num_rows($result)) while ($row = mysql_fetch_row($result)) $select_array[] = "\t<option value=\"$row[0]\">$row[0]</option>\n";
?> 
Select<form method="POST" action="track_date_results.php">  
<select size="1" name="min">
<?php echo implode('',$select_options); ?>
</select>
<select size="1" name="max">
<?php echo implode('',$select_options); ?>
</select>
<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2">  
</form>

The little guy: I put it after the first loop in the

                          <?php

                            reset($row) ;

                            }

                          ?>only the first box populates

 

Wildbug: no good - neither drop down box gets populated

 

I'm doing this on a Godaddy, Linux hosting plan using Mysql 4.1 and whatever limited flavor PHP they're using.

 

Thanks ~Rich

Oh, I made a typo.  "$select_array" -> "$select_options" on the fifth line.

 

<?php
$query = 'SELECT date FROM GC_Tracker';  
$result = mysql_query($query) or die(mysql_error());
$select_options = array();
if ($result && mysql_num_rows($result)) while ($row = mysql_fetch_row($result)) $select_options[] = "\t<option value=\"$row[0]\">$row[0]</option>\n";
?> 
Select<form method="POST" action="track_date_results.php">  
<select size="1" name="min">
<?php echo implode('',$select_options); ?>
</select>
<select size="1" name="max">
<?php echo implode('',$select_options); ?>
</select>
<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2">  
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.