Here i am populating my dropdowns with database fields using php
my code follows,
while ($row = mysql_fetch_array($result)) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
after fetching i echo it to html
<select id="Series1" onchange="changeVal('Series1')">
<option value="">Please select</option>
<?php echo $series1 ?>
</select>
my problem is i have some null values in the database field, i don't want that to be inserted in the options field. my final result now look like this
please help me.
Try this
while ($row = mysql_fetch_array($result)) {
if($row['Series'] != '' || $row['Series'] != NULL) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
}
OR
In your sql query
SELECT * FROM your_table WHERE Series IS NOT NULL
You can try like this
while ($row = mysql_fetch_array($result)) {
if(isset($row['Series'])) {
$series1 .= "<option id='Series1' value='" . $row['Series'] ."'>" . $row['Series'] ." </option>";
}
}
Try this...
<select id="Series1" onchange="changeVal('Series1')">
<option value="">Please select</option>
<?php
while ($row = mysql_fetch_array($result))
{
if($row['Series'] != '' || $row['Series'] != NULL)
{
?>
<option value="<?php echo $row['Series']; ?>"><?php echo $row['Series']; ?></option>
</select>
<?php
}
}
?>
Related
I am trying to retain the value selected in a drop down menu. Everything is working, but I don't know how to show and retain the selected value. How can I do this?
I've got this working using another way:
<?php if($_POST['selClass'] == $row1['class']) echo 'selected="selected"' ?>
but this leads to other problems, i.e. a blank option in my drop down menu.
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
There is two option you can choose whichever you feel ease.
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($_POST['selClass'] == $row1['class']){
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
}else{
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
}
?>
OR
<?php
$selectedClass = $_POST['selClass'];
while ($row1 = mysqli_fetch_array($rs5)) { ?>
<option value="<?php echo $row1['çlass']?>" <?php if(selectedClass == $row1['class']) { echo "selected='selected'"; }?> ><?php echo $row1['class']?></option>
<?php } ?>
I want to make multiple dropdowns from data out of my mysql database. I want 4 dropdowns to be exact. This is what I have at this moment:
<?php
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie, waar, metwie, voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
?>
For instance, this:
mysql_connect('#', '#', '#');
mysql_select_db('test');
$sql = "SELECT wie FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
}
echo "</select>";
$sql = "SELECT waar FROM data";
$result = mysql_query($sql);
echo "<select name='test1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
}
echo "</select>";
$sql = "SELECT metwie FROM data";
$result = mysql_query($sql);
echo "<select name='test2'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
}
echo "</select>";
$sql = "SELECT voeruig FROM data";
$result = mysql_query($sql);
echo "<select name='test3'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
}
echo "</select>";
?>
As stated by HawasKaPujaari, avoid using mysql. Use mysqli. You could use a conditional switch statement like this:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT wie, waar, metwie, voeruig FROM data")) {
printf("Select returned %d rows.\n", $result->num_rows);
while ($row = mysql_fetch_array($result)) {
switch ($row) {
case "wie":
echo "<select name='wie'>";
echo "<option value='" . $row['wie'] . "'>" . $row['wie'] . "</option>";
echo "</select>";
break;
case "waar":
echo "<select name='waar'>";
echo "<option value='" . $row['waar'] . "'>" . $row['waar'] . "</option>";
echo "</select>";
break;
case "metwie":
echo "<select name='metwie'>";
echo "<option value='" . $row['metwie'] . "'>" . $row['metwie'] . "</option>";
echo "</select>";
break;
}
case "voeruig":
echo "<select name='voeruig'>";
echo "<option value='" . $row['voeruig'] . "'>" . $row['voeruig'] . "</option>";
echo "</select>";
break;
}
/* free result set */
$result->close();
}
?>
php and mysql are different softwares. what you are doing here is connecting php with mysql using mysql_*() functions
in your case what you get is an array in php. you can use this array for whatever you want. if you want to print array as is as for debugging use:
echo "<pre>";
print_r($row);
echo "</pre>";
From this you will get array structure
then you ca use different dropdowns for array elements
Note: mysql_*() is not safe. Use mysqli_* or PDO.
Try code below. hope it help you
<?php
$wie=array();
$waar =array();
$metwie =array();
$voeruig =array();
while ($row = mysql_fetch_array($result)){
$wie[]=$row["wie"];
$waar[]=$row["waar"];
$metwie[]=$row["metwie"];
$voeruig[]=$row["voeruig"];
}
?>
<select name="wie">
<?php
foreach($wie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?>
<php
}
?>
</option>
<select name="waar">
<?php
foreach($waar as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
<select name="metwie">
<?php
foreach($metwie as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?></option>
<php
}
?>
</select>
<select name="voeruig">
<?php
foreach($voeruig as $k=>$v)
{
?>
<option value="<?php echo $v?>"><?php echo $v;?> </option>
<php
}
?>
</select>
I have a php select populated and given a matching value by using php to loop through the results of a sql query.
$result = mysqli_query($con, "select * from course")
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
That gives me a drop down list populated with all course titles,
upon submission I can access the selected value using $_POST['CourseSelect'];
However the drop down (select) resets itself to the default value when the page reloads.
How can I keep that option selected using php?
I know that I can use the selected keyword inside of an a select option to make that option the default selected option.
for example the second option would be selected when loading the page:
<select>
<option>One</option>
<option selected>Two</option>
<option>Three</option>
</select>
You can make it simple as
while($row = mysqli_fetch_array($result))
{
$select = '';
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] == $row['Title'] ) $select = 'SELECTED';
echo "<option value='".$row['Title']."' ".$select.">" . $row['Title'] . "</option>";
}
you can use like below..
$result = mysqli_query($con, "select * from course");
$selected = "";
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $row['Title'] == $_REQUEST['CourseSelect'] ? "Selected" : "";
echo "<option value='" . $row['Title'] ." ' $selected>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
try this:
echo "<option value='0'> - Select Course - </option>";
while($row = mysqli_fetch_array($result))
{
$selected = $_POST['CourseSelect'] == $row['Title'] ? 'selected' : '';
echo "<option value='{$row['Title']}' {$selected}>{$row['Title']}</option>";
}
you can do like this
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != "0")
{
echo "<option >".$_POST['CourseSelect']."</option>";
}
else
{
echo "<option value='0'> - Select Course - </option>";
}
while($row = mysqli_fetch_array($result))
{
if( isset($_POST['CourseSelect']) && $_POST['CourseSelect'] != $row['Title'])
echo "<option value='" . $row['Title'] ." '>" . $row['Title'] . "</option>";
}
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
PHP Function
# select box
/*
Example:
Parameter 1:
$options[1] = 'Course 1';
$options[2] = 'Course 2';
$options[3] = 'Course 3';
Parameter 2:
$selectedOption = 2; The dropdown need to be selected
*/
function buildOptions($options, $selectedOption)
{
foreach ($options as $value => $text)
{
if ($value == $selectedOption)
{
$Return .='<option value="'.$value.'" selected="selected">'.stripslashes($text).'</option>';
}
else
{
$Return .='<option value="'.$value.'">'.stripslashes($text).'</option>';
}
}
return $Return;
}
Function Call
$result = mysqli_query($con, "select * from course");
while ($row = mysqli_fetch_array($result))
{
$UniqueId = $row['Title'];
$Value = $row['Title'];
$optionsArray[$UniqueId] = $Value; // Store the values into an array
}
$CourseSelect = isset($_POST['CourseSelect']) ? $_POST['CourseSelect'] : '0';
echo "<form action='' method='post'>";
echo "<select name='CourseSelect'>";
echo "<option value='0'> - Select Course - </option>";
echo buildOptions($optionsArray, $CourseSelect);
echo "</select>";
echo "<input name='SubmitCourse' type='submit'>";
echo "</form>";
Notes
You can use buildOptions() for all of your projects to display select box. I have been using this for years.
with this code I get dropdown menu to select:
<select name='select'>
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_array($sql)) {
<OPTION selected='{$kategorija}' VALUE='" . $result[0] . "'>" . $result[0] . "</OPTION>
}
</select></td>
This is how my table looks:
ID DropDownMenu xxx yyy zzz
How to set that dropdown menu selected value is the value connected to that ID.
In my case I always get last value from dropdown menu as selected one.
Try this: UPDATED
$out = '<select name="select">';
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_assoc($sql)) {
$out .= "<OPTION selected='" .$result['$kategorija']. "' VALUE='" .$result[0]. "'";
if ($result['$kategorija'] == 'something') {
$out .= "selected";
}
$out .= ">" .$result[0]. "</OPTION>";
}
$out .= "</select></td>";
echo $out;
first store the selected sport in database to a variable
eg:
$sport='cricket';//substitute with database fetch value
Sport: <select name="sport" >
<option value="All" >All</option>
<?php $a="select * FROM kategorija";
$d2=mysql_query($a);
while($d4=mysql_fetch_array($d2))
{?> <option value="<?php echo $d4['sport']; ?>"<?php if($sport==$d4['sport']){ echo "selected";} ?> > <?php echo $d4['sport']; ?> </option> <?php
}
?></select>
How can I make the filtered selection still appear after the request.
So if I have options 1,2,and 3. When I select 2 and data shows I still want 2 to display signifying that the data is filtered through option 2.
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";
You can do something like this :
$country = isset($_POST['Country']) ? $_POST['Country'] : '';
while($row = mysql_fetch_array($result))
{
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".(($row['Fund_Manager_Company_Code'] == $country) ? 'selected="selected"' : '').">". $row['Fund_Manager_Company_Name'] ."</option>";
}
What you need is to add selected attribute to option:
$Country = $_POST['Country'];
$sected = 'selected = "selected" ';
while($row = mysql_fetch_array($result))
{
echo " <option ".($row['Fund_Manager_Company_Code'] == $Country? $selected : '')."value='". $row['Fund_Manager_Company_Code'] ."'>". $row['Fund_Manager_Company_Name'] ."</option>";
}
The one that the value selected and posted then will be selected...
something like
echo" <option value='" . $row['Fund_Manager_Company_Code'] . "' " . ((isset($_POST['Country']) && $_POST['Country'] == $row['Fund_Manager_Company_Code'])
? 'selected="selected"' : '') . ">" . $row['Fund_Manager_Company_Name'] . "</option>";
After your submit, you need to catch the selection in your PHP code:
$selection = $_POST['Country'];
echo "<form name='country_list' method='POST' action='http://opben.com/colombia/familias-de-carteras' >";
echo "<select name='Country' tabindex='1' >";
while($row = mysql_fetch_array($result))
{
$selected = "";
if ($row['Fund_Manager_Company_Code'] == $selection) {
$selected = "selected";
}
echo " <option value='". $row['Fund_Manager_Company_Code'] ."' ".$selected.">". $row['Fund_Manager_Company_Name'] ."</option>";
}
echo "</select>";
echo "<input type='submit' value='Filter' />";
echo "</form>";