PHP Select Option from Array list - php

im new to PHP and im trying this stuff for hours, I wanted to show the Array items to the Select Option. Here's the code:
<select name="state">
<?php
$arrstate=array
(
array("AK","Alaska"),
array("AL","Alabama"),
array("AR","Arkansas"),
array("AZ","Arizona"),
array("CA","California"),
array("CO","Colorado"),
array("CT","Connecticut"),
array("DC","District of Columbia"),
array("DE","Delaware"),
array("FL","Florida"),
array("GA","Georgia"),
array("HI","Hawaii"),
array("IA","Iowa"),
array("ID","Idaho"),
array("IL","Illinois"),
array("IN","Indiana"),
array("KS","Kansas"),
array("KY","Kentucky"),
);
for($lop=0;$lop<=49;$lop++)
{
if (strtoupper($lead_info['state'])==$arrstate[$lop][0])
{
echo "<option selected=\"selected\" value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";
}else{
echo "<option value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";
}
}
?>
</select>
But it seems it only shows ".$arrstate[$lop][1]."
What seems to be the problem?

try it
<?php
$arrstate = array
(
'AK' => 'Alaska',
'AL' => 'Alabama',
'AR' => 'Arkansas',
'AZ' => 'Arizona'
); ?>
<select>
<?php foreach($arrstate as $key => $value) { ?>
<option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
<?php } ?></select>

<?php
$arrstate=array
(
"AK","Alaska",
"AL","Alabama",
"AR","Arkansas",
"AZ","Arizona"
);
?>
this is the easiest way...
<select>
<?php
foreach($arrstate as $key => $value) {
?>
<option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
<?php
}
?>
</select>

You can retrieve from database like this example:
<select>
<?php $result = mysqli_query($conn, "SELECT * FROM categories");
while ($row = mysqli_fetch_array($result)) {
$categories = array($row["category"]);
$arrlength = count($categories);
for($x = 0; $x < $arrlength; $x++) {
echo "<option>";
echo $categories[$x];
echo "</option>";
}}?>
</select>

Related

Dropdown value stays selected

I have a dropdown and I want the input selected after the page refresh or post. So every post has the same $hoogte_array.
<table><form action="index.php" method="post">
<tr><th>Hoogte: <select name="hoogte">
<?
$hoogte_array[1]= 63;
$hoogte_array[2]= 103;
$hoogte_array[3]= 123;
$hoogte_array[4]= 153;
$hoogte_array[5]= 173;
$hoogte_array[6]= 203;
$kleur_array[1] = "groen";
$kleur_array[2] = "blauw";
foreach ($hoogte_array as $key => $valuehoogte)
{
echo "<option value='".$key."''>".$valuehoogte."</option>";
}
?>
</select></th>
<th>Kleur: <select name="kleur">
<?
foreach ($kleur_array as $key => $valuekleur)
{
echo "<option value='".$key."''>".$valuekleur." </option>";
}
?>
foreach ($hoogte_array as $key => $valuehoogte)
{
$hoogte = (isset($_POST['hoogte']))?$_POST['hoogte']:"1";
$sel = ($hoogte == $key)? 'selected="selected"' : '';
echo "<option value='".$key."'' ".$sel.">".$valuehoogte."</option>";
}

Echo selected value first

I need to echo the selected value first in a form when user wants to make an update. I tried several variants:
1)
<?php
$opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
echo '<select name="up_opt" >' ;
foreach ($opt as $i => $value) {
echo "<option value=\"$i\"";
if ($_REQUEST['up_opt'] == $i)
{
echo "selected" ;
}
echo ">$opt[$i]</option>" ;
}
echo '</select>' ;
?>
2)
<?php $opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
$edu = $_REQUEST['edu'];
<select name="up_opt">
<?php foreach ( $opt as $i=>$opt ) : ?>
<option value="<?php echo $i?>" <?php echo $i == $edu ? 'selected' : ''?>><?php echo $opt ?></option>
<?php endforeach; ?>
</select>
3)
<select name="up_opt">
<option value="1" <?php if ($_GET['1'] == 'option1') { echo 'selected'; } ?>>Opt1</option>
<option value="2" <?php if ($_GET['2'] == 'option2') { echo 'selected'; } ?>>Opt2</option>
<option value="3" <?php if ($_GET['3'] == 'option3') { echo 'selected'; } ?>>Opt3</option>
</select>
None of these variants echoes the checked value first. Can someone help me, tell me what is wrong or give me another variant?
Variant 3 is ok (but I'd rather use a loop instead of hard-coded options). Your mistake is that you compare 'option1', 'option2' and so one when your real values are '1', '2', '3'. Also as #ElefantPhace said, don't forget about spaces before selected, or you'll get invalid html instead. So it would be this:
<select name="up_opt">
<option value="1" <?php if ($_GET['up_opt'] == 1) { echo ' selected="selected"'; } ?>>Opt1</option>
<option value="2" <?php if ($_GET['up_opt'] == 2) { echo ' selected="selected"'; } ?>>Opt2</option>
<option value="3" <?php if ($_GET['up_opt'] == 3) { echo ' selected="selected"'; } ?>>Opt3</option>
</select>
With loop:
<?php
$options = array(
1 => 'Opt1',
2 => 'Opt2',
3 => 'Opt3',
);
?>
<select name="up_opt">
<?php foreach ($options as $value => $label): ?>
<option value="<?php echo $value; ?>" <?php if ($_GET['up_opt'] == 1) { echo ' selected="selected"'; } ?>><?php echo $label; ?></option>
<?php endforeach ?>
</select>
Here is all three of your variants, tested and working as expected. They are all basically the same, you were just using the wrong variable names, and different ones from example to example
1)
<?php
$opt= array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3') ;
echo '<select name="up_opt">';
foreach ($opt as $i => $value) {
echo "<option value=\"$i\"";
if ($_POST['up_opt'] == $i){
echo " selected";
}
echo ">$value</option>";
}
echo '</select>';
?>
2) uses same array as above
$edu = $_POST['up_opt'];
echo '<select name="up_opt">';
foreach ( $opt as $i => $value ){
echo "<option value=\"$i\"", ($i == $edu) ? ' selected' : '';
echo ">$value</option>";
}
echo "</select>";
3)
echo '<select name="up_opt">';
echo '<option value="1"', ($_POST['up_opt'] == '1') ? 'selected':'' ,'>Opt1</option>';
echo '<option value="2"', ($_POST['up_opt'] == '2') ? 'selected':'' ,'>Opt2</option>';
echo '<option value="3"', ($_POST['up_opt'] == '3') ? 'selected':'' ,'>Opt3</option>';
echo '</select>';

dropdown list is empty in <select>

I cannot seem to find my error, my dropdown list is empty but there are values in the array when i do a print_r($data).
My code:
<?php $sql = "SELECT p.firstname, p.lastname from person p where p.personid >= :personid";
$array = array('personid' => 75101);
$sth = $dbh->prepare($sql);
if($sth->execute($array)) {
$data = $sth->fetchAll();
} ?>
<tr><td style="width:120px;">Contact Person</td>
<td>
<select>
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php $key; ?>" > <?php $value ?> </option>
<?php } ?>
</select>
</td>
can some one help please?
thanks
try this...
<?php $sql = "SELECT p.firstname, p.lastname from person p where p.personid >= :personid";
$array = array('personid' => 75101);
$sth = $dbh->prepare($sql);
if($sth->execute($array)) {
$data = $sth->fetchAll();
} ?>
<tr><td style="width:120px;">Contact Person</td>
<td>
<select>
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
<?php } ?>
</select>
</td>
Try this
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
<?php } ?>
You cant print a variable without using echo , you forgot to print the $key and $value ,
change
<option value=" <?php $key; ?>" > <?php $value ?> </option>
with
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
You are not print variables. Only use in your code.
If You need short tags, try this code:
<?php foreach ($data as $key => $value) { ?>
<option value="<?=$key;?>"><?=$value["firstname"]." ".$value["lastname"];?></option>
<?php } ?>
But if on your server short tags is disabled then try code from previous replay, only $value variables replace with $value["firstname"].

convert the values from the json to drop down

i would like to extract all the values that correspond to the organisations to come under the drop down menu as options for organisation,but now only the last value of the oraganisation is showing up in the drop down as an option
here is my code
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
echo $each->location[0]->building[0];
echo $each->location[0]->name;
echo $each->name;
}
$org=$each->name;
$arr=array($org);
reset($arr);
//print_r($org);
//$result = count($org);
//echo $result;
while(list(,$value)=each($arr)){
//echo "value:$value<br/>\n";
//$_SESSION['organisation']=$value;
//echo $_SESSION['organisation'];
}?>
<select name="category_id">
<option value=""></option>
<?php
$keys = array_keys($arr);
$count1=count($keys);
echo $count1;
for($i=0; $i<count($arr); $i++)
{?>
<option value="<?php echo $keys[$i]; ?>"><?php echo $arr[$i]; ?></option>
<?php
}
?>
</select>
Make use of that first foreach rather than starting an entire new loop. Tested and this works:
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
echo '<select name="category_id"><option value=""></option>';
$stepper = 0;
foreach($items as $each) {
$building = $each->location[0]->building[0];
$name = $each->location[0]->name;
$final_name = $each->name;
echo '<option value="'.$stepper.'">'.$final_name.'</option>';
$stepper++;
}
echo '</select>';
?>
Use this code
<?php
$org = array();
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
$org[]=$each->name;
}
?>
<select name="category_id">
<option value=""></option>
<?php
foreach($org as $key=>$val)
{?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>

selected state mysql array

I am trying to combine a table select output with a piece of code I found to keep the selected state:
<select>
<?php
$desired_option = 'arsenal';
$arr = array('arsenal', 'aston villa', 'birmingham', 'blackpool', 'bolton');
for($i = 0; $i < count($arr); $i++) {
$selected = ($arr[$i] == $desired_option) ? 'selected="selected"' : '';
echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>";
}
?>
</select>
<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<? echo $teamsData['id']; ?>" ><? echo $teamsData['name']; ?></option>
<?
}
?>
</select>
I have tried everything. Any ideas?
Thanks :)
<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$selected = 'team_to_be_selected';
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<?php echo $teamsData['id']; ?>" <?php echo ($teamsData['name'] == $selected) ? 'selected="selected"' : ''; ?>><?php echo $teamData['name'];?></option>
<?
}
?>
</select>

Categories