开发者

A count for loop

开发者 https://www.devze.com 2023-01-07 21:54 出处:网络
I have tried everything to get this to work and just cant get the count right. <?php $total = count($u);

I have tried everything to get this to work and just cant get the count right.

<?php
$total = count($u);
for ($i = 0; $i < $total; $i++) {
    $j = 0;
    $s = $database->checkUserPlayedRecent($u[$i]);
    if (mysql_num_rows($s) > 0){
        $j = $j + 1;
        ?>
        <tr>
            <?php echo "<td>$j</td>"; ?>
            <?php echo "<td>$u[$i]</td>"; ?>
            <?php echo "<td>$p[$i]</td>"; ?>
        </tr>
        <?
    }
}
?>

I am tr开发者_JAVA百科ying to increment $j each time but ONLY for the results from num_rows. Originally $j was $i + 1 but that wont work as I am not showing every result from the for loop, only those with num_rows returned. Any help here? Cheers


 $total = count($u);
                    for ($i = 0; $i < $total; $i++) 
                    {
                        $j = 0;

$j assignment is inside the loop, so it's set to 0 every time. Just move it to right before the loop.

 $total = count($u);
                    $j = 0;
                    for ($i = 0; $i < $total; $i++) 
                    {


I would change your inner if to this:

                    if (mysql_num_rows($s) > 0)
                    {
                        $j = $j + 1;
                        ?>
                        <tr>
                                <?php
                                    echo "<td>$j</td>";
                                ?>

                                <?php
                                    echo "<td>$u[$i]</td>";
                                ?>
                                <?php
                                    echo "<td>$p[$i]</td>";
                                ?>
                        </tr>
                        <?
                    } else { // if no rows returned, hasn't played recently
                        echo "<tr><td>Hasn't Played</td></tr>";
                   }

At the moment, you're not accounting for no rows returned. Also, you may be able to avoid all the in-and-out of PHP by echoing or printing the whole thing in one shot. It's predictable enough that would be pretty simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消