If in the browser I have parameters like:
http://localhost/specials.php?year=2009&make=honda&model=civic
and the dropdown looks something like this:
<select name="year">
<?php
$query = mysql_query("select distinct year from tbl_content where year = '$year'");
while($row = mysql_fetch_assoc($query)) {
echo "<option value=\"{$row['year']}\">{$row['year']}</option>";
}
?>
</select>
Now what I'm trying to do is show select when the dropdown options value is equal to the parameter year in the browser URL.
I tried this:
<select name="year">
<?php
$query = mysql_query("select * from tbl_year
while($row = mysql_fetch_assoc($query)) {
#=============================
if(isset($_GET['year'])) {
$year = (int)$_GET['year'];
if($year == $row['year'] { $selected = "selected"; }
else { $selected = "";
}
echo "<option value=\"{$row['year']}\" {$selected}>{$row['year']}</option>";
}
?>
</select>
Maybe try "selected='selected'" to make it valid xml.
<select name="year">
<?php
$selectedYear = NULL;
if(isset($_GET['year']))
$selectedYear = (int)$_GET['year'];
$query = mysql_query('SELECT year FROM tbl_year GROUP BY year ORDER BY year ASC');
while($row = mysql_fetch_assoc($query)) {
echo '<option value="' . htmlspecialchars($row['year']) . '"';
if($selectedYear === (int)$row['year']) {
echo ' selected="selected"';
}
echo '>' . htmlspecialchars($row['year']) . '</option>";
}
?>
</select>
Fell free to separate functionality!
<?php
function selectList($name,$values,$labels=null,$selected=null){
if($labels==null) $labels=&$values;
$data="<select name='$name'>";
foreach($values as $k=>$v){
$selected=($v==$selected)?'selected="selected"':false;
$data.="<option value='$v' $selected>".htmlspecialchars($labels[$k])."</option>";
}
$data.="</select>";
return $data;
}
$select=isset($_REQUEST['year'])?(int) $_REQUEST['year']:null;
$query=mysql_query("SELECT DISTINCT `year` FROM `tbl_year` ORDER BY `year`");
while(($row=mysql_fetch_assoc($query))!==false){
$values[]=$row['year'];
}
echo selectList("year",$values,null,$select);
?>
Related
Is it possible to while loop an entire select tag to have multiple dropdown menus?
What I am trying to achieve is to have a column full of dropdown menus in a table.
This is what I have tried so far
<?php
$DNS_FROM = $DNS."_port-%";
$select = "SELECT * FROM `uplink_port_mapping` WHERE DNS_From LIKE '$DNS_FROM'";
$select1 = mysqli_query($conn, $select);
$select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
$select3 = mysqli_query($conn, $select2);
while($uplink_from = mysqli_fetch_assoc($select1)){
echo "<tr>";
echo "<td>".$uplink_from['DNS_From']."</td>";
echo "<td>"."<select name = 'uplink_to' multiple='multiple'>
<option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";
while ($uplink_to = mysqli_fetch_assoc($select3)){
echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
}
echo"</select>";
echo"</td>";
echo"</tr>";
}
?>
How it is right now.
The reason for the second while loop only working once is, that mysqli_fetch_assoc($result) will leave the pointer of the $result recource at it's end.
So when you try to loop the second time, mysqli_fetch_assoc($result) will not return anything (cause it's at the end of the $result recource.
Two possibilites:
Reset the pointer to the beginning:
<?php
....
while($uplink_from = mysqli_fetch_assoc($select1)){
echo "<tr>";
echo "<td>".$uplink_from['DNS_From']."</td>";
echo "<td>"."<select name = 'uplink_to' multiple='multiple'>
<option value = '".$uplink_from['DNS_To']."' selected='selected'>". $uplink_from['DNS_To']."</option>";
// here's the change:
mysql_data_seek($select3, 0);
while ($uplink_to = mysqli_fetch_assoc($select3)){
echo "<option value='".$uplink_to['DNS_From']."'>".$uplink_to['DNS_From']."</option>";
}
echo"</select>";
echo"</td>";
echo"</tr>";
}
....
?>
Or - which I think is the better solution - store that data into an array first, and then walk through that array:
<?php
...
$select2 = "SELECT DNS_From FROM `uplink_port_mapping` WHERE DNS_From NOT LIKE '$DNS_FROM' AND DNS_To = ''";
$result2 = mysqli_query($conn, $select2);
$dns_from = Array();
while ($uplink_to = mysqli_fetch_assoc($select3)){
$dns_from[] = $uplink_to;
}
....
// inside your first while loop:
foreach($dns_from as $dns) {
echo "<option value='".$dns['DNS_From']."'>".$dns['DNS_From']."</option>";
}
....
// note that I left out a bunch of your code, that doesn't change.
?>
Multiple select dropdown basic examples
$array = [
'Apple' => 1,
'Orange' => 0,
'Banana' => 1,
];
echo '<select multiple>';
foreach ($array as $fruit => $sel) {
$selected = 0;
if ($sel == 1) {
$selected = 'selected';
}
echo '<option value="' . $fruit . ' " ' . $selected . '>' . $fruit . '</option>';
}
echo '</select>';
I am struggling to get this code to work, what I want from it is to show the item (that is already in the database) to be selected in the selection form.
<label>Server Ports:</label>
<select multiple class="form-control" name="select[]">
<?php
// Get Server Information
$query = "SELECT port_no FROM _servers WHERE (server_id = '$servid') ";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$no = $row['port_no'];
}
$query = "SELECT id, name, port_no, unique_id FROM ports ORDER BY name ASC";
$result = mysql_query($query) or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result))
{
$port_no = $row['port_no'];
$port_name = $row['name'];
$p_unique = $row['unique_id'];
}
?>
<option value="<?php echo $p_unique;?>"
<?php
if ($p_unique == $no) {
$check = 'selected';
} else {
$check = '';
}
echo $check;
?>
>
<?php
echo $row['name'];
?>
(
<?php
echo $row['port_no'];
?>
)</option>
<?php
}
?>
</select>
Are you sure your test must be if ($p_unique == $no) {} and not if ($port_no == $no) {} ?
If yes, try to checks your variables values :
Do a var_dump() of $no :
var_dump($no);
In your while() loop, you can also check values of $p_unique and $no like that :
var_dump($p_unique.'/'.$no);
Also, here is a simplest way to test for selected :
<option value="<?php echo $p_unique;?>" <?php if ($p_unique == $no) echo 'selected="selected"'; ?>><?php echo $row['name'] ;?> (<?php echo $row['port_no'] ;?>)</option>
<div class="select_list">
<div class="labels">Category:</div>
<?php
if(!isset($_POST['postbtn)){
echo "<script>$(document).ready(function(){
$('.category').val(0);
})
</script>";
}
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
if(isset($_POST['category']) && $_POST['category']=='0'){
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
if(isset($_POST['category']) && $_POST['category']==$query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
</div>
The problem with the above code is that the selected element stays selected after the submission is done. I need the select option to return back to 'none' when the form is submitted successfully. How can that be done?
You have an syntax error in your line:
if(!isset($_POST['postbtn)){
change it to:
if(!isset($_POST['postbtn'])){
I'm guessing some things you didn't mention:
You validate your script above the code you posted, and you want - if the script was validated successfully - that the "none" option is selected.
$selected_value = $validation ? $_POST['category'] : false;
Only if the Validation is correct the value will be set, else it will be set to false;
<?php
echo "<select name='category' id='catg_list' class='list_catg'>
<option value='0'";
//CHANGE
if($selected_value == 0){ //Or if($selected_value === FALSE ||$selected_value === 0)
echo "selected";
}
echo">none</option>";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
echo "<option value='".$query_fetch['id']."'";
//CHANGE
if($selected_value !== FALSE && $selected_value == $query_fetch['id'])
{
echo "selected";
}
echo ">".$query_fetch['name']."</option>";
}
echo "</select>";
?>
Simplify your code as following to do required task.
$category = isset($_POST['category']) ? $_POST['category'] : 0;
$array["0"]="none";
$query = mysql_query("SELECT id, name from table1");
while($query_fetch = mysql_fetch_assoc($query)){
$id = $query_fetch['id'];
$array[$id] = $query_fetch['name'];
}
echo "<select name='category' id='catg_list' class='list_catg'>";
foreach($array as $id=>$name) {
$selected = $category==$id ? "selected" : "";
echo "<option value='".$id."' ".$selected." >".$name."</option>";
}
echo "</select>";
<select name="corequisite">
<option value="" selected="selected">No Corequisite</option>';
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res)){
echo'<option value="'.$row['Code'].'"';if($row['Code']==$result['corequisite']) echo'
selected="selected"';;echo'>'.$row['Name'].'</option>';
}
echo'</select>';
I want the 'No Corequisite' value to be selected if no other options will be match with $result['corequisite']. But the first item in the table will be selected!
How can I fix it?
It may be fixed by inserting 'corequisite' as the first record. A good way?
Answering your last question, probably a bad way.
Try this code, currently on the laptop so haven't tested it.
<select name="corequisite">
<?php
$res = mysql_query("SELECT * FROM course");
$rows = mysql_num_rows($res);
if ($rows == 0)
{
$sel = 'selected';
} else
{
$sel = '';
}
echo '<option '.$sel.' value="">No Corequisite</option>';
while($row = mysql_fetch_array($res))
{
$sel = '';
if ($row['Code'] == $result['corequisite'])
{
$sel = 'selected';
}
echo '<option '.$sel.' value="'.$row['Code'].'">' . $row['Name'] . '</option>';
}
?>
</select>
I haven't tested it, but this should work
<select name="corequisite">
<?php
// this is your string that will return all your results
$to_echo = '';
// this will check if any value inside is selected ( this will be useful later )
$any_item_selected = false;
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res))
{
if($row['Code']==$result['corequisite']) // OK! so one item is selected, remember it!
{
$to_echo .= '<option selected="selected" value="'.$row['Code'].'">'.$row['Name'].'</option>';
$any_item_selected = true;
}
else
$to_echo .= '<option value="'.$row['Code'].'">'.$row['Name'].'</option>';
}
// Now we can check if anything were selected, and prepend the "No corequisite" option, selected or not! :)
if( ! $any_item_selected)
$to_echo = '<option value="" selected="selected">No Corequisite</option>' . $to_echo;
else
$to_echo = '<option value="">No Corequisite</option>' . $to_echo;
echo $to_echo;
?>
</select>
I tried many things but i just cant make the value of option an id from database and i cant write the option as date and title from database
im doing this so far, any help would be appreciated.
<select name="agenda "size="10">
<?php
global $connection;
$result = mysql_query("SELECT * FROM agenda where date > now() order by date", $connection);
$i = 0;
while ($row = mysql_fetch_array($result) && $i < 20)
{
$id = $row['id_agenda'];
$date = $row['date'];
$title = $row['title'];
//here i would like to make an option with
//value = id_agenda and write the date_agenda and title_agenda
//something like this
//<option value="$row[$id]">$date $title</option>
$i++;
}
?>
<option value="Google">meeting 2</option>
</select>
Use:
echo "<option value=\"$id\">$date $title</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value=\"$row[id_agenda]\">$row[date] $row[title]</option>";
}