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>
Related
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>";
}
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>
I need select option like below format using php
<select>
<option value="2011-2012">2011-2012</option>
<option value="2012-2013">2011-2012</option>
<option value="2013-2014">2011-2012</option>
<option value="2014-2015">2011-2012</option>
<option value="2015-2016">2011-2012</option>
</select>
You can easily use foreach to achieve this:
<select>
<?php $years = array('2011-2012', '2012-2013', '2013-2014', '2014-2015');
foreach ($years as $year) { ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>
$years = range(date("Y"), 2006);
$yearsplus = range(date("Y", strtotime('+1 years')), 2006);
$yearrange = new MultipleIterator();
$yearrange->attachIterator(new ArrayIterator($years));
$yearrange->attachIterator(new ArrayIterator($yearsplus));
foreach($yearrange as $values) {
$result[] = $values;
}
echo "<select>";
foreach ($result as $res) {
echo '<option value="'.$res[0].'-'.$res[1].'">'.$res[0].'-'.$res[1].'</option>';
}
echo "</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"].
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>