0

I have this code and the problem i'm having with it is that once i select an option and save it won't remember the selection next time.but the post has been registered in the database with proper values. is there a syntax error that caught your attention? thanks for your help.

Platform : PHP,SQLite

<tr>
<?php 
//location
?>
<td class='tdt' class='tdt'><?php te("Department");?>:</td>
<td>
<select id='locations' name='locations'>
<option value=''><?php te("Select");?></option>
<?php 
    foreach ($locations  as $key=>$location ) {
        $dbid=$location['id']; 
        $itype=$location['name'];
        $s="";
        if (($locations=="$dbid")) $s=" SELECTED "; 
            echo "    <option $s value='$dbid'>$itype</option>\n";
    }
?>
</select>

</td>
</tr>

<tr>
som
  • 4,650
  • 2
  • 21
  • 36
Carter
  • 917
  • 1
  • 8
  • 8
  • Wait.. what? You're checking if `$locations` (which is an array you're iterating THROUGH) is the same as `$dbid` which is a key in a subarray of `$locations`? It'd **never** match. `$locations[0]['id']` would **never** be the same as `$locations`? – h2ooooooo Sep 04 '13 at 11:17
  • Hi thanks for your reply.how would you fix that.thanks – Carter Sep 04 '13 at 11:19
  • Well for starters you're not checking `$_POST` anywhere in this code, so obviously it doesn't care what the POST is? – h2ooooooo Sep 04 '13 at 11:19
  • I'm not sure how to add $_POST into the mix.I've tried different variations.but with the same outcome.Thanks again – Carter Sep 04 '13 at 11:22
  • `if ($_POST["locations"] == $dbid) $s = ' selected="SELECTED"';` ? – h2ooooooo Sep 04 '13 at 11:22
  • Thanks but that didn't work either.still not returing the selected value.appriciate your help. – Carter Sep 04 '13 at 11:25

1 Answers1

0

You may try this (Assuming you have prepared $locations array)

<select id='locations' name='locations'>
    <option value=''><?php te("Select");?></option>
    <?php 
        foreach ($locations  as $key => $location ) {
            $s = $_POST['locations'] == $location['id'] ? 'selected' : '';
            echo "<option $s value='" . $location['id'] . "'>" . $location['name'] . "</option>";
        }
    ?>
</select>
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Hi Sheikh thanks for helping me out.does this array seems functional to you? $locations=$r['locations']; – Carter Sep 04 '13 at 11:38
  • I assumed that, you have an array like `$locations = array('1'=>'loc1', '2'=>'loc2')`. – The Alpha Sep 04 '13 at 11:40
  • I have a table that consist of all locations called"locations (id,name)".once user make a selection it will register in the employee table under locations as an INT.That INT is tied to the table that i have mention at the begining.Thanks Sheikh – Carter Sep 04 '13 at 11:45
  • Then you have to pull that data (locations) from db and supply it to the `foreach` loop, is that you are doing now ? – The Alpha Sep 04 '13 at 11:48
  • yes.Exactly.But i'm not sure how to setup an array for that.this my SQL query $sql="SELECT * FROM locations order by name"; $sth=$dbh->query($sql); $locations=$sth->fetchAll(PDO::FETCH_ASSOC); thanks – Carter Sep 04 '13 at 11:54
  • It's ok, also check [this](http://stackoverflow.com/questions/1519872/pdo-looping-throug-and-printing-fetchall) if you need to compare your code. – The Alpha Sep 04 '13 at 11:58