traker
traker

Reputation: 101

How to fix 'select option not selected ' in php

I have a select dropdown where i want to set selected option of select tag

I tried the following code

<select name="project" required class="form-control">
<option value="">Select  Project</option>
 <?php foreach ($this->getProject as $getproject):
                                                    ?>
 <option value="<?=$getproject['p_id'] ?>"<?php $getproject['p_id'] == $this->getdocs[0]['fk_project_id'] ? ' selected="selected"' : ''; ?>><?php echo $this->getdocs[0]['fk_project_id']; ?></option>
 <?php endforeach; ?>
  </select>

Here $getproject['p_id']=1 and $this->getdocs[0]['fk_project_id']=1 .But its not get selected

Upvotes: 0

Views: 1161

Answers (1)

Netlog
Netlog

Reputation: 135

just replace

<?php $getproject['p_id'] == $this->getdocs[0]['fk_project_id'] ? ' selected="selected"' : ''; ?>

with this code:

<?php echo ( ($getproject['p_id'] == $this->getdocs[0]['fk_project_id']) ? ' selected="selected"' : ''); ?>

Upvotes: 1

Related Questions