Welcome Guest, Not a member yet? Register   Sign In
no output display in dropdown list
#1

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>
Reply
#2

(This post was last modified: 10-11-2018, 07:33 AM by Piotr.)

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?
Reply
#3

I think he needs to look at his php code better.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(10-11-2018, 07: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.
Reply
#5

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.
Reply
#6

(10-11-2018, 03: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)) ?
Reply
#7

(This post was last modified: 10-11-2018, 11:10 AM by dave friend.)

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> 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB