CodeIgniter Forums
no output display in dropdown list - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: no output display in dropdown list (/thread-71923.html)



no output display in dropdown list - kvanaraj - 10-11-2018

Code:
<select name="staff" id="staff" required>
<option value="">Select Staff</option>
<?php

while($row_subject = mysql_fetch_array($query_subject)) {
?>
<option value="<?php echo $row_subject['deo_code'];?>  
($this->input->post('staff') == $row_subject['deo_code'] ? ' selected' : '') ><?php echo $row_subject['deo_code']."-".$row_subject['deo_name'];?></option>
<?php } ?>
</select>



RE: no output display in dropdown list - Piotr - 10-11-2018

Code:
<select name="staff" id="staff" required>
<option value="">Select Staff</option>

<?php
while($row_subject = mysql_fetch_array($query_subject))
{
    $sel = '';
    if($this->input->post('staff') == $row_subject['deo_code']) {
        $sel = 'selected';
    }
    echo '<option value="'.$row_subject['deo_code'].'" '.$sel.'>'.$row_subject['deo_code']."-".$row_subject['deo_name'].'</option>';
}
?>
</select>

1) If you dont know how to use a ? b : c use if first
2) Why you use four times <?php when you can use one?


RE: no output display in dropdown list - InsiteFX - 10-11-2018

I think he needs to look at his php code better.


RE: no output display in dropdown list - dave friend - 10-11-2018

(10-11-2018, 08:20 AM)Piotr Wrote: 2) Why you use four times <?php when you can use one?

Because for outputting text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print. The larger the block of text the more significant the difference.

Because string concatenation creates a large number of temporary objects causing unnecessary use of the garbage collector. Both consume memory and can dramatically slow down script execution. Doing concatenation in loops seriously compounds these effects.

Because there is zero performance hit for dropping into and out of PHP parsing mode.

Because it's easier to write complex output without creating a lot of HTML and/or PHP syntax errors.

It is, more often than not, easier to comprehend. IOW, it's easier to read. Which is probably the most important reason.


RE: no output display in dropdown list - Piotr - 10-11-2018

He don't write server optimized for 1 million users, he generate list of options.
Allow him to learn how to write clear code after that he gonna be think about optimization..

For you writing four times <?php tag is better. Good.. its your opinion and this is for you. For me especially in this case better is make this at once.


RE: no output display in dropdown list - dave friend - 10-11-2018

(10-11-2018, 04:35 AM)kvanaraj Wrote:
Code:
<select name="staff" id="staff" required>
<option value="">Select Staff</option>
<?php

while($row_subject = mysql_fetch_array($query_subject)) {
?>
<option value="<?php echo $row_subject['deo_code'];?>  
($this->input->post('staff') == $row_subject['deo_code'] ? ' selected' : '') ><?php echo $row_subject['deo_code']."-".$row_subject['deo_name'];?></option>
<?php } ?>
</select>

I believe the problem is you don't actually do anything with the ternary statement.
Maybe the code that follows will work.

Note that I'm using PHP Alternative Syntax for control structures and the shorhand syntax for echo, e.g.<?php echo $something; is the same as <?= $something;

PHP Code:
<select name="staff" id="staff" required>
 
   <option value="">Select Staff</option>
 
   <?php
    while 
($row_subject mysql_fetch_array($query_subject)) :
 
       $sel $this->input->post('staff') == $row_subject['deo_code'] ? ' selected' NULL;
 
       ?>
        <option value='<?= $row_subject['deo_code']; ?>'
        <?php
        if
(isset($sel))
 
       {
 
           echo $sel;
 
       }
 
       echo $row_subject['deo_code']."-".$row_subject['deo_name'];
 
       ?>
    </option>
<?php endwhile; ?>
</select> 

My questions are:
Why are you using mysql_fetch_array() in a CodeIgniter project?
Do you know that the MySQL extension of PHP is deprecated? mysql_fetch_array() is from that extension.
Why are you making database calls in a view? These should be happening in a model utilized in a controller.
Do you know it's poor practice to call a function in a loop as you do with while($row_subject = mysql_fetch_array($query_subject)) ?


RE: no output display in dropdown list - dave friend - 10-11-2018

Here's an even more fun way to do it. People often forget that echo will output a comma-separated list of arguments.

I put each argument on a separate line in this example just to illustrate the technique. It would not have to be done that way and might make more sense (be easier to understand) if formatted differently.

PHP Code:
<select name="staff" id="staff" required>
    <
option value="">Select Staff</option>
    <?
php while ($row_subject mysql_fetch_array($query_subject))
    {
        echo 
"<option value='",
        
$row_subject['deo_code'],
        
"'",
        
$this->input->post('staff') == $row_subject['deo_code'] ? ' selected' NULL,
        
$row_subject['deo_code'],
        
"-",
        
$row_subject['deo_name'],
        
"</option>";
     }
?>
</select>