Zok
03-04-2009, 01:39 AM
$chan_stats = $vbulletin->db->query_read("SELECT * FROM channel_cache WHERE loc='#totse' OR loc='#zok' OR loc='#halfbaked' LIMIT 0, 10");
$i = 0;
while ($chan = $db->fetch_array($chan_stats)) {
$url_chan = str_replace('#','%23',$chan['loc']);
if ($i%2==0) {$class='alt2';} else {$class='alt1';}
$chan[topic] = htmlspecialchars($chan[topic]);
$topic = '';
foreach(explode(' ',$chan['topic']) as $i=>$t) {
if($i == 0) {
$topic .= Truncate($t);
} else {
$topic .= ' ' . Truncate($t);
}
}
$topic = Truncate($topic,65);
$irc_bits .= '
<tr><td class="'.$class.'">
<div class="smallfont">
<div align="center"><b><a href="http://zb3.zoklet.net/stats2/?loc='.$url_chan.'" title="View more stats" >'.$chan['loc'].'</a></b></div>
<b>Users:</b> '.$chan['user_count'].'<br/>
<b>Messages/minute:</b> '.($chan['line_count']/5).'<br/>
<b>Topic:</b> "'.$topic.'"
<div align="right"><a href="http://widget.mibbit.com/?settings=a27f7346cca1d91393b774f48ad34408&server=irc.slashnet.org&channel='.$url_chan.'">Join the channel</a>
</div>
</td></tr>';
$i++;
}
$db->free_result($chan_stats);
Specifically lines 2,6, and 9.
On line 2 I set a counter ($i) that will get incremented on every iteration of the outermost while loop. It controls the background color for the table row - the <tr> tag.
Now on line 9 I have a foreach loop that I set up to iterate through the words of a string and truncate each of them. This counter happens to have the same name as the counter from line 2 to control background color. PHP is stupid and doesn't set up a new scope for the foreach loop - meaning that when I assign the NEW counter with the same name, it writes OVER the old counter.
Proper scoping would make it so that the $i declared inside the foreach loop would have no bearing on the $i outside of the loop.
Fucking php!
$i = 0;
while ($chan = $db->fetch_array($chan_stats)) {
$url_chan = str_replace('#','%23',$chan['loc']);
if ($i%2==0) {$class='alt2';} else {$class='alt1';}
$chan[topic] = htmlspecialchars($chan[topic]);
$topic = '';
foreach(explode(' ',$chan['topic']) as $i=>$t) {
if($i == 0) {
$topic .= Truncate($t);
} else {
$topic .= ' ' . Truncate($t);
}
}
$topic = Truncate($topic,65);
$irc_bits .= '
<tr><td class="'.$class.'">
<div class="smallfont">
<div align="center"><b><a href="http://zb3.zoklet.net/stats2/?loc='.$url_chan.'" title="View more stats" >'.$chan['loc'].'</a></b></div>
<b>Users:</b> '.$chan['user_count'].'<br/>
<b>Messages/minute:</b> '.($chan['line_count']/5).'<br/>
<b>Topic:</b> "'.$topic.'"
<div align="right"><a href="http://widget.mibbit.com/?settings=a27f7346cca1d91393b774f48ad34408&server=irc.slashnet.org&channel='.$url_chan.'">Join the channel</a>
</div>
</td></tr>';
$i++;
}
$db->free_result($chan_stats);
Specifically lines 2,6, and 9.
On line 2 I set a counter ($i) that will get incremented on every iteration of the outermost while loop. It controls the background color for the table row - the <tr> tag.
Now on line 9 I have a foreach loop that I set up to iterate through the words of a string and truncate each of them. This counter happens to have the same name as the counter from line 2 to control background color. PHP is stupid and doesn't set up a new scope for the foreach loop - meaning that when I assign the NEW counter with the same name, it writes OVER the old counter.
Proper scoping would make it so that the $i declared inside the foreach loop would have no bearing on the $i outside of the loop.
Fucking php!