PHP dropdown populated from database - php

I need to pull down a column of color codes for a color picker site I'm making but it won't work:
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
$getColors = mysql_query("SELECT * FROM color_codes");
while($list = mysql_fetch_array($getColors)){
?>
<option id="<?php echo $list ['colorID']; ?>">
<?php echo $list['color_code'] ?></option>
<?php } ?>
</select>
Any ideas?

just did a couple of changes to your code and it worked for me, Hope it works for you.
<?php
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die(mysqli_connect_error());
$sql="select * from test";
$result=$mysqli->query($sql);
?>
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
while($list = $result->fetch_array(MYSQLI_ASSOC)){
?>
<option id="<?php echo $list['id']; ?>">
<?php echo $list['name'] ?></option>
<?php } ?>
</select>

Related

how to get the data on script and load it in query

Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
Try:
onchange="getdll(this.value)"
THEN in your script, put a parameter
function getdll(val){
alert(val);
}
Should be able to get the value you selected

space between 2 column names with php from sql

I ve got the idea of basic Concat with sql but i cant get a space between first and surname on my drop down menu. The code all works its just a basic thing i cant find to give me a space between the first name and the last name.
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first,surname) from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['CONCAT(first,surname)']; ?></option>
<?php
}
?>
</select>
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first,' ',surname) as full_name from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['full_name']; ?></option>
<?php
}
?>
</select>
Update you code like this:
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first, ' ', surname) as fullname from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['fullname']; ?></option>
<?php
}
?>
</select>

some one help me, i cant get value from database when edit filed to select

I have create a select option form but when edit i can get current value from database.
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($nm_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?php echo $selected;?>>
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
You should not use value for select tag. Check the condition in inside the option tag and make it is selected like following
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){?>
<option value="<?php echo $value->kd_proyek; ?>" <?php if($nm_proyek==$value->kd_proyek) { ?> selected="selected" <?php }?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
<?php
$conn = new mysqli('SERVER NAME', 'DB USER NAEM', 'DB USER PASSWORD', 'DB NAME');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//set the value of which option you want to selected
$nm_proyek = '';
?>
<!-- for combobox you should use 'multiple' -->
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." multiple />
<option value=""></option>
<?php
$query = $conn->query("SELECT * FROM `t_proyek`");
while($value = $query->fetch_object()) {?>
<option value="<?php echo $value->kd_proyek; ?>" <?php $nm_proyek == $value->kd_proyek ? "selected='selected'" : "" ?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>

I want to select a table from a database and show in a drop down list

I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.
Thanks a lot.
Current Code:
<?php
include 'database/connectdatabase.php';
if(isset($_POST['select_db']))
{
$select_db = $_POST['select_db'];
$selectdb_query = 'SHOW TABLES FROM $select_db';
$query_select = mysql_query($selectdb_query,$connectDatabase);
if(!$query_select)
{
echo 'NO table selected!';
}
?>
<form method="POST" action="selecttable.php" autocomplete="off">
<select name="select_db">
<option selected="selected">Select Database</option>
<option>section_masterfile</option>
</select>
</form>
<form method="POST" action="#" autocomplete="off">
<?php while ($row = mysql_fetch_row($query_select)) {
$num_row = mysql_num_rows($row);?>
<select name="select_table">
<option selected="selected">Select Table</option>
<?php for($i=0;$i>=$num_row;i++){?>
<option><?php echo $row[0];?></option>
<?php}?>
</select>
<?php}?>
</form>
The problem is that you're already fetching rows yet you haven't even submitted the form yet.
I suggest restructure you logic this way:
$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
$select_db = $con->real_escape_string($_POST['select_db']); // escape the string
$query = $con->query("SHOW TABLES FROM $select_db");
while($row = $query->fetch_assoc()) {
$tables[] = $row['Tables_in_' . $select_db]; // use associative instead
}
}
?>
<form method="POST" autocomplete="off">
<select name="select_db" onchange="this.form.submit();">
<option disabled selected>Select Database</option>
<option>test</option>
</select>
<br/><br/>
<select name="select_table">
<?php foreach($tables as $table): ?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php endforeach; ?>
</select>
</form>
Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.
error_reporting(E_ALL);
ini_set('display_errors', '1');
<select name="select_table">
<option selected="selected">Select Table</option>
<?php while ($row = mysql_fetch_row($query_select)) { ?>
<option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php } ?>
</select>
Try This
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select))
{
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Consider this code:
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select)) {
?>
<option><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>

How to Get Two option value by 1 select?

Please modify this code so that i will get 2 option value from this code
<select name="student_class1" class="input-xlarge" >
<option value="null">--Select Class---</option>
<?php $sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while($display_class= mysql_fetch_assoc($sel_service))
{ ?>
<option value="<?php echo $display_class['sub_cat_id']; ?>"><?php echo ucfirst($display_class['sub_cat_name']); ?></option>
<?php } ?>
student_class1 giving me value sub_cat_id
i want
student_class2 will give me sub_cat_name
iwant both values by one select
plz modify it
i already spent my whole sunday on this problem
something like?
<form action="" method="get" accept-charset="utf-8">
<select name="student_class1" class="input-xlarge" >
<?php
$sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while ($row = mysql_fetch_assoc($sel_service)) {
?>
<option value="null">--Select Class---</option>
<option value="<?php echo $row['sub_cat_id']; ?>"></option>
<option value="<?php echo $row['sub_cat_name']; ?>"></option>
<?php
}
?>
</select>
</form>

Categories