I am using the following code. I take some values from an xml, then I use as html option. I am using almost the same code for the first and second select. First option show in the select menu the right values: http://vilavaleaprahovei.ro/kimea/anvelope.php, but the other one shows 0. Could somebody to tell me what to do on the "Eficienta combustibil" to show the right values?
<form action="anvelope.php" method="post">
<?php
$jante = "http://vilavaleaprahovei.ro/kimea/feeds/alcarRO_feed.xml";
$xml=simplexml_load_file($jante);
$items = [];
$limitItems = 0;
foreach($xml->Produs as $child)
{
$marca = (string)$child->Marca;
if(!isset($items[$marca])) {
$items[$marca] = [];
}
$items[$marca]['sarcina'][] = (int)$child->Sarcina;
$items[$marca]['eficienta_combustibil'][] = (float)$child->Eficienta_Combustibil;
}
//SARCINA
$option_arr9 = array_column($items,'sarcina');
function generate_option9($item, $key)
{
echo "<option value='" . $item . "'>" . $item . "</option>";
}
$options = array_unique($option_arr9[0], SORT_STRING); // You can add array_unique and SORT_NUMERIC here
asort($options);
echo "<select name='sarcina'><option>Sarcina</option>";
array_walk_recursive($options, 'generate_option9');
echo "</select>";
//EFICIENTA COMBUSTIBIL
$option_arr10 = array_column($items,'eficienta_combustibil');
function generate_option10($item, $key)
{
echo "<option value='" . $item . "'>" . $item . "</option>";
}
$options = array_unique($option_arr10[3], SORT_STRING); // You can add array_unique and SORT_NUMERIC here
asort($options);
echo "<select name='eficienta_combustibil'><option>Eficienta Combustibil</option>";
array_walk_recursive($options, 'generate_option10');
echo "</select>";
?>
</form>
Related
<?php
$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
"May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
"October"=>"10","November"=>"11","December"=>"12");
echo"<select name='month'>";
echo"<option>-select-</option>";
for($i=0;$i<12;$i++)
{
echo "<option>".$month[$i]."</option>"."<br />";
}
echo"</select>";
?>
i want to display the name January,February.. in drop down option and also want their value in another variable.
$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
"May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
"October"=>"10","November"=>"11","December"=>"12");
echo"<select name='month'>";
echo"<option>-select-</option>";
foreach($month as $key=>$value){
echo "<option value='". $value ."'>".$key."</option>"."<br />";
}
echo"</select>";
$value will give its values.
You should use a foreach in this case:
<?php
$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04","May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09","October"=>"10","November"=>"11","December"=>"12");
?>
<select name="month">
<?php foreach($month as $k => $v) {
echo "<option value='" . $v . "'>" . $k . "</option>";
} ?>
</select>
We're storing the associative array's key and value in separate variables and echoing them out accordingly.
You can do it using foreach loop.
foreach ($month as $name => $value) {
echo "<option>".$name."</option>"."<br />";
}
Use foreach and print the key values
<?php
$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
"May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
"October"=>"10","November"=>"11","December"=>"12");
foreach($month as $key=>$value){
echo "<option>".$key."</option>"."<br />";
}
?>
I have seen many similar issues but none that can help explain my issue.
I have an array called cities, with a nested array for the state that has the cities of that state. It looks like this:
$cities = array(
"ca" => array(
"los-angeles" => "Los Angeles"
),
"wa" => array(
"bellingham" => "Bellingham",
"seattle" => "Seattle",
"tacoma" => "Tacoma"
)
);
My PHP code to display many HTML select fields:
<?php
foreach ($cities as $state) {
echo "<select name='city' id='" . $state . "'>";
foreach ($state as $city => $name) {
echo "<option value='" . $city . "'>" . $name . "</option>";
}
}
?>
The id of the select is always Array. How can I use the key, like "ca" or "wa"?
The problem is that you should be using the array key for the select on the states. Here is my revision of your code. Note that I explicitly have named state_key and state_value as well as city_key and city_value. Naming things explicitly like this helps in debugging. I also added a closing </select> element so the content renders correctly.
foreach ($cities as $state_key => $state_value) {
echo "<select name='city' id='" . $state_key . "'>";
foreach ($state_value as $city_key => $city_value) {
echo "<option value='" . $city_key . "'>" . $city_value . "</option>";
}
echo "</select>";
}
That's because the first foreach loop is going over an array of arrays. So each member is going to be an array. If you had id ='" . $state[0] you would see the first member of said array.
the var_dump function is your friend.
You could get all the cities by using array_keys function.
foreach ($cities as $key=>$state) {
echo "<select name='city' id='" . $key . "'>";
foreach ($state as $city => $name) {
echo "<option value='" . $city . "'>" . $name . "</option>";
}
}
change to this
in your case $state is an array containing cities. so it is always showing the value as Array().
Try uisng echo "<select name='city' id='" . $state['los-angeles'] . "'>"; instead of echo "<select name='city' id='" . $state . "'>";
If you want ca as your select element id then use below one,
foreach ($cities as $key=>$state) {
echo "<select name='city' id='" . $key . "'>";
foreach ($state as $city => $name) {
echo "<option value='" . $city . "'>" . $name . "</option>";
}
}
If you want like los-angeles as id of your select element, then use below one
foreach ($cities as $key=>$state) {
$SelectId = strtolower(str_replace(" ",'-',$state['los-angeles']));
echo "<select name='city' id='" . $SelectId . "'>";
foreach ($state as $city => $name) {
echo "<option value='" . $city . "'>" . $name . "</option>";
}
}
I was wondering if someone could help me. I want to iterate through an associative array and put the results into two combo boxes. The combo boxes will have the exact same data in each other.
while ($this->results = $this->dbhandle->fetch(PDO::Fetch_Assoc))
{
echo "<option value='" . $this->result['id] . "'>"
. $this->result['name'] . "</option>";
}
Do I have to run this loop twice for 2 seperate comboboxes or is there a more efficient way of doing this?
Your best bet in this situation is to just accumulate the text in a string and echo it out twice.
$options = "";
while ($this->results = $this->dbhandle->fetch(PDO::Fetch_Assoc))
{
$options.= "<option value='" . $this->result['id] . "'>"
. $this->result['name'] . "</option>";
}
echo "<select name='combo1'>".$options."</select>";
echo "<select name='combo2'>".$options."</select>";
How about something like this (raw code to give you an idea):
while
{
$combo_options .= "<option>" ....// rest of your code
}
Then print the variable inside your selects:
<select id="combo1"><?=$combo_options?></select>
<select id="combo2"><?=$combo_options?></select>
You can capture the output as a variable and then echo it as needed
while ($this->results = $this->dbhandle->fetch(PDO::FetchAssoc)){
$output .= "<option value='" . $this->result['id'] . "'>" . $this->result['name'] . "</option>";
}
/// later on in your script
print $output;
I would like to start out saying I don't want to use Javascript....
I have an array which is returning correctly I believe, however I am only having the first entry in the array display in the drop down list.
Is this a problem with the array? or with this function?
foreach($array as $key=>$value){
$html = "<option value='$key'>$value</key>";
}
echo "<select name="process">$html</select>";
You have to use the concatenation operator (.):
$html = '';
foreach($array as $key => $value)
{
$html.= "<option value='$key'>$value</option>";
}
echo "<select name=\"process\">$html</select>";
However, looking at the function you posted earlier, mysql_fetch_assoc only returns a single row at a time. You need to loop over that, instead. The following should suffice:
function tasks_list($p_id) {
$project_tasks = array();
$p_id = (int)
$p_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
$result = mysql_query("SELECT task_name FROM tasks WHERE project_id = $p_id");
while($project_tasks = mysql_fetch_assoc($result))
{
$html.= "<option value='".$project_tasks['task_name']."'>".$project_tasks['task_name']."</option>";
}
echo "<select name=\"process\">$html</select>";
}
You need to concat the strings. Start by initializing $html to an empty string before starting your for loop.
$html = '';
foreach($array as $key => $value)
{
$html .= "<option value='$key'>$value</key>";
// same as $html = $html . "<option value='$key'>$value</key>";
}
Note: There is also a typo in your code:
echo "<select name="process">$html</select>";
should either be:
echo "<select name=\"process\">$html</select>";
OR
echo "<select name='process'>$html</select>";
if that's what you meant.
$html = "";
foreach ($array as $key => $value){
$html .= "<option value='$key'>$value</option>";
}
echo "<select name='process'>$html</select>";
= reassigns; .= appends
If you use = it reassigns $html in every iteration, so that $html will contain the result of the last iteration - one single option.
I'm trying to modify a function that I've been using to dynamically populate <select> element to use arrays from a database. The original function used hard-coded arrays to populate the elements, and pre-selected the option which matched the db value.
The revised function creates the element, but it's only adding the first value from the db. How can I modify it so that it will loop through all the values that should be added to the <select> element?
PHP Function and Query
<?php
function printSelectOptions($dataArray, $currentSelection) {
foreach ($dataArray as $key => $value) {
echo '<option ' . (($key == $currentSelection)) . ' value="' . $key . '">' . $value . '</option>';
}
}
try {
$stmt = $conn->prepare("SELECT * FROM student");
$stmt->execute();
}catch(PDOException $e) {
echo $e->getMessage();
}
$row = $stmt->fetch();
?>
Populate Select Element
<select name="fname">
<?php
echo printSelectOptions(array($row['fname']));
?>
</select>
The Original Function & Code for Populating an Element
function printSelectOptions($dataArray, $currentSelection) {
foreach ($dataArray as $key => $value) {
echo '<option ' . (($key == $currentSelection) ? 'selected="selected"' : '') . ' value="' . $key . '">' . $value . '</option>';
}
}
<select name="fname">
<?php
$options = array("John"=>"John", "Mary"=>"Mary", "Elizabeth"=>"Elizabeth");
$selected = $row['fname'];
echo printSelectOptions($options, $selected);
?>
</select>
Since you have only fetched a single row via fetch(), only a single value is getting passed into your function printSelectOptions(). Instead, get all rows via fetchAll()
and modify your function to receive the full array, plus a string which is the column name (array key) you want to print from.
// All rows into $rows...
$rows = $stmt->fetchAll();
// Make the function accept the full 2D array, and
// a string key which is the field name to list out:
function printSelectOptions($dataArray, $currentSelection, $fieldname) {
// String to hold output
$output = '';
foreach ($dataArray as $key => $value) {
// Rather than echo here, accumulate each option into the $output string
// Use the $fieldname as a key to $value which is now an array...
$output .= '<option ' . (($key == $currentSelection)) . ' value="' . $key . '">' . htmlspecialchars($value[$fieldname], ENT_QUOTES) . '</option>';
}
return $output;
}
Then call the function as:
echo printSelectOptions($rows, $currentSelection, 'fname');
The way it is right now, the option's value attribute is populated by the array key, which would be numbered from zero. That's similar to your original array version, but it might be more useful to specify another column name like id as the key column.
// This one also takes a $valuename to use in place of $key...
function printSelectOptions($dataArray, $currentSelection, $valuename, $fieldname) {
// String to hold output
$output = '';
foreach ($dataArray as $key => $value) {
// Rather than echo here, accumulate each option into the $output string
// Use the $fieldname as a key to $value which is now an array...
$output .= '<option ' . (($value[$valuename] == $currentSelection)) . ' value="' . $value[$valuename] . '">' . htmlspecialchars($value[$fieldname], ENT_QUOTES) . '</option>';
}
return $output;
}
And would be called as:
echo printSelectOptions($rows, $currentSelection, 'id', 'fname');