I have a list of checkboxes displayed below. This shows all the contactors and allows them to be selected via check box.
<?php
$query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
<option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
</select></li>
I have an array saved in another place that I would like to generate the list above but with the items in the array below already checked/selected.
<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");
while($row = mysql_fetch_array($result))
{
$contractors = unserialize($row['contractors']);
foreach ($contractors as $contractor)
echo "" . htmlspecialchars ($contractor).' - ';
?>
Any help would be greatly appreciated.
Try this :
<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>
Related
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>
i have a table in my database called categorys, it has two columns cat_id and cat_name ,, i finally managed to get the select right but it doesn't show all of the table rows, there's 22 row it only shows 11 of them ! how can i fix that ?
here's the code i used
$sql = "SELECT cat_id, cat_name FROM categorys";
$result = $conn->query($sql);
?>
<select name="taskOption">
<?php if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { ?>
<option value="<?php echo $row['cat_id']; ?>>
<?php echo $row['cat_name']; ?>
</option> <?php } ?>
</select>
<?php } ?>
Change here, missing closing double quote "
<option value="<?php echo $row['cat_id']; ?>">
You are missing the closing quote after your option value. So it takes two times through before it closes the value. Therefore, only half your rows are showing.
<?php
$sql = "SELECT cat_id, cat_name FROM categorys";
$result = $conn->query($sql);
?>
<select name="taskOption">
<?php if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){ ?>
<option value="<?php echo $row['cat_id']; ?>">
<?php echo $row['cat_name']; ?>
</option> <?php } ?>
</select>
<?php } ?>
This is my working code that fetches the data of the id_no in the database. Now, what I want is when I selected a specific id_no the name of the person owning that id_no will automatically be printed. :
<?php
$sql = "SELECT id_no from employee ORDER BY id_no asc";
$result = $conn->query($sql);
?>
<select name='id_no'>
<?php while ($row = $result->fetch_array()) { ?>
<option value="id_no" > <?php echo($row['id_no']); ?> </option>
<?php } ?>
</select>
Thanks so much!
this code will display the selected value. try this
<?php
$sql = "SELECT id_no from employee ORDER BY id_no asc";
$result = $conn->query($sql);
?>
<select name='id_no' onchange="changeSelect(this.value)">
<?php while ($row = $result->fetch_array()) { ?>
<option value="<?php echo($row['id_no']); ?>" >
<?php echo($row['id_no']); ?>
</option>
<?php } ?>
</select>
<div id="demo"></div>
<input type="text" id="text-input" />
<script>
function changeSelect(value)
{
document.getElementById("demo").innerHTML = value;
document.getElementById("text-input").value = value;
}
</script>
<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>
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();
?>