I have a table and I put the values in it with an array
I want to add a select tag that takes the values in each row of the table from the array and displays them in the select and sends the new values through the post method.
please help
<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
<select class="form-control" name="pay_method[]" id="pay_method">
<option value="0" <?php if ($pay_method[$i] == '0') echo 'selected=""'; ?>>نقد</option>
<option value="1" <?php if ($pay_method[$i] == '1') echo 'selected=""'; ?>>چک</option>
<option value="2" <?php if ($pay_method[$i] == '2') echo 'selected=""'; ?>>کارت خوان</option>
<option value="3" <?php if ($pay_method[$i] == '3') echo 'selected=""'; ?>>کارت به کارت</option>
</select>
</td>
<?php } ?>
It is unclear what you are expecting. Here are the fixes for the flaws in the code. In particular, ids and names must be unique, so I have appended the iterator. And you don't need to say selected=''.
<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
<select class="form-control" name="pay_method<?php echo $i;?>" id="pay_method<?php echo $i;?>">
<option value="0" <?php if ($pay_method[$i] == '0') echo 'selected'; ?>>نقد</option>
<option value="1" <?php if ($pay_method[$i] == '1') echo 'selected'; ?>>چک</option>
<option value="2" <?php if ($pay_method[$i] == '2') echo 'selected'; ?>>کارت خوان</option>
<option value="3" <?php if ($pay_method[$i] == '3') echo 'selected'; ?>>کارت به کارت</option>
</select>
</td>
<?php } ?>
Related
I need multiple values in select2's select box, but it selecting the last one.
I can insert apple, tomato into the database, but only tomato displays in select2 in the browser.
This is the code that I have, that is only selecting one value for the select, not multiple.
<?php if ($views['mb_medical']) $medi_arr = explode('|', $views['mb_medical']);
for ($i = 0; $i < count($medi_arr); $i++) { ?>
<select class="medical_subject" name="mb_medical[]" id="mb_medical" multiple="multiple">
<option value="apple" <?php if($medi_arr[$i] == 'apple') echo 'selected'; ?>>apple</option>
<option value="tomato" <?php if($medi_arr[$i] == 'tomato') echo 'selected'; ?>>tomato</option>
<option value="banana" <?php if($medi_arr[$i] == 'banana') echo 'selected'; ?>>banana</option>
<option value="melon" <?php if($medi_arr[$i] == 'melon') echo 'selected'; ?>>melon</option>
</select>
<?php } ?>
In picture, select2 is missing apple, but it does have tomato.
I have already checked the $medi_arr[$i] outputs correclty.
I am adding a sample code so you can use this code in your code to full-fill your desired task.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js" integrity="sha512-2ImtlRlf2VVmiGZsjm9bEyhjGW4dU7B6TNwh/hx/iSByxNENtj3WVE6o/9Lj4TJeVXPi4bnOIMXFIJJAeufa0A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" integrity="sha512-nMNlpuaDPrqlEls3IX/Q56H36qvBASwb3ipuo3MxeWbsQB1881ox0cRv7UPTgBlriqoynt35KjEwgGUeUXIPnw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<select name="category" class="select2 select2-offscreen" id="category" style="width:100%" data-placeholder="Select Fields" tabindex="-1" multiple>
<option value="">Select Category</option>
<option value="1">Business</option>
<option value="7">Education</option>
<option value="5" selected="selected">Environment</option>
<option value="3" selected="selected">Health</option>
<option value="11">Movies</option>
<option value="4">Science</option>
<option value="2">Sports</option>
<option value="6">Tech</option>
<option value="10">Top Stories</option>
</select>
<script>
$("#category").select2();
</script>
it's work finally.
<?php if ($views['mb_medical']) {
$medi_arr = explode('|', $views['mb_medical']); ?>
<select class="medical_subject" name="mb_medical[]" id="mb_medical" multiple>
<option value="apple"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'apple') echo 'selected';} ?>>apple
</option>
<option value="tomato"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'tomato') echo 'selected';} ?>>tomato
</option>
<option value="banana"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'banana') echo 'selected';} ?>>banana
</option>
<option value="melon"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'melon') echo 'selected';} ?>>melon
</option>
</select>
How do i keep my select item selected via PHP? i did it the same way with radiobuttons and it worked. the only thing i changed was changed "checked" to "selected" as was recommended to me.
Here is my PHP:
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
And here is my form:
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && $favFruit == "apple") echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && $favFruit == "banana") echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && $favFruit == "plum") echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && $favFruit == "pomegranate") echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && $favFruit == "strawberry") echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && $favFruit == "watermelon") echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
$favFruit will be an array even when selecting a single option, and null when nothing is selected.
print_r($_POST["favFruit"]); will return Array ( [0] => banana [1] => plum )
You need for see if the item exists in the array and select based on that check.
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && in_array('apple', $favFruit)) echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && in_array('banana', $favFruit)) echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && in_array('plum', $favFruit)) echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && in_array('pomegranate', $favFruit)) echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && in_array('strawberry', $favFruit)) echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && in_array('watermelon', $favFruit)) echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
I have this query inside a codeigniter view and it takes about 10 minutes to load the data. If I remove the second select it loads pretty normally.
Could anyone see any reason to cause this?
Is there anything I can do here to improve the execution time and therefore increase my page load?
My code is as follows:
<tbody>
<?php
//$this->db->select('teacher.*,teacher_attendance.*');
$this->db->select('teacher.teacher_id,teacher.name,teacher_attendance.*');
$this->db->from('teacher_attendance');
$this->db->where ('timestamp', $timestamp);
$this->db->group_by('teacher.teacher_id');
$this->db->join('teacher', 'teacher.teacher_id = teacher_attendance.teacher_id', 'left');
//$this->db->limit(50);
$teachers =$this->db->get()->result_array();
foreach($teachers as $row){
?>
<tr>
<td><?php echo $row['teacher_id'];?></td>
<td><img src="<?php //echo $this->crud_model->get_image_url('teacher',$row['teacher_id']);?>" class="img-circle" width="30" /></td>
<td><?php echo $row['name'];?></td>
<td>
<select id="selected1" class="sele1 form-control selectboxit" name="status_<?php echo $row['attendance_id']; ?>">
<option value="0" <?php if ($row['status'] == 0) echo 'selected'; ?>><?php echo get_phrase('undefined'); ?></option>
<option value="1" <?php if ($row['status'] == 1) echo 'selected'; ?>><?php echo get_phrase('present'); ?></option>
<option value="2" <?php if ($row['status'] == 2) echo 'selected'; ?>><?php echo get_phrase('absent'); ?></option>
</select>
<!-- <input type="radio" name="status_<?php echo $row['attendance_id']; ?>" value="1" <?php if ($row['status'] == 1) echo 'checked'; ?>/>
<input type="radio" name="status_<?php echo $row['attendance_id']; ?>" value="2" <?php if ($row['status'] == 2) echo 'checked'; ?> />
-->
</td>
<td>
<select class="form-control selectboxit" name="leave_status<?php echo $row['attendance_id']; ?>">
<option value="0" <?php if ($row['leave_status'] == 0) echo 'selected'; ?>><?php echo get_phrase('undefined'); ?></option>
<option value="1" <?php if ($row['leave_status'] == 1) echo 'selected'; ?>><?php echo get_phrase('CASUAL'); ?></option>
<option value="2" <?php if ($row['leave_status'] == 2) echo 'selected'; ?>><?php echo get_phrase('MEDICAL'); ?></option>
<option value="3" <?php if ($row['leave_status'] == 3) echo 'selected'; ?>><?php echo get_phrase('DUTY'); ?></option>
<option value="4" <?php if ($row['leave_status'] == 4) echo 'selected'; ?>><?php echo get_phrase('HALF DAY'); ?></option>
<option value="5" <?php if ($row['leave_status'] == 5) echo 'selected'; ?>><?php echo get_phrase('OTHER'); ?></option>
<option value="6" <?php if ($row['leave_status'] == 6) echo 'selected'; ?>><?php echo get_phrase('LATE'); ?></option>
<option value="7" <?php if ($row['leave_status'] == 7) echo 'selected'; ?>><?php echo get_phrase('SHORT'); ?></option>
</select>
Remove * from the query and put only required colums. It will improve your query execution time. And also add index on table column on which you are using WHERE clause like timestamp, teacher_id, etc.
It will improve your query performance as fast as required.
I've used Selected="Selected" on SELECT that is fed by values I have written into the code, my query is how can I use it with a dynamic query from the MySQL db?
This is my written code
<label>Fuel</label>
<select tabindex="1" id="proptenure" name="proptenure">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
How can use the above type of line on my Query based SELECT
<?php echo ($searchtenure == '2' ? 'selected' : '')?>
This is an example of how I'm using SELECT from a query
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
So how can I use the Selected="selected"?
This should do the job:
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<?php if($fueltype->PropertyFuelType == 2){ $selected = ' selected="selected"'; }else{ $selected = NULL; }?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"<?php echo $selected; ?>><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
please, i'm somewhat new with php bear with me.
I have a selectbox...
<select name="criteria">
<option value="1">Student ID</option>
<option value="2">Firstname </option>
<option value="3">Lastname</option>
<option value="6">All</option>
</select>
<input name="searchvalue" type="text">
<input name="search" type="submit" value="Search">
When i search with a particular option, after the post request, the selected option usually defaults to Student ID. But i want it to still remain the option i searched with. Any suggestion would be appreciated.
To be honest I'd adjust your select code just a little bit to allow for a bit more dynamic editing..
<?php
$options = array('1'=>'Student ID', '2'=>'Firstname', '3'=>'Lastname', '6'=>'All');
?>
<select name="criteria">
<?php
foreach($options as $key=>$value){
$addtItem = '';
if(isset($_POST['criteria']) && $_POST['criteria']==$key){
$addtItem = 'selected="selected"';
}
echo('<option '.addtItem.' value="'.key.'">'.$value.'</option>');
}
?>
</select>
This way you can quickly add more values without a ton of repeat code..
<select name="criteria">
<option value="1" <?php if ($_POST["criteria"] == 1) echo "SELECTED"; ?>>Student ID</option>
<option value="2" <?php if ($_POST["criteria"] == 2) echo "SELECTED"; ?>>Firstname </option>
<option value="3" <?php if ($_POST["criteria"] == 3) echo "SELECTED"; ?>>Lastname</option>
<option value="6" <?php if ($_POST["criteria"] == 6) echo "SELECTED"; ?>>All</option>
</select>
Really ugly, but that works.
You need to check the $_POST array and select the corresponding select item
<?php
$criteria = isset($_POST['criteria']) ? $_POST['criteria']: 0;
?>
<select name="criteria">
<option value="1" <?php echo ($criteria == '1') ? 'selected="selected"': ''; ?>>Student ID</option>
<option value="2" <?php echo ($criteria == '2') ? 'selected="selected"': ''; ?>>Firstname </option>
<option value="3" <?php echo ($criteria == '3') ? 'selected="selected"': ''; ?>>Lastname</option>
<option value="6" <?php echo ($criteria == '6') ? 'selected="selected"': ''; ?>>All</option>
</select>