Jump to content

[SOLVED] Loop Help Needed


NoSalt

Recommended Posts

Hello All

 

    I am at the end of my rope trying to figure out why this isn't working and I was hoping one of you could help me. Here is what I am trying:

 

<table>
<?php
    for($j=0; $j<sizeof($dataArray){
        if($dataArray[$j]){
?>
<tr class="active_<?php echo ($j%2); ?>"><td class="padRight" nowrap><?php echo $infoArray[$j]; ?>:</td><td class="padLeft"><?php echo $dataArray[$j]; ?></td></tr>
<?php
            $j++
        }
    }
?>
</table>

 

Whenever I load this page my local server pegs out and I have to restart Apache. I really think this should work but is just doesn't. Can someone please tell me what I am doing wrong?

 

Thanks for reading  :)

Link to comment
https://forums.phpfreaks.com/topic/133928-solved-loop-help-needed/
Share on other sites

You need to increment $j within your definition of the for loop, not within the loop itself

<table>
<?php
    for($j=0; $j<sizeof($dataArray);$j++){
        if($dataArray[$j]){
?>
<tr class="active_<?php echo ($j%2); ?>"><td class="padRight" nowrap><?php echo $infoArray[$j]; ?>:</td><td class="padLeft"><?php echo $dataArray[$j]; ?></td></tr>
<?php
        }
    }
?>
</table>

 

Whenever I load this page my local server pegs out and I have to restart Apache. I really think this should work but is just doesn't. Can someone please tell me what I am doing wrong?

 

Thanks for reading  :)

Rats ...

 

You can probably see what I am attempting to do. I want to alternately color the table rows for clear viewing on the page. However, some of the array variables are going to be 'null' and I don't want to increment $j for those variables because I will have two rows in succession be the same color. Any tips for pulling this off?

 

Thanks for reading and replying

<?php
$k = 0;
for($j=0; $j<sizeof($dataArray);$j++){
   if($dataArray[$j]){
      ?>
      <tr class="active_<?php echo ($k%2); ?>"><td class="padRight" nowrap><?php echo $infoArray[$j]; ?>:</td><td class="padLeft"><?php echo $dataArray[$j]; ?></td></tr>
      <?php
      }
      $k++;
   }
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.