SO I have about 60 fields of data queried from a database. They look like this in page source:
<option>Genesis</option>
<option>Exodus</option>
<option>Leviticus</option>
For example I wanna have it so that 1-20 are a certain optgroup and then 20-60 is another. Could I do it using my format of options of would they have to be numbered like this:
<option value="1">Genesis</option>
<option value="2">Exodus</option>
<option value="3">Leviticus</option>
This is for my php class, but i dont think php is involved in making optgroups here, or is it? Thank you hope you understand my question. Hoping for help.
I pull the data from mysql using this :
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv"); ?>
And then output it in the select dropdown box using this:
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Use:
<select>
<?php
$i=1;
while($option = $query->fetch_object()){
if($i%10==1) echo "<optgroup label='Option Group'>";
echo "<option value='$i'>".$option->Name."</option>";
$i++;
if($i%10==1) echo "</optgroup>";
}
?>
</select>
Using while loop :
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
foreach ($row = mysql_fetch_array($result)) {
echo "'<option value={$row['id']}>{$row['value']}</option>'";
}
Related
i am trying below code to display list of enum values in select dropdown box.
but its displaying only dropdown box, but values are not displaying....
tablename = tbl_users, column name = userStatus
<select>
<?
$stmt = $user_home->runQuery('SHOW COLUMNS FROM '.tbl_users.' WHERE field="'.userStatus.'"');
while($data = $stmt->fetch()) {
foreach(explode("','",substr($row[1],6,-2)) as $option) {
print("<option>$option</option>");
}
}
?>
<select>
Note : I really tried lot before posting question here & i am new to php coding, still learning....
To display list of enum values in select dropdown:
<select name="select">
<?php
$sql = 'SHOW COLUMNS FROM table_name WHERE field="field_name"';
$row = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
foreach(explode("','",substr($row['Type'],6,-2)) as $option) {
print("<option value='$option'>$option</option>");
}
?>
</select>
For display enum value as dropdown you can do something like this.
<?php $status = array('Y'=>'Approve','N'=>'unapprove'); ?>
<select>
<?php foreach($status as $key=>$state) { ?>
<option value="<?php echo $key;?>"><?php echo $state;?></option>
<?php } ?>
</select>
i am using Php/Mysql , i have the client table and trying to display data in a drop down list. Unfortunately, only one client is displayed in drop down which i have the total of 3 clients. Why only one ? For example : Michael King, Michael Jordan , Michael John when i select all the data from table and make an output to display in dropdown, Michael John is only in the dropdown.
Here my Mysql code :
//All data is selected from client_tb
<?php
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$id = $row['id'];
$lname = $row['lname'];
$fname = $row['fname'];
}
?>
//my dropdown which will show the clients from client_tb but only one will appear.
<option value ="<?=$lname?><?=$fname?>"><?=$lname?> , <?=$fname?> </option> </select><br><br>
You can also achieve dropdown outside the while loop. Try this:
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
$options =array();
while($row=mysqli_fetch_array($result))
{
$options[] =$row;
}
Your dropdown:
<select name="">
<?php
foreach($options as $option):
echo '<option value ="'.$option['lname'].''.$option['fname'].'">'.
$option['lname'].','.$option['fname'].'</option>';
endforeach;
?>
</select>
You could also add your db query into a function , then call it.
function myFunction() {
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$myvalues[] =$row;
}
return $myvalues;
}
Now the dropdown,
Note the options are inside the loop
<select name="">
<?php foreach($myvalues as $myvalue) {
echo '<option value="'.$myvalue['lname'].''.$myvalue['fname'].'">'.
$myvalue['lname'].','.$myvalue['fname'].'</option>';
}
?>
</select>
Say I've got a dropdown list on my site like this:
<select>
<option value="test">Volvo</option>
<option value="icles">Saab</option>
<option value="lol">Mercedes</option>
<option value="hax">Audi</option>
</select>
But I don't want the above values, what if I want to get the values from an SQL table, how would I do this? Obviously this would be PHP but could someone give me an example?
you will have to do it like this:
first select the values:
$result = "SELECT * from table";
then you will have to foreach() those values and create your selectbox like this:
echo '<select>';
foreach($result as $res) {
echo '<option value="'.$res['somevalue'].'">' . $res['car_name'] . '</option>';
}
echo '</select>';
and you're done :D
I want to do the next thing but I don't know how to do this, I'll try to explain me
I have an field generated by php code like this (Works)
<select id="profiles_select" name="profiles_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['profile']);?>">
<?php echo strtoupper($system['profile']);?></option>
<?php
} while($system = mysql_fetch_assoc($r)); //the "$r" it's the query
$rows = mysql_num_rows($r);
if($rows > 0) {
mysql_data_seek($r, 0);
$systemas = mysql_fetch_assoc($r);
}
?>
</select>
The query
<?php
$q="SELECT DISTINCT profile FROM sys_profiles";
$r=mysql_query($q,$ConecLocal) or die(mysql_error());;
$systemas=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
What I need?
I need generate another similar to first generated by php code but, this time I need made a Query including the value of the first , something like this:
<?php
$value_select=$_GET['profiles_select'];
$q2="SELECT DISTINCT systems FROM sys_profiles where profile=$value_select";
$r2=mysql_query($q,$ConecLocal) or die(mysql_error());;
$profiles2=mysql_fetch_assoc($r);
$tsys=mysql_num_rows($r);
?>
Next of the query I need show in the another the query result, something similar to the first select (generated by php), but do the query when the first of the it's selected
<select id="systems_select" name="system_select">
<?php
do {
?>
<option value="<?php echo strtoupper($system['systems']);?>">
<?php echo strtoupper($profiles2['systems']);?></option>
<?php
} while($profiles2 = mysql_fetch_assoc($r2)); //the "$r2" it's the another query
$rows2 = mysql_num_rows($r2);
if($rows2 > 0) {
mysql_data_seek($r2, 0);
$systemas = mysql_fetch_assoc($r2);
}
?>
</select>
Thanks for the help.
I have dropdown list that gets values form array based on a mySQL SELECT query. Everything is working fine except that I would like to add the option to select ALL values in the list. Here is my code...
$dataArray = array();
$result = mysql_query("SELECT id, user_name FROM apsc_customers");
while($row = mysql_fetch_assoc($result)) {
$dataArray[$row['id']] = $row['user_name'];
}
AND
if($this->customer_id == ""){
$this->arrFilteringFields[_CUSTOMER] = array("table"=>DB_PREFIX."customers", "field"=>"id", "type"=>"dropdownlist", "source"=>$dataArray, "sign"=>"like%", "width"=>"");
}
Looking forward to any replies.
Thx
Where is your dropdown list? I can't see it in your code. Do you mean html element select created by PHP and based on data from MySQL table? Then you need to use javascript code to select all options in such dropdown list. What is $this in your code? Provide more information, more code and more clarificatios.
Do you mean:
<select>
<?php
while($row = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['user_name']; ?></option>
<?php } ?>
</select>