Xtremer360 Posted October 24, 2011 Share Posted October 24, 2011 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 } } ?> Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 24, 2011 Share Posted October 24, 2011 >= "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). Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 24, 2011 Share Posted October 24, 2011 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"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.