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
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 have a drop down menu with two options in html, I also created a PHP script that checks what option from the drop down menu has been selected and based on the selection executes a mysql query to fetch data from database.
But I am also trying to echo out a new drop down menu with the results obtained from database and that is where the I am struggling because no errors are diaplayed but also no drop down menu is 'echoed' out onto the page.
HTML:
<?php require "course.php" ?>
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP code:
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value=''>".$result2['course']."</option>";
}
echo "</select>";
echo '</br>';
}
Could someone point out a mistake I am doing or perhaps suggest where I could look for answers
you said $query2 is displaying value in print_r.so the only mistake i find in your code is display:none .
remove display:none
echo "<select id='Forex' name='Forex' style='display: none'>";
----------------------------------------------------------------------^
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>'";
}
I've got a database of regions for sale and I'm displaying it in a table for admins to edit the cost, whether it's for sale and who's selling it (seller). For the seller I've got a drop down list with an first option as the current owner for ease of use and then under that I just want it to list the rest of the possible users that could be sellers.
Thing is, there's a good 200 regions and I don't want to loop through every user for each region to display them in a list. Is there a way I can prepare the many 's rather than loop every time. Something like preparing the list as a string then inserting it as HTML? Wouldn't know how to do it that way if possibe.
Thanks in advance.[
let's say you make your list this way:
$str = "<select>";
$result = mysql_query("select * from sellers");
while($row=mysql_fetch_assoc($result)){
$str.= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
$str.="";
And you just output that everywhere you want the list to appear.
Also, you don't have to put the one you want selected by default at the top, you can also add the word 'selected' to the option tag like so:
<option value='1' selected>John Doe</option>
If I understand the question correctly. You need to create an array of option tags.
$Name = array();
$q = "SELECT name FROM users";
$r = mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($r)){
$Name[] = "<option value='$row[\"name\"]'>$row[\"name\"]</option>";
}
Then anytime you need that you just loop through array like so:
<select id='thisselect' name='thisselect'>
<?php
foreach($Name as $key =>$line){
echo "$line";
}
?>
</select>
All the answers are good but I suggest you to separate the logic from the content itself, instead of doing all that with concatenated strings, you should do it like this:
<select id="region" name="region">
<?php foreach($regions as $key => $region) ?>
<option value="<?php echo $key ?>"><?php echo $region ?></option>
<?php endforeach; ?>
</select>
I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.
I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.