Reputation: 371
I have an html select box that is populated by an array from the db. All the data in this table including the select box it written to a new table in the db along with current date. When the user returns to the class I want the select box to default to the last entry the user submitted whether it be the day before or a week before. I can get the query pretty close to where I need it but I dont really where to start as far as passing that to the select box.
Here is my select box that is echo from php.
<td>
<select name="movement[]" width=200>
<option>Select...</option>
<option>'. ($wc['mv_00']). '</option>
<option>'. ($wc['mv_01']). '</option>
<option>'. ($wc['mv_02']). '</option>
<option>'. ($wc['mv_03']). '</option>
<option>'. ($wc['mv_04']). '</option>
</select></td>
Here is the code for the entire array that is echo out.
if(empty($workout_class) === false)
{
foreach($workout_class as $wc){
if ($wc['pagenum'] !== $pagenum) continue 1;
echo '<tr>
<td><input type="hidden" name="client_id[]" value="'.($wc['client_id']).'">
<input type="hidden" name="first_name[]" value="'.($wc['first_name']).'">'. ($wc['first_name']).'
<span><input type="hidden" name="nickname[]" value="'.($wc['nickname']).'">('. ($wc['nickname']).')</span>
<span><input type="hidden" name="last_name[]" value="'.($wc['last_name']).'">'. ($wc['last_name']).'</span>
</td>
<td><input type="hidden" name="order[]" value="'.($wc['order']).'">'. ($wc['order']). '</td>
<td>
<select name="movement[]" width=200>
<option>Select...</option>
<option>'. ($wc['mv_00']). '</option>
<option>'. ($wc['mv_01']). '</option>
<option>'. ($wc['mv_02']). '</option>
<option>'. ($wc['mv_03']). '</option>
<option>'. ($wc['mv_04']). '</option>
</select></td>
<td><input type="hidden" name="rep_set_sec[]" value="'.($wc['rep_set_sec']).'">'. ($wc['rep_set_sec']). '</td>
<td><input type="hidden" name="rest[]" value="'.($wc['rest']).'">'. ($wc['rest']). '</td>
<td>00</td>
</tr>';
} // foreach($data_array
Can this be done with php or is this something where js would be better?
OK, here is my variable that get the last movement entered based on the class, client and order.
$recent_movement = recent_movement($class_id, $client_id, $order);
Here is my query that returns the array of movements from db based on MAX(date)
function recent_movement($class_id, $client_id, $order){
$class_id = (int)$class_id;
$client_id = (int)$client_id;
$query = mysql_query("SELECT `movement` FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `order` = '$order' AND
`date` = (SELECT MAX(`date`) FROM `completed_movements` WHERE `order` = '$order')");
$last_movement = mysql_fetch_array($query);
return $last_movement['movement'];
}
This is where Im not sure how to tell the select menu to set select="selected" based on what my $recent_movement returns.
Upvotes: 0
Views: 116
Reputation: 444
Ok. You split your echo where the tags are. Then , you iterate through them and when you reach the one that you wish to set as default you simply set
<option selected="selected" value="value">Option</option>
In this way this option will be the default one when entering the page. This will work in your case.
I do not recommend using echo to build your page. You should instead rely on a template engine in your application. I recommend Twig Template Engine .
Upvotes: 0
Reputation: 7040
OK, for argument's sake, let's say that you store the latest selection in $recent_movement
. Just compare the value of that variable to the value of the options, and set the matching option to "selected":
<?php
.
.
.
echo '...
<option' . ($ws['mv_00'] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_00']). '</option>
<option' . ($ws['mv_01'] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_01']). '</option>
<option' . ($ws['mv_02'] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_02']). '</option>
<option' . ($ws['mv_03'] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_03']). '</option>
<option' . ($ws['mv_04'] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_04']). '</option>
...';
You can also simplify this by implementing a loop with a counter:
<?php
$options = "";
for($i = 0; $i <= 4; $i++) {
$options .= '<option' . ($ws['mv_0' . $i] == $recent_movement ? ' selected="selected"' : "") . '>'. ($wc['mv_0' . $i]). '</option>';
}
UPDATE:
Had this been my own code, I would have made some minor changes. Instead of that huge echo, I would have just exited PHP mode and gone with straight HTML. I would have also used my looping string constructor (posted above) for the options
:
<?php
foreach ($workout_class as $wc) :
if ($wc['pagenum'] !== $pagenum) continue 1;
$options = "";
for ($i = 0; $i <= 4; $i++) {
$options .= '<option' . ($ws['mv_0' . $i] == $recent_movement ? ' selected="selected"' : "") . '>' . ($wc['mv_0' . $i]) . '</option>';
}
?>
<tr>
<td><input type="hidden" name="client_id[]" value="<?= $wc['client_id'] ?>">
<input type="hidden" name="first_name[]" value="<?= $wc['first_name'] ?>"><?= $wc['first_name'] ?>
<span><input type="hidden" name="nickname[]" value="<?= $wc['nickname'] ?>">(<?= $wc['nickname'] ?>)</span>
<span><input type="hidden" name="last_name[]" value="<?= $wc['last_name'] ?>"><?= $wc['last_name'] ?></span>
</td>
<td><input type="hidden" name="order[]" value="<?= $wc['order'] ?>"><?= $wc['order'] ?></td>
<td>
<select name="movement[]" width=200>
<option>Select...</option>
<?= $options ?>
</select></td>
<td><input type="hidden" name="rep_set_sec[]" value="<?= $wc['rep_set_sec'] ?>"><?= $wc['rep_set_sec'] ?></td>
<td><input type="hidden" name="rest[]" value="<?= $wc['rest'] ?>"><?= $wc['rest'] ?></td>
<td>00</td>
</tr>
<?php endforeach; ?>
Upvotes: 1
Reputation: 17858
if you had
<option>A</option>
<option>B</option>
<option>C</option>
When to pick "B" you would do
<option>A</option>
<option selected="selected">B</option>
<option>C</option>
As a result all you need to do is get the last value you wanted, and then for each output, if the value is the one you wanted add the "selected = ..." part.
(Edited to set value of selected to selected)
Upvotes: 2