PHP foreach loop to print out html listbox options - php

Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?

<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>

Try this code. In your code '&lt' and '&gt' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>

Related

How to Use Dynamic Dropdown Selected value in Php

Php/Html:
Here I add The all Countries list to add/edit page.But Edit page Want Value Selected in dropdown.please help
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name ?>" selected="<?php
echo $network_list->country_name ?>"><?php echo $network_list>country_name ?></option>
<?php }?>
</select>
This is how I would do it. This will check the value against the value the user selects with the value from the database and properly populate your dropdown menu.
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
echo
'<select class="form-control" id="country_list" name="country">';
foreach($network_lists as $network_list) {
if($_POST['country'] == $network_list->country_name) {
echo '<option value="' . $network_list->country_name . '"' . ' selected="selected"' . '>' . $network_list->country_name . '</option>';
}else {
echo '<option value="' . $network_list->country_name . '">' . $network_list->country_name . '</option>';
}
}
echo
'</select>';
Selected value is in $_POST['country'];
Try:
<select class="form-control" id="country_list" name="country">
<?php
global $wpdb;
$network_lists = $wpdb->get_results("SELECT * FROM media_countries");
foreach($network_lists as $network_list){
?>
<option value="<?php echo $network_list->country_name; ?>" <?php echo ( $_POST['country_name'] == $network_list->country_name ? 'selected' : '' ); ?>><?php echo $network_list>country_name; ?></option>
<?php }?>
</select>
The above code automatically selects the option that has value equals to the submitted $_POST['country'] value.

php use 1 sql statement

How can I make my queries short?
I have the same query multiple times on my page on different sections.
Here's the code to see what I mean:
//sql for dropdown contact type
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType2 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType3 = $db->query('SELECT * FROM tb_phone_contact_type');
Output in dropdown:
<select name="contact_type1" class="form-control">
<?php
while($row = $sql_getContactType1->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
while($row = $sql_getContactType2->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
while($row = $sql_getContactType3->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
How can I achieve it in just one query statement on my output?
Do the query once, put the results in an array, and then use that array in each <select>.
<?php
$sql_getContactType = $db->query('SELECT id, name FROM tb_phone_contact_type');
$contact_types = array();
while ($row = $sql_getContactType->fetch_assoc()) {
$contact_types[] = $row;
}
?>
<select name="contact_type1" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
Why using 3 select? u can use one they do same job.
For selects you must use foreach.
First, get all data in a single query. Then you can add if inside your loop to determine the contact type to show. This example code may help you:
$contacts = $db->query('SELECT * FROM tb_phone_contact_type')->fetch_assoc();
<select name="contact_type1" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_1') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_2') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_3') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
Execute query and store data into array... You can use array multiple times..
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_info[] = $row;
}
foreach ($row_info as $info) {
echo "Id: {$info[id]}<br />"
. "Name: {$info[name]}<br />"
. "Code: {$info[code]}<br /><br />";
}
SQL for dropdown contact type
<?php
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_data[] = $row;
}
?>
Output in dropdown :
<select name="contact_type1" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
Use above the code if you need 3 dropdown on a page.

How to avoid page reload when i select dropdown list ?? and also how to get selected values php

I am stuck with small problem.
when i change my drop down list the page is getting reloaded.
Can any one help me .
This code is used to retrieve userid, based on userid get all the application id form drop down and populate it into dropdown list.
If i select value in drop down i am storing selected value
Here is my code.
<?php
$selected = " ";
function get_options($select) {
global $wpdb;
$user_ID = get_current_user_id();
echo $user_ID;
$sql = $wpdb->get_col("select Application_ID from wp3_cteTest where $user_ID = userid");
echo $sql;
//print_r($keysql);
$options = "";
while (list($k, $v) = each($sql)) {
if ($select == $v) {
$options.='<option value="' . $v . '" selected>' . $v . '</option>';
} else {
$options.='<option value="' . $v . '">' . $v . '</option>';
}
}
return $options;
}
if (isset($_POST['applications'])) {
$selected = $_POST['applications'];
echo $selected;
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<?php $default_state = 'please Select your id'; ?>
<select name="applications" onchange=this.form.submit()>
<option value=' ' ><?php echo $default_state ?></option>
<?php echo get_options($selected) ?>
</select>
</form>
</body>
</html>
I Tried removing form, if i remove form drop down doesn't work, I wont get selected value.
Then what is wrong??
Thanks in advance!
You've got onchange=this.form.submit() in your select tag, so when you change the option selected javascript is submitting the form.
Just change...
<select name="applications" onchange=this.form.submit()>
...to...
<select name="applications">
So if you just need the selected id without submit the form then replace this line:
<select name="applications" onchange=this.form.submit()>
with this :
<select name="applications" onchange="getval(this)">
and Add little script :
<script type="text/javascript">
function getval(sel) {
alert(sel.value);
}
</script>

PHP decode JSON into selectmenu selected options

I want to get the options selected, i tried many ways and when i select an option from the list and after pressing submit the selectmenu points to the first element, which makes the option that i selected not actually selected. And i can't send what the user have chosen.
<?php
$operatorsAndPackages = $_SESSION['outputOperatorsAndPackages'];
$json_a = json_decode($operatorsAndPackages,true);
?>
<label>Paquet : Opérateur : Montant</label><br/>
<select id="recharge_operator" name="recharge_operator">
<?php foreach ($json_a as $value): ?>
<?php echo $value; ?>
<option value="<?php echo $value[paquet_id] . ":" . $value[code_operateur] . ":" . $value[montant_recharge]; ?>"><?php echo $value[paquet_id] . ":" . $value[code_operateur] . ":" . $value[montant_recharge]; ?></option>
<?php endforeach ?>
</select>
You need to look at the submitted value (I'll assume this is a POST operation in my code sample) and compare it to each assembled value in the loop to determine if the current is the same as what was submitted. If it is, you set the selected attribute of your option element.
Lines 13 and 16 of the following code sample are the most pertinent changes.
<?php
$operatorsAndPackages = $_SESSION['outputOperatorsAndPackages'];
$json_a = json_decode($operatorsAndPackages,true);
?>
<label>Paquet : Opérateur : Montant</label><br/>
<select id="recharge_operator" name="recharge_operator">
<?php
foreach ($json_a as $value)
{
$value = $value['paquet_id'] . ":" . $value['code_operateur'] . ":" . $value['montant_recharge'];
$selected = ($value === $_GET['recharge_operator']);
$html_safe_value = htmlentities($value);
?>
<option value="<?php echo $html_safe_value; ?>"<?php if ($selected) {?> selected="selected"<?php } ?> >
<?php echo $html_safe_value; ?>
</option>
<?php
}
?>
</select>
I have a quick demo with source code available at: http://jaaulde.com/test_bed/SO_Lotus91/

populate multipe selectboxes from same table

I would like to populate three selectboxes with the same items (from one database table).
This is the code I have so far. I want to avoid three queries. Would you help me with a solution to populate multiple selectboxes?
<select name="resort_3" class="swcomp" data-index="3">
<?php
$today = date("Y-m-d");
$sQuery2 = "SELECT DISTINCT res_id, resort from sv_snow WHERE lud='$today' ORDER by resort";
$sResult2 = mysql_query($sQuery2) or die('Error, query 2 failed');
while($ro = mysql_fetch_assoc($sResult2))
{
echo '<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
</select>
Save the options in a string and then assign those to all 3 select boxes. Simple as that
<?php
while($ro = mysql_fetch_assoc($sResult2))
{
$options.='<option value="'. $ro['res_id'] . '">'. ucfirst($ro['resort']) . '</option> ';
}
?>
<select name="resort_1" class="swcomp" data-index="1">
<?php echo $options; ?>
</select>
<select name="resort_2" class="swcomp" data-index="2">
<?php echo $options; ?>
</select>
<select name="resort_3" class="swcomp" data-index="3">
<?php echo $options; ?>
</select>

Categories