Forum Moderators: coopster

Message Too Old, No Replies

Loop Nesting / For Each - Dreamweaver

nested loops in dreamweaver

         

deaddog

8:17 pm on May 21, 2008 (gmt 0)

10+ Year Member



I am using dreamweaver to make a table. The table uses a record set called rsactionitems. In one td of the table I need to nest data from $row_rsactionitems['action'];

Basically, what I'm after is a nested do while loop around
<?php echo $row_rsactionitems['action']; ?>

or maybe a for each statement?

The code is below, I hope someone can help.


<?php do { ?>
<?php $status = $row_rsactionitems['status']; ?>
<?php

if ($status == 1) {
$color = "#FFFFFF";
}
if ($status == 2) {
$color = "#006600";
}
if ($status == 3) {
$color = "#FF6600";
}
if ($status == 4) {
$color = "#999999";
}
else {
$color == white;
}

echo '<tr style="background:' . $color . '">';
?>
<?php if ($totalRows_rsactionitems > 0) { // Show if recordset not empty ?>
<td><?php echo $num++. "." ?> <a href="/englist/admin/actionitems/edit.php?itemid=<?php echo $row_rsactionitems['itemid'];?>&amp;checkid=<?php echo $row_rsactionitems['checkid']; ?>""style="color: black">Edit</a> <br /></td>
<td><?php echo $row_rsactionitems['date']; ?></td>
<td><?php echo $row_rsactionitems['where']; ?></td>
<td><?php echo $row_rsactionitems['who']; ?></td>
<td><?php echo $row_rsactionitems['card']; ?></td>
<td><?php echo $row_rsactionitems['description']; ?></td>
<td><?php echo $row_rsactionitems['action']; ?> <br />

<a href="/englist/admin/checks/view.php?itemid=<?php echo $row_rsactionitems['itemid'];?>&amp;checkid=<?php echo $row_rsactionitems['checkid']; ?>" style="color: black">All Actions </a></td>
<td><?php echo $row_rsactionitems['reference']; ?></td>
<td><?php echo $row_rsactionitems['area']; ?><br /><?php echo $row_rsactionitems['number']; ?></td>
<td><?php echo $row_rsactionitems['hours']; ?></td>
</tr><?php } // Show if recordset not empty ?>

<?php } while ($row_rsactionitems = mysql_fetch_assoc($rsactionitems)); ?>


</table>

PHP_Chimp

9:10 pm on May 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php do {
$status = $row_rsactionitems['status'];
// change to be an if, [url=http://uk.php.net/manual/en/control-structures.elseif.php]elseif[/url], else loop
// not an if, if, else loop
// my personal preference is [url=http://uk.php.net/manual/en/control-structures.switch.php]switch[/url] for that sort of thing
if ($status == 1) {
$color = "#FFFFFF";
}
elseif ($status == 2) {
$color = "#006600";
}
elseif ($status == 3) {
$color = "#FF6600";
}
elseif ($status == 4) {
$color = "#999999";
}
else {
$color == white;
}
echo '<tr style="background:' . $color . '">';
if ($totalRows_rsactionitems > 0) {
echo '<td>'. $num++ . '.';
echo <<<TABLE
<a href="/englist/admin/actionitems/edit.php?itemid={$row_rsactionitems['itemid']}&amp;checkid={$row_rsactionitems['checkid']}"style="color: black">Edit</a><br />
</td>
<td>{$row_rsactionitems['date']}</td>
<td>{$row_rsactionitems['where']}</td>
<td>{$row_rsactionitems['who']}</td>
<td>{$row_rsactionitems['card']}</td>
<td>{$row_rsactionitems['description']}</td>
TABLE;
//<td>{$row_rsactionitems['action']}<br />
foreach ($row_rsactionitems['action'] as $action) {
echo "<td>$action</td><br />";
}
echo <<<MORETABLE
<a href="/englist/admin/checks/view.php?itemid={$row_rsactionitems['itemid']}&amp;checkid={$row_rsactionitems['checkid']}" style="color: black">All Actions </a></td>
<td>{$row_rsactionitems['reference']}</td>
<td>{$row_rsactionitems['area']}<br />
{$row_rsactionitems['number']}</td>
<td>{$row_rsactionitems['hours']}</td>
</tr>
MORETABLE;
}
while ($row_rsactionitems = mysql_fetch_assoc($rsactionitems));
echo '</table>';

Assuming that $row_rsactionitems['action'] is an array then you can use foreach to go through the array.
I have rewritten the code slightly to get rid of all of the jumping in and out of php (mainly just so that I could read what it was doing, without all the extra ?><php everywhere).
As you have just said "nest" data from that variable I'm not sure if this will work for you. So you may need to give us some more details about that is in that variable.

I would suggest that you stop using dreamweaver to write your code, as the what it produces could only be described as code vomit.

You may have to check through the code to make sure that I didnt erase anything that you actually needed. Apologies if I did.

deaddog

2:58 pm on May 25, 2008 (gmt 0)

10+ Year Member



Thanks for the help. Sorry about the code vomit, :). I'm a PHP paraplegic and Dw is my wheelchair, got to get around somehow.

I put the code in and now I'm getting:
Warning: Invalid argument supplied for foreach() in E:\wamp\www\englist\admin\checks\actionlist.php on line 187.

 <td><?php echo $row_rsactionitems['action']; ?> <br />
187<?php foreach ($row_rsactionitems['action'] as $action) {
echo '<td>$action</td><br />';
} ?>

Each action has its own table row with its own unique actionid. I think this means it's not an array.