I'm going to try to tell you what I'm about to do with this code.
I'm creating a table with in de first column the names of the users followed by the amount of days in the current month (each day in a column). Now I'm trying to create select options in each row/column to choose a value from.
Here's my code:
<table width="100%" cellspacing="0" cellpadding="0" class="addtable">
<tr>
<td class="tabletitle">Personeelslid</td>
<?php
do{
echo "<td class=\"tabletitle\">".$i."</td>";
$i++;
} while ($i<=$dagen);
?>
</tr>
<?php
while($row_pers = $mysql->fetch_array($result_pers))
{
$j=1;
if ($color == _COLOR_WHITE){
$color = _COLOR_GREY;
} else {
$color = _COLOR_WHITE;
}
echo "<tr class=\"trborder\">";
echo "<td bgcolor=\"".$color."\">".$row_pers['pers_naam']."</td>";
while ($j<=$dagen){
echo "<td bgcolor=\"".$color."\">";
?>
<select name="naam_id" id="naam_id">
<option value="test">test</option>
<?php
while($row_shiften = $mysql->fetch_array($result_shiften))
{
echo "<option value=\"".$row_shiften['shift_code']."\">".$row_shiften['shift_code']."</option>";
}
?>
</select>
<?php
echo "</td>";
$j++;
}
echo "</tr>";
}
?>
</table>
The current result gives my all I want except the select options are only showing up on 1 place. Is it possible to cache it or rewrite my code to make it work?
Thanks in advace
You can store the options output inside of a string, and generate that once. Then reference the stored options for each row in the output table:
<?php
while($row_shiften = $mysql->fetch_array($result_shiften))
{
$options += "<option value=\"".$row_shiften['shift_code']."\">".$row_shiften['shift_code']."</option>";
}
...
while ($j<=$dagen){
echo "<td bgcolor=\"".$color."\">";
?>
<select name="naam_id" id="naam_id">
<option value="test">test</option>
// add in the options generated once
<?php echo $options; ?>
</select>
<?php
echo "</td>";
$j++;
}
Also, you will not want all your <select>
to have the same id, so you should append some sort of unique suffix.
We probably will need more code, for example: table structure and queties. It looks like if you are trying to combine data from two tables, and it is much more common to do that with a JOIN query. (e.g. http://mysqljoin.com/ a site dedicated to this topic entirely)
Good luck!
Well you have "used up" your $result_shiften so you can't use it anymore. You need to copy it to another var, somewhere at the top of your script:
$options = "";
while($row_shiften = $mysql->fetch_array($result_shiften))
{
$options += "\m<option value=\"".$row_shiften['shift_code']."\">".$row_shiften['shift_code']."</option>";
}
then just use echo $options underneath
<option value="test">test</option>
<?php echo $options; ?>
...etc
I'm not sure if I'm following your request properly, but it looks like you need a more unique name/ID for the SELECT entities. Right now you have a variable number of SELECT tags all using the same "name" and "id" ("naam_id").
Change those to variables based on the result from your $row_pers
result. Alternatively, you can use $j
, but that may not help you once you've submitted the form and are looking to process the inputs.