Set first value of dynamic HTML option to empty - php

<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option value="">Select</option>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Above is the php code of the select option. Populated with data from database table, how can I set that the first value if empty. I tried adding a Select but this is the result.
The option select keeps repeating. What to do with this?

Try this
<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
?>
<option value="">Select</option>
<?php
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>

Just move that option line "Select" out of your php code:
<select id="section" name="section">
<option value="">Select</option>
<?php
...your php code
?>
</select>

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

How to show the value of the database to combobox?

How to show the value of the database to combobox? I've tried like this:
http://pastebin.com/gZL4qAPS
$result = $koneksi->query(
"SELECT tb_sekolah.idSekolah,
tb_sekolah.namaSekolah,
tb_sekolah.tb_kategori_sekolah_idKategori,
tb_kategori_sekolah.namaKategori,
tb_uptd.namaUPTD,
tb_sekolah.alamat,
tb_sekolah.telp,
tb_sekolah.kataSandi,
tb_sekolah.status
FROM tb_sekolah, tb_kategori_sekolah, tb_uptd
WHERE tb_sekolah.tb_kategori_sekolah_idKategori = tb_kategori_sekolah.idKategori
AND tb_uptd.idUPTD = tb_sekolah.tb_UPTD_idUPTD
AND idSekolah='$id'"
);
while ($row = $result->fetch_array()){
<select name="id_kategori" size="1" class="form-control" required>
<option label="-- Pilih Kategori --" ></option>
<?php //looping kategori
$result1 = $koneksi->query("SELECT * FROM tb_kategori_sekolah");
while ($row1 = $result1->fetch_array()){
if ($row['tb_kategori_sekolah_idKategori']==$row1['idKategori']){
$status = 'selected' ;
} ?>
<option <?php echo isset($status)?$status:''; ?> value="<?php echo $row1['idKategori'] ?>"><?php echo $row1['namaKategori']; ?>
</option>
<?php
}
?>
</select>
<?php
}
?>
were selected to always be the last value of tb_kategori_sekolah
it is simply because you have not set declare
$status = NULL;
inside the While function
your code suppose to be like this
<select name="id_kategori" size="1" class="form-control" required>
<option label="-- Pilih Kategori --" ></option>
<?php //looping kategori
$result1 = $koneksi->query("SELECT * FROM tb_kategori_sekolah");
while ($row1 = $result1->fetch_array()){
// DECLARE THIS AS NULL
$status = NULL;
if ($row['tb_kategori_sekolah_idKategori']==$row1['idKategori']){
$status = 'selected' ;
} ?>
<option <?php echo isset($status)?$status:''; ?> value="<?php echo $row1['idKategori'] ?>"><?php echo $row1['namaKategori']; ?>
</option>
<?php
}
?>
</select>

Setting selected option in drop-down box

My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>

Display text from a mysql table like a array

<?php
mysql_query("SELECT * FROM page where page_id = '$page_id'") or die(mysql_error());
while($land = mysql_fetch_array($resultxx)){
$land =$land['land'];
?>
output $land is NL,DE,BE,AL..
How is it possible to splits the output
example
<?php
echo "
<select>
<option value='NL'>NL</option>
<option value='DE'>DE</option>
<option value='BE'>BE</option>
<option value='AL'>AL</option>
</select>";
}
?>
Try This
<?php
mysql_query("SELECT * FROM page where page_id = '$page_id'") or die(mysql_error());
while($land = mysql_fetch_array($resultxx)){
$land =$land['land'];
$landarray = explode(",",$land);
$count = count($landarray);
echo "<select>";
for($i=0;$i<$count;$i++){
echo '<option value='.$landarray[$i].'>'.$landarray[$i].'</option>';
}
echo "</select>";
}
?>

Select Form: Database request many times

I have a database that I need to use to do select.
The select is repeat many times (days of the week).
I can't do a loop for because it can have some "gap" in the id so I use the while.
The problem is that for each select I need to call the database again and I think it's not really a good solution. I would like to find another solution which isn't so "heavy"
Here is my code:
<select name="monday">
<option value="none">Monday </option>
<?php
$Requete2 = "SELECT * FROM `record`";
$Result2 = mysql_query($Requete2) or die(mysql_error());
$rows2 = mysql_num_rows($Result2);
while($row2 = mysql_fetch_array($Result2)){ ?>
<option value="<?php echo ($row2['id'] - 1); ?>"><?php echo $row2['name']; ?></option>
<?php } ?>
</select>
<select name="tuesday">
<option value="none">Tuesday </option>
<?php
$Requete2 = "SELECT * FROM `record`";
$Result2 = mysql_query($Requete2) or die(mysql_error());
$rows2 = mysql_num_rows($Result2);
while($row2 = mysql_fetch_array($Result2)){ ?>
<option value="<?php echo ($row2['id'] - 1); ?>"><?php echo $row2['name']; ?></option>
<?php } ?>
</select>
Thanks
If you want to print out the same for every day you can use this(I havn't tested it for syntax errors):
<?php
$sql_query = "SELECT * FROM `record`";
$query = mysql_query($sql_query) or die(mysql_error());
$optionHtml = '';
while($row = mysql_fetch_array($query)){
$optionHtml .= '<option value="' . ($row["id"] - 1) . '">' . $row["name"] . '</option>';
}
?>
<select name="monday">
<option value="none">Monday</option>
<?php echo $optionHtml; ?>
</select>
<select name="tuesday">
<option value="none">Tuesday</option>
<?php echo $optionHtml; ?>
</select>

Categories