Jump to content

Recommended Posts

I am not getting the correct option loop  selected results for this and I am not sure why. It was working fine when I had it in a while loop but I am separating my html from php and I changed it over to a foreach loop as it is an array.......

 

Where have I gone wrong, Thanks in advance, James


<select id='status'>
  <option>Please Select</option>

  <?php foreach ($options as $option): ?>
  <option value=<?php echo $option['id']>  <?php echo $option['list_value'] ?>  </option>
  <?php endforeach ?>

</select><br/>
// Status select options

$sql_status =   "SELECT
                value_lists.id,
                value_lists.list_value
                FROM
                value_lists
                WHERE
                value_lists.list_name = 'project_status'
                ORDER BY
                value_lists.list_value ASC";

$status_results = mysqli_query($link,$sql_status);
$options = mysqli_fetch_array($status_results);

php 5.3.13 | mysql 5.5.24 | apache 2.2.22

mysqli_fetch_array() return one row at a time in an array consisting of the data from each column.

 

You will need to create an array of all rows first, then loop through that. eg;

 

$options = array();

while ($row = mysqli_fetch_array($status_results)) {
  $options[] = $row;
}

That was a mistake I made at post time. Even with only the one php tag it does not work. I get 4 rows returned in the combo with 2 2 A A instead of Active, complete. On hold, Lost.

 

<select id='status'>

<option>Please Select</option>

 

<?php foreach ($options as $option): ?>

<option> <?php echo $option['list_value'] ?> </option>

<?php endforeach ?>

 

</select><br/>

I have this working now and I have to set 'selected' when displayed. I gave it a good go but I have not got it working. What is wrong with my sel function?

 

Thanks in advance.

<select id='status'>

  <option>Please Select</option>
  <?php while($row_status = mysqli_fetch_assoc($results_status)): ?>
  <?php //if ($row_status['list_value']==$status){$rs = "selected";} else {$rs = "";} ?>
  <option <?php sel($row_status['list_value'],$category)?> value=<?php echo $row_status['list_value']?> >  <?php echo $row_status['list_value'] ?>  </option>

<?php endwhile; ?>

</select>
// Status select options

$sql_status =   "SELECT
                value_lists.id,
                value_lists.list_value
                FROM
                value_lists
                WHERE
                value_lists.list_name = 'project_status'
                ORDER BY
                value_lists.list_value ASC";

$results_status = mysqli_query($link,$sql_status);

function sel($row_status,$status){
    if (is_array($row_status)){
        foreach ($row_status as $optval){
            if ($status == $optval){
                return "selected";
            }
        }
    }
    else if ($status == $row_status){
        return "selected";
    }

    return '';}

 

<option <?php sel($row_status['list_value'],$category)?> value=<?php echo $row_status['list_value']?> >  <?php echo $row_status['list_value'] ?>  </option>

 

sel($row_status['list_value'],$category) returns a value, so you need to echo it.

 

value=<?php echo $row_status['list_value']?> You need quotes around the value (for HTML).

 

So: 

<option <?php echo sel($row_status['list_value'],$category)?> 
value="<?php echo $row_status['list_value'];?>" >  
<?php echo $row_status['list_value'] ?>  </option>
Usually, I use the ID as the value so the database lookup on the submission script uses the primary key.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.