I try to display me database username with a blank default option. My problem is that my default option is not showing up.
<?php
mysql_connect("localhost","demo","xxxxx");
mysql_select_db("demo");
$first = 0;
$sql=mysql_query("SELECT vorname FROM users WHERE dispo=1");
if(mysql_num_rows($sql)){
$select= '<select name="select">';
while($rs=mysql_fetch_array($sql)){
if (first == 0){
$select.='<option value='' selected="selected"></option>';
$first++;
}else{
$select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>';
}
}
}
$select.='</select>';
echo $select;
?>
I'm pretty new to php and database, so I would appreciate the help.
I just changed it to always add a default blank option if there are records from the result, then it adds all records as options.
What you had was replacing the first record with the default option.
$sql=mysql_query("SELECT vorname FROM users WHERE dispo=1");
if(mysql_num_rows($sql)) {
$select= '<select name="select">';
$select.='<option value='' selected="selected"></option>';
while($rs=mysql_fetch_array($sql)){
$select.='<option value='.$rs['vorname'].'>'.$rs['vorname'].'</option>';
}
$select.='</select>';
echo $select;
?>
Change below two lines
if ($first == 0){ // from if (first == 0){
// and
$select.='<option value="" selected="selected"></option>';
// from $select.='<option value='' selected="selected"></option>';
Related
I have a MySQL table with headers "courseid", "world" and "name".
I am trying to populate an HTML dropdown box via a PHP loop and I can not get it to work.
<?php
$sql = "SELECT courseid, world, name FROM courses ORDER BY world DESC, name ASC";
$result = $link->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$world = '';
if ($world != $row['world']) {
echo '<option value="'.$row['courseid'].'">'.$row['name'].'</option>';
}else{
echo '<optgroup label="'.$row['world'].'">';
}
}
echo '</select>';
} else {
echo "0 results";
}
$link->close();
?>
I would like it to loop through the whole table and display as follows:
<optgroup label"world1">
<option value="courseid1">name1</option>
<option value="courseid2">name2</option>
<option value="courseid3">name3</option>
</optgroup>
<optgroup label"world2">
<option value="courseid4">name1</option>
<option value="courseid5">name2</option>
<option value="courseid6">name3</option>
</optgroup>
(courseid, world and name are values from the table)
A couple of issues. Firstly, you need to move $world = ''; before your while loop, otherwise it will cause the if test to always be true. Secondly, the if test should only be used to test whether to finish an old and start a new optgroup, not to decide whether to output an option. This should work:
if ($result->num_rows > 0) {
echo '<select>';
$world = '';
while($row = $result->fetch_assoc()) {
if ($world != $row['world']) {
if ($world != '') echo '</optgroup>';
echo '<optgroup label="'.$row['world'].'">';
$world = $row['world'];
}
echo '<option value="'.$row['courseid'].'">'.$row['name'].'</option>';
}
echo '</optgroup></select>';
}
else {
echo "0 results";
}
I have set a value in a session variable in 1 page, now I have to display that session variable value on another page's dropdown as selected.
I've tried everything but couldn't get it to work, maybe because of bad coding.
Here is my code:
<select name="subject_id"
class="select-form subjectSelect sub" onchange="ajaxDrp(this.value)"
>
<option value="" style="color:#000">Select</option>
<?php
$sql = "select * from mock_subject ";
$res = mysqli_query($dbhandle,$sql);
$numrows =mysqli_num_rows($res);
echo mysql_error();
if($numrows){
while($obj = mysqli_fetch_object($res)){
if($obj->status == 1){
if($subjectId == $obj->id){
echo '<option value="'.obj->id.'"
style="color:#000"
selected >'.$obj- >subject_name.'
</option>';
} else {
echo '<option value="'.$obj->id.'"
style="color:#000">'.($obj->subject_name).'
</option>';
}
}
}
}
?>
</select>
Session is already started, my session variable value is subject'Id, but in the dropdown I have to display subjectId's subject name, and code is like this code session:
$_SESSION["subject"] = "contains subject ID"
<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 populated the options for a dropdown box using the results of a query. How do I retain the selected value after the user submits?
Here's the code:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training' value=selected>Training Name</option>";
$training = strip_tags(#$_POST['training']);
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[trainingID]>$nt[trainingName]</option>";
}
Thanks!
Try this:
$query="SELECT trainingName,trainingID FROM training ORDER BY trainingName";
$result = mysql_query ($query);
echo "<select name='training'>";
echo "<option>---------------------Select---------------------</option>";
while($nt=mysql_fetch_array($result)){
$selected = false;
// check if the current value equals the value submited
if($_POST['training'] == $nt['trainingID']){
$selected = true;
}
// show selected attribute only if $selected is true
echo "<option value='{$nt['trainingID']}' ". ($selected ? "selected" : "") .">{$nt['trainingName']}</option>";
}
echo '</select>';
I am new to php, i created drop down which calling data from mysql data base, user selects option and its save to data base.
Problem Arises in edit form in which its do not showing selected value.
Drop Down code is below:
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
echo "<option value='$heading'>$heading\n";
}
echo "</select>"
Please advise solution for the edit form.
Thanks in Advance
you must close <option> tag:
echo "<option value='$heading'>$heading</option>";
$query = 'SELECT name FROM owner';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//create selection list
echo "<select name='owner'>\name";
while($row = mysql_fetch_row($result))
{
$heading = $row[0];
?>
<option <?php if($heading=="SOMETHING") { echo "selected='selected'"; } ?> value="SOMETHING">SOMETHING</option>
<option <?php if($heading=="SOMETHING2") { echo "selected='selected'"; } ?> value="SOMETHING2">SOMETHING2</option>
<option <?php if($heading=="SOMETHING3") { echo "selected='selected'"; } ?> value="SOMETHING3">SOMETHING3</option>
<?php
}
echo "</select>"
I'd do it this way.
$numrows = mysql_num_rows($result);
if ($numrows != 0){
echo "<select name='owner'>\name";
while ($x = mysql_fetch_assoc($result)){
echo "<option value='".$x['heading']."'>".$x['heading']."</option>";
}
echo "</select>";
}
$x['heading'] is using the value of the row 'heading' in the database
It's much more efficient and simply looks more sophisticated.