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"].
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>
Supplier:* <br/><select name="supplier">
<?php foreach ($db->query($sql) as $row) { ?>
<option value="<?php echo $row['supplier_id']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
<?php } ?>
</select>
Retrieving data on the next php script:
$supplier_name = ??????
$supplier_id = ?????
The above code is allowing a user to make a selection of a supplier from a supplier table.
How can I pass both the supplier_name and the associated supplier_id to two different variable from the form using POST?
Bellow you have a quick example. It's not the case to use hidden input elements.
Your select:
<select name="supplier">
<?php foreach ($db->query($sql) as $row) { ?>
<option value="<?php echo $row['supplier_id'] .'|' . $row['supplier_name']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
<?php } ?>
</select>
Php script that processes the form:
if(isset($_POST['supplier']) {
$arr = explode('|', $_POST[supplier]);
if( count($arr) == 2 ) {
$supplierId = $arr[0];
$supplierName = $arr[1];
}
}
The easiest way would be to do something like:
<option value="<?php echo $row['supplier_id'].';'.$row['supplier_name']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
And then evaluate them like so:
$values = explode( ';', $_POST['supplier_info'] );
$supplier_id = $values[0];
$supplier_name = $values[1];
But it would be better to only transmit the id and then llokup the name of the supplier when evaluating the $_POST-Data
Im a newbie to this forum and have just started coding in php. Need some help. I have the following code
<?php error_reporting(-1);
require_once('/mysql/sql_connect.php');
$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
$r = #mysqli_query ($dbc, $q);
$results_array = array();
while($row = mysqli_fetch_assoc($r)) {
array_push($results_array, $row);
echo "<pre>"; print_r($results_array); echo "</pre>"; }
?>
<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
<input type="submit" name="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.
If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.
I hope I've made sense. Thanks in advance.
It will obviously echo the key, as you assigned the value of options as $key
if you need the options in the $_POST['pty_select'] use this:
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $value['profile'];?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
You mean you need the value what you have used to display it.
Then,
Change to :
<option value="<?php echo $value['profile']; ?>">
<?php echo $value['profile']; ?>
</option>
And now let's go to ideal world :)
Build data pairs database_id => name for options:
$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users
WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);
$values = array();
while($r = mysqli_fetch_row($r)) {
$values[$r[0]] = $r[1];
}
Never use # when working with database, why do you want to suppress errors instead of preventing/handling them?
Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:
function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
// Header
echo '<select';
foreach( $attributes as $key => $val){
echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
}
echo '>';
// Values
foreach( $values as $key => $val){
echo '<option value="' . htmlspecialchars( $key) .'"';
if( $key === $selected_value){
echo ' selected="selected"';
}
echo '>' . htmlspecialchars( $val) . '</option>';
}
echo '</select>';
}
And now usage :)
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<?php selectbox( $values, array( 'name' => 'pty_select')); ?>
<input type="submit" name="Submit" />
</form>
And what to do with it then?
$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
$name = $values[$id];
}
Give
<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option>
instead of
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
Change it to something like
if(isset($_POST['Submit'])) {
echo $results_array[$_POST['pty_select']]['profile'];
}
Or alternatively use profile option value.
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>