multiple option echo php & mysql - php

how can i output more when 1 array. when i delete , ["nr"] evrything works. but i need the second one too...
<div>
<select name="name[]">
<option value = ""> Select the person with phone nbr. </option>
<?php foreach ($results as $output) {?>
<option> <?php echo $output ["fname"],["nr"]; ?> </option>
<?php } ?>
</select>
</div>

does "nr" is an index of $output array? if so, then you have to write it like this:
<?php echo $output["fname"] . "," . $output["nr"]; ?>

Related

Send multiple values from dropdown view to the controller action

I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
Thanks in advance.
You haven't kept the variable inside option's value tag.
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
Also here at PHP end, you can explode the string into an array and store it further into db.
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Thanks #BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,
<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
<select name='select' onchange="this.form.submit()">
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option>
<?php endforeach; ?>
</select>
</form>
Here is the php code,
$select = explode(',', $this->input->post('select'));
print_r($select)

How to Add/Filter Strings with <options> tag in html/php

<select name="taskOption">
<option value="<?php echo "$item_price "; ?>">
<?php echo "$item_price [ Fruit Name] "; ?>
</option>
</select>
if (isset($_REQUEST['Button'])) {
$selectOption = $_POST['taskOption'];
The above code workes fine.
I want to add one string with the value for filtration purpose.
<select name="taskOption">
<option value="<?php echo "$item_price : Item_Name"; ?>">
<?php echo "$item_price [ Fruit Name ] "; ?>
</option>
</select>
if (isset($_REQUEST['Button'])) {
$selectOption = $_POST['taskOption'];
so that when <select name="taskOption"> is called it returns two values.
e.g
<option value="<?php echo "$item_price:Item_Name"; ?>">
output 10:Item_Name
That i can seperate later using explode.
Please help how to achieve this.
The Basic idea is to geet ITEM_PRICE and ITEM_NAME , That i can save into new variables and store into db
Try value="<?php echo $item_price . ':' . $Item_Name; ?>
I am writing from my phone. Sorry for any discrepancies

set_select() not working in codeigniter framework

I want the select box show the same value before the from submit after the redirection in this code $cat value come to the session i am not using validation rule for select box i want only the select box not change after the redirection
Select Leave Category...
leave_category_id ?>"
<?php echo set_select('leave_category_id', $cat, true); ?>> <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
Try
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option>Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
// Using short tag
//$v_category->leave_category_id == $cat ? $selected = 'selected' : $selected = '';
// without using short tag
if($v_category->leave_category_id == $cat) $selected = 'selected'; else $selected = '';
?>
<option value="<?php echo $v_category->leave_category_id ?>" <?php echo $selected?> > <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
There are two things to keep in mind: First you have to use the validation class to be able to use the set_select() function. Second you have to pass the database information to that function, like this:
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', ($model->selection == 'one')); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two', ($model->selection == 'two')); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three', ($model->selection == 'three')); ?> >Three</option>
</select>
If $model->selection is two, then the second option will be selected in the example above.
see the link

Repitition of selected item in dropdownlist in PHP

I have this problem that the selected value in my dropdownlist is showing twice. What should i do so that it will show only once.
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<option value="<?php
$start_from=$jobs_result['start_from'];
//$date_arr_to= explode(" ", $start_from);
//$date_to= $date_arr_to[0];
//$time_to= $date_arr_to[1];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>" selected="selected">
<?php echo $sec_res['tp']; ?></option>
<?php include("list.php"); ?>
</select>
</td>
Got it, instead of modifying one of the available options and marking it as selected, you actually add another option with your code.
You could change list.php to look like this (i.e. an array of all the times)
<?php
$drop_down_values = array('11:35', '11:36', '11:27');
?>
Then your code should look like this:
<?php
include("list.php");
$start_from=$jobs_result['start_from'];
$sec="SELECT DATE_FORMAT('$start_from', '%H:%i') as tp";
$sec_exe=mysql_query($sec);
$sec_res=mysql_fetch_array($sec_exe);
?>
<td>
<select class="span8" style="width:100%" name="from_time[]" id="from_time<?php echo $count;?>" value="start_from" onchange="calculate_by_ajax(this.id);">
<?php foreach ($drop_down_values as $value) {
if ($value == $sec_rec) {
echo "<option selected=\"selected\">$value</option>";
} else {
echo "<option>$value</option>";
}
}
?>
</select>
</td>

Set HTML dropdown options from PHP variable

I want to set the on a HTML dropdown menu from a php variable. I give you my code so you can see I want to do:
<?php
$html_table = '
<table border="0" cellspacing="0" cellpadding="0"><tr>';
while($arr = pg_fetch_array($result1))
{
$html_table .= "<tr><td> $arr[0] </td></tr>";
}
$html_table .='</tr>';
?>
<p>
<select name="db" size="1">
<option> $html_table </option> #### <- that is my question, how to get that working
</select>
</p>
I hope you understand what I want to do. If you know about nicer ways, let me know.
Cheers
<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
$options .= '<option>'.$arr[0].'</option>';
}
?>
<p>
<select name="db" size="1">
<?php echo $options; ?>
</select>
</p>
Just replace $html_table with <?php echo $html_table; ?> and you're good to go - from the PHP point of view that is. HTML is of course invalid, as pointed out by others.
First: you didn't close your <table> tag.
Second: you can't paste<table> tag into <select>. You should put a series of <option> elements, each with one element of the array.
Third: To print PHP variable you should do like this:
<option><?php echo $html_table; ?></option>
But again: it will break your HTML, because table can't be inside select option.
The best solution is:
<select name="db" size="1">
<?php while ($arr = pg_fetch_array($result1)): ?>
<option><?php echo $arr[0]; ?></option>
<?php endwhile; ?>
</select>
so set your option elements like this
<option value="<?php echo $html_table; ?>"><?php echo $html_table; ?></option>
just know, an option can't be a table.

Categories