I want a dynamic option list which reads from database table lets say table of : Students,
It must show to user the list of student_name and when it is selected by user it must send student_id of that student to the database. For example:
Students table :
student_id student_name
----------------------------
1 John
2 Edward
In users option list must be included only John, Edward.. but when user selects John, the option picker must send only student_id('1') to database.
My current code , but is not fetching list from db :S :
yes, this is my code but for some reasons it ain't work :
<select Name='student_id'>
<option value="">--- Select ---</option>
<?
mysql_connect ("localhost","root","");
mysql_select_db ("mydb");
$select="student_name";
if (isset ($select)&&$select!=""){
$select=$_POST ['student_name'];
}
?>
<?
$list=mysql_query("select * from students order by student_name asc");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['student_id']; ?>"<? if($row_list['student_name']==$select){ echo "selected!"; } ?>>
<?echo $row_list['student_name'];?>
</option>
<?
}
?>
</select>
may this one help you
$result = mysqli_query($con,"SELECT * FROM Students");
echo "<select>";
while($row = mysqli_fetch_array($result)) {
echo "<option id=".$row['student_id'].">" . $row['student_name'] . "</option>";
}
echo "</select>";
mysqli_close($con);
?>
You should fetch your database from your PHP, then build your list out of it
//YOU MUST ADD YOUR DATABASE CONNECTION
mysql_connect('localhost','username','password');
mysql_select_db("dbname");
//HERE IS YOUR SQL REQUEST
$SQL_request = "SELECT * FROM `student_table`";
$req = mysql_query($SQL_request) or die(mysql_error().'<br/>'.$SQL_request);
echo '<select name = "students">';
while($result = mysql_fetch_assoc($req)){
echo '<option value="'.$result['student_id'].'">'.$result['student_name'].'</option>';
}
echo '</select>';
?>
Try below code.
$con = mysqli_connect('localhost','root','','mydb');
$sql="SELECT student_id ,student_name FROM students";
$result = mysqli_query($con,$sql);
?>
<select name = "student">
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['student_id']; ?>"><?php echo $row['student_name']; ?></option>
<?php
}
?>
</select>
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 am trying to understand the code and I am confused to understand the code at this point below. in the first example I want to fetch data from the database and display them as drop-down list but unfortunately it is not displaying anything, and in the second example everything is working fine (fetching data displaying as the drop-down list) bu the problem is that I cant understand the code in the second example it looks much more complicated than the first example. So I want to correct my first example and after that I can rebuilt the second example based on the first one.
First table is department (ID, persondepartment, Description)
Second table board ( personboard, description)
ps: this is for my project so I dont care about Security
First Example - Not Working
<td>Department</td>
<td>
<select name='persondepartment' ID="dept">
<option ID="0">-- Select Department --</option>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$getalldepartments = "SELECT * FROM department";
WHILE ($viewdepartments = mysql_fetch_array(
$getalldepartments)){
?>
<option ID="<?php echo $viewdepartments['ID'];?>
"> <?php echo $viewdepartments ['persondepartment'] ?></option>
<?php } ?>
</select>
</td>
Second Example - Working
<td>Board</td>
<td>
<?php
mysql_connect('localhost', 'data_datab', 'password');
mysql_select_db ("data");
$sql = "SELECT * FROM board";
$result = mysql_query($sql);
echo "<select name='personboard'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['personboard'] . "'>" . $row['personboard'] . "</option>";
}
?>
</td>
Try to change
$getalldepartments = "SELECT * FROM department";
to:
$getalldepartments = mysql_query("SELECT * FROM department");
and consider to use mysqli instead of deprecated mysql extension.
Correct version with PDO
<td>Department</td>
<td>
<select name="persondepartment" id="dept">
<option value="0">-- Select Department --</option>
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=data', 'data_datab', 'password');
} catch (PDOException $e) {
echo '<option value="">' . $e->getMessage() . '</option>';
}
$getalldepartments = $pdo->query("SELECT * FROM department");
while ($viewdepartments = $getalldepartments->fetch(PDO::FETCH_ASSOC)) {
?>
<option value="<?= $viewdepartments['ID']; ?>"><?= $viewdepartments['persondepartment']; ?></option>
<?php
}
?>
</select>
</td>
I am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular
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>