Query in codeigniter view very slow - php

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.

Related

how to use multi select in array

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 } ?>

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

Mark an option as selected from dynamic list of options

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>

Retrive select option from database php

The below code show the retrived data from database and it also repeat the same option
For example: if you select employed from dropdown, it repeat the same employed twice in dropdown
<select class="txtbox" name="currentstatus">
<option value="<?php echo "$currentstatus"; ?>"><?php echo "$currentstatus"; ?></option>
<option value="Employed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Employed") echo "selected"; ?>>Employed</option>
<option value="Unemployed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Unemployed") echo "selected"; ?>>Unemployed</option>
</select>
What about this? This displays two options and selects one according to the database or by $_POST:
<select class="txtbox" name="currentstatus">
<?php foreach (array('Employed', 'Unemployed') as $status): ?>
<option value="<?php echo $status; ?>"<?php if ($currentstatus == $status || (isset($_POST["currentstatus"]) && $_POST["currentstatus"] == $status)) echo ' selected'?>><?php echo $status; ?></option>
<?php endforeach; ?>
</select>

Highlighting multiple selections on a form after submitting

This select box below does remember and highlight -one- selection after submitting the form. But when i make it multiple, it doesn't highlight any of the selectionS after submitting.
Any idea about how to achieve this?
Thanks in advance.
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if ($no_way == 'all') { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if ($no_way == $option) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
$_GET['no_way'] only handles single parameters you have to use $_GET['no_way[]'] and in_array($option, $no_way)
This works for me:
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if (in_array("all",$no_way)) { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if (in_array($option,$no_way)) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
I'm not sure if this will help or not, but is there any chance you've tried just using selected instead of selected="selected"?
<option <?php if ($no_way == $option) { ?> selected<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option>

Categories