Jump to content

Select Drop Down Box using php


aeafisme23

Recommended Posts

My code below is pulling from a database and it's just listing the elements as a loop right now, below my code i will paste my sad attempt at putting it into a <select><option></select> but i dont know how to correctly put the data in there any suggestions?

							<?php
						//Setup connection to the database 
					    $connect = mysql_pconnect("localhost", "user", "pw") 
					    or die(mysql_error()); 

					    //Connect to the database 
						mysql_select_db("x_x", $connect) or die(mysql_error());    

					    // Perform an SQL query on the Address table    
						$sql_address = mysql_query("SELECT dma_id, station_id from station_list order by dma_id") 

					    or die (mysql_error()); 
						   
						// Define the colours for the alternating rows 
						$colour_odd = "#FFFFFF"; 
						$colour_even = "#FFFFFF"; $row_count = 0;  //To keep track of row number 

					    // Fetch the array of records and Loop through them 
						while($results = mysql_fetch_array($sql_address)){ 
						// Decide which colours to alternate for the rows If Remainder of $row_count divided by 2 == 0. 
						$row_color = (($row_count % 2) == 0) ? $colour_even : $colour_odd; 

						//Echo the table row and table data that needs to be looped Check All With selected: over until the end of the recordset     
						echo ' 
						 <tr bgcolor="' . $row_color . '"> 
						  <td width=250 valign=top><p class=\"small\">' .$results['dma_id'] . ' <a href=view_records.php?station=' .$results['station_id'] . '>' .$results['station_id'] . '</a><br></p></td> 				  
						  </tr>'; 

					   // Increment the row count 
						 $row_count++; } 
					   // Free the MySQL resource 
					   mysql_free_result($sql_address); 
					?>

 

My attempt at making it a drop down box. I am pretty sure im off base on most of it but just looking for some insight :)

	<?php
//Setup connection to the database 
$connect = mysql_pconnect("localhost", "x", "x") 
or die(mysql_error()); 

//Connect to the database 
mysql_select_db("x", $connect) or die(mysql_error());    

// Perform an SQL query on the Address table    
$sql_address = mysql_query("SELECT dma_id, station_id from station_list order by dma_id") 

or die (mysql_error()); 

// Fetch the array of records and Loop through them 
while($results = mysql_fetch_array($sql_address)){ 
// Decide which colours to alternate for the rows If Remainder of $row_count divided by 2 == 0. 

//Echo the table row and table data that needs to be looped Check All With selected: over until the end of the recordset     
echo ' 
 <tr bgcolor="' . $row_color . '"> 
  <td width=250 valign=top>
  <p class=\"small\">
<form><select name="dmalist">
	<option value=''>- Select -</option>
	<option  value="view_records.php?station=' .$results['station_id'] . '> formSelected( $HTTP_POST_VARS[ "dma_id" ], [ "station_id" ] ); </option>
	</select></form>
	</p>
  </td> 				  
  </tr>'; 

   // Increment the row count 
 $row_count++; } 
   // Free the MySQL resource 
   mysql_free_result($sql_address); 
?>

Link to comment
https://forums.phpfreaks.com/topic/92378-select-drop-down-box-using-php/
Share on other sites

You're not off by much...you don't want to loop through and recreate a new <select> box with every result (at least I hope not)...so you really only want to loop through the <option> part of the code...example

 


//YOUR EARLIER CODE HERE

<tr bgcolor="' . $row_color . '"> 
  <td width=250 valign=top>
  <p class=\"small\">
<form><select name="dmalist">
	<option value=''>- Select -</option>

<?php
while($results = mysql_fetch_array($sql_address)){ 	 
?>
	<option  value="view_records.php?station=' .$results['station_id'] . '> formSelected( $HTTP_POST_VARS[ "dma_id" ], [ "station_id" ] ); </option>
<?php
}
?>
</select></form>
	</p>
  </td> 				  
  </tr>';

//REST OF YOUR CODE HERE 

 

...hmmm...o.k...that was really dirty code alteration I posted...changed it...gah...teach me to post really fast in reply...

Okay my attempt at this did not return an error but it did however return absolutely nothing but a select box that was listed as Select but no database results were listed, i guarantee my problem is this line of code

<option  value="view_records.php?station=' .$results['station_id'] . '> formSelected( $HTTP_POST_VARS[ "dma_id" ], [ "station_id" ] ); </option>

but i could be wrong, take a look at my code that i did below. Thanks in advance! :)

 

	<?php
//Setup connection to the database 
$connect = mysql_pconnect("localhost", "x", "x") 
or die(mysql_error()); 

//Connect to the database 
mysql_select_db("x", $connect) or die(mysql_error());    

// Perform an SQL query on the Address table    
$sql_address = mysql_query("SELECT dma_id, station_id from station_list order by dma_id") 

or die (mysql_error()); 
?>
<table>
<tr bgcolor="' . $row_color . '"> 
  <td width=250 valign=top>
  <p class="small\">
<form><select name="dmalist">
	<option value=''>- Select -</option>

<?php
while($results = mysql_fetch_array($sql_address)){ 	 
?>
	<option  value="view_records.php?station=' .$results['station_id'] . '> formSelected( $HTTP_POST_VARS[ "dma_id" ], [ "station_id" ] ); </option>

<?php	 
   // Increment the row count 
 $row_count++; } 
   // Free the MySQL resource 
   mysql_free_result($sql_address); 
?>


</select></form>
	</p>
  </td> 				  
  </tr></table>

I also tried this approach with no avail, this approach did not return anything and returned php syntax errors. I think this approach may be better than the last but things are starting to mesh so much together that im actually confusing myself. Any clarity would be nice :)

 

3rd attempt:

<table>
<tr> 
  <td width=250 valign=top>
 	 <p class="small">
			<form><select name="dmalist">
				<option value=''>- Select -</option>
					<?php
					while($results = mysql_fetch_array($sql_address)){ 	 

						<option value=view_records.php?station=' .$results[`station_id`] . '>dma: ' .$results['dma_id'] . ' station: ' .$results['station_id'] . '</option> 

					   // Increment the row count 
						 $row_count++; } 
					   // Free the MySQL resource 
					   mysql_free_result($sql_address); 
					?>
				</select>
			</form></p>
  </td> 				  
</tr>
</table>

<?php
// open connection
$connection1 = mysql_connect($server, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

//this code is bringing in the values for dropdown number 1.
$query="SELECT * FROM " . $pre . "country ORDER BY country";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
$result = mysql_query ($query);
echo "<select name=\"bsc_country\" value='1'>Country Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"$nt[countryid]\">$nt[country]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

// close connection
mysql_close($connection1);
?>

 

The above is what I use for a drop down for countries.  It pulls from a mysql database.  If this is what you are looking for.

<form>
<table>
    <tr>
        <td width=250 valign=top>
        <p class="small">
            <select name="dmalist">
                <option value=''>- Select -</option>
<?php

while($results = mysql_fetch_array($sql_address)){
    $stationID = $results['station_id'];
    $dmaID = $results['dma_id'];

    echo"<option value='view_records.php?station=$stationID'>dma: $dmaID station: $stationID</option>";

    // Increment the row count
    $row_count++;
}

// Free the MySQL resource
mysql_free_result($sql_address);
?>

            </select>
        </p>
        </td>
    </tr>
</table>
</form>

Try using this modified code. I think it will do what you need.

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.