Jump to content

Not displaying page because of for/foreach loops


Xtremer360

Recommended Posts

I know there's something wrong with my code because the page never loads so there's somethign wrong with either the for loop or foreach loop. The topRankings variable is set correct and giving the right value.

 

<?php 
            for ($i = 0; $i >= count($topRankings); $i++){
                foreach($topRankings[$i] as $k=>$v)
                {
                    $seperator = ($elements == $count) ? '' : '<hr />'; 
                    $name = (!isset($row['character_name'])) ? 'TBD' : $row['character_name'];
                    $count++;
                ?>
                    <li><span class="red"><?php echo $name; ?></span></li>
                <?php
                }
            }
            ?>

Link to comment
Share on other sites

>=

 

"There's your problem"

 

 

for (expr1; expr2; expr3)

    statement

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

 

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

 

At the end of each iteration, expr3 is evaluated (executed).

 

Link to comment
Share on other sites

A better question would be why you have that foreach() loop inside the for() loop. As xtopolis stated your problem is the ">=" condition. I think you meant to use "<=". But, you don't need that at all. You need two sets of foreach loops.

 

A couple other things. The ternary operator to determine the name will be the same value on every iteration of the loop since you are not comparing any values that are dynamically determined in the loop. So, you should determine the $name before the loop since doing it on each iteration is inefficient. In fact, you are not using ANY values of the arrays in the loops at all. Very odd. I assume that must be a mistake. Also, in my opinion, you should do "positive" tests in your conditions if it makes sense rather than negative tests. Makes it much easier to read the code. Lastly, the line to determine the separator - it looks like you are trying to put the separator after each line, except the last. It would probably be moire efficient to redefine the values of the array as you loop through them with the output you want and then do an implode. But, your whole output is wrong, so it's hard to really suggest a better method

 

foreach($topRankings as $rank)
{
    foreach($rank as $key => $value)
    {
        //$seperator = ($elements == $count) ? '' : '<hr />'; //Putputput in array and then do an implode
        $name = (isset($row['character_name'])) ? $row['character_name'] : 'TBD';
        echo "<li><span class='red'>{$name}</span></li>\n";
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.