MyBB Community Forums

Full Version: My scratch made shoutbox [Help Needed]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've made my own shoutbox but I've got a bit of a problem, it doesn't show all shouts, I've got an alternating row colour function in it, and basically if you have say 3 shouts in the database, only two shouts will display, Here's my shoutbox code, I would love some help with this.

<?php

define("IN_MYBB", 1);
require "./global.php";

if($mybb->input['action'] == "update")
{
$query = $db->query("SELECT uid, avatar, username, message FROM ".TABLE_PREFIX."shoutbox ORDER BY id DESC LIMIT 20");
eval("\$shoutbox_shouts= \"".$templates->get("shoutbox_shouts")."\";");
output_page($shoutbox_shouts);
while($row = mysql_fetch_array($query))
{
// Displays alternate table row colors
function row_color($i){
$bg1 = "#1B1B1B"; // color one
$bg2 = "#0d0d0d"; // color two

if ( $i%2 ) {
return $bg1;
} else {
return $bg2;
}
}
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
for( $i = 0; $i < $row = mysql_fetch_array($query); $i++){
echo "<tr><td width=\"2%\" bgcolor=".row_color($i)."><img src=\"".$row['avatar']."\" height=\"30\" width=\"30\" /></td><td width=\"98%\" bgcolor=".row_color($i)."> <a href=\"".$mybb->settings['bburl']."/member.php?action=profile&uid=".$row['uid']."\">".$row['username']."</a><img src=\"".$theme['imgdir']."/bullet.gif\" />".$row['message']."</td></tr>";
}
echo "</table>";}
}

if($mybb->input['action'] == "insert")
{
$query = $db->query("INSERT INTO ".TABLE_PREFIX."shoutbox(uid, avatar, username, message) VALUES ('".$mybb->user['uid']."', '".$mybb->user['avatar']."', '".$mybb->user['username']."', '".$_POST['message']."');",mysql_real_escape_string(strip_tags($message)));
}
?>

If I remove all the alt colour coding, it displays all the shouts, this is fully working, I just need to keep the alt row colours and display all shouts
That is some really bad and inefficient coding. Why are you declaring a function inside a while loop?? I almost fell off my chair when I saw that!

Anyways, if you want to keep your code the way it is, then I think changing this line should fix it
for( $i = 0; $i < $row = mysql_fetch_array($query); $i++){
TO
for( $i = 0; $i <= $row = mysql_fetch_array($query); $i++){

However if you care about efficiency then think about rewriting the code, heres a quick rewrite:
<?php

define("IN_MYBB", 1);
require "./global.php";

if($mybb->input['action'] == "update")
{
	$query = $db->query("SELECT uid, avatar, username, message FROM ".TABLE_PREFIX."shoutbox ORDER BY id DESC LIMIT 20");
	eval("\$shoutbox_shouts= \"".$templates->get("shoutbox_shouts")."\";");
	output_page($shoutbox_shouts);
	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	$i = 0;
	while($row = mysql_fetch_array($query))
	{
		echo "<tr><td width=\"2%\" bgcolor=".row_color($i)."><img src=\"".$row['avatar']."\" height=\"30\" width=\"30\" /></td><td width=\"98%\" bgcolor=".row_color($i)."> <a href=\"".$mybb->settings['bburl']."/member.php?action=profile&uid=".$row['uid']."\">".$row['username']."</a><img src=\"".$theme['imgdir']."/bullet.gif\" />".$row['message']."</td></tr>";
		$i++;
	}
	echo "</table>";
}

if($mybb->input['action'] == "insert")
{
	$query = $db->query("INSERT INTO ".TABLE_PREFIX."shoutbox(uid, avatar, username, message) VALUES ('".$mybb->user['uid']."', '".$mybb->user['avatar']."', '".$mybb->user['username']."', '".$_POST['message']."');",mysql_real_escape_string(strip_tags($message)));
}

// Displays alternate table row colors
function row_color($i){
	$bg1 = "#1B1B1B"; // color one
	$bg2 = "#0d0d0d"; // color two

	if ( $i%2 ) {
		return $bg1;
	} else {
		return $bg2;
	}
}

?>
Thanks for the help, but I cleaned up the code like you suggested, but not all shouts are displaying.
Jesus. this spam problem neeeds sorting.