Code going crazy not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Code going crazy not working

Post by like_duh44 »

I have the following code echo out the chatrooms in a chat along with the users in the room:

Code: Select all

<?php
/*
if (!eregi("modules.php", $_SERVER['PHP_SELF'])) {
    die ("You can't access this file directly...");
}
*/
$slash = "/";
include("header.php");
include("chatroom".$slash."config.php");
include("chatroom".$slash."chat_themes".$slash."default_theme.php");
$link = mysql_connect($server, $user, $password);
mysql_select_db($database, $link);
echo "<table border="2"><tr><td width="75%">Chatroom</td><td>Users in Room</td></tr>";
$result = mysql_query("SELECT * FROM `".$prefix."chat_rooms`") or die("Room query failed");
while ($row = mysql_fetch_row($result))
{
	$rooms[] = $row;
}
mysql_free_result($result);

if ($rooms != "")
{
	foreach ($rooms as $room)
	{
		$roomid = $room[0];
		$i = 1;
		echo "<tr><td><b/><a href="chatroom/aatachat.php?roomid=".$room[0]."&name=".$room[1]."" target="_blank">".$room[0]." - ".$room[1]."</b></a><br/>$room[2]</td>";
		echo "<td>";
		$sql = "SELECT * FROM `".$prefix."chat_users` WHERE `room_active_".$roomid."` = '1'";
		$userresult = "";
		$userresult = mysql_query($sql) or die("User query failed");
		while ($row = mysql_fetch_row($userresult))
		{
			$users[] = $row;
		}
		$usernum = mysql_num_rows($userresult);
		mysql_free_result($userresult);
		if ($users != "")
		{
			foreach ($users as $user)
			{
				$username = $user[0];

				if ($usernum == 1)
				{
					echo $username;
				}
				else
				{
					if ($i < $usernum)
					{
						echo $username.", ";
					}
					elseif ($i == $usernum)
					{
						echo $username;
					}
				}
				$i++;				
			}
		}
		else
		{
			echo "No users in Room";
		}
		echo "</td></tr>";
	}
}
else
{
	echo "No Chat Rooms available";
}
				

include("footer.php");
?>
This is what it says:
Table number - Table name Doom, Doomer, Doomie
Table description
------------------------------
Table2 number - Table2 name DoomDoomerDoomieDoomier
Table2 description
As you can see, everything is working fine, but for some reason its outputting the names of the previous tables with it. Any clues

Along with this, I would like to ask something. For the mysql queries, I do this:

Code: Select all

<?php
$username = $user[0];
?>
But this should also work:

Code: Select all

<?php
$username = $user['user'];

?>
For some reason this doesnt work. I tried double quotes also, and It shows it on the php website that it should work, but it doesnt. Any ideas? Thanks

Doom out...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I can start by recommending you something...
Instead of

Code: Select all

mysql_fetch_row
you can use:

Code: Select all

while ($row = mysql_fetch_array($result)) {
 // and here...
 echo $row['user'];
 echo $row[0];
}
I just think that would make it abit easier to manage, both by coding it, and by reading the code. Try and return if you dont want to change it, or if the problem persists. ;)
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

I did that, but I'm still getting the same error. If you go to my site http://www.vgapws1.myhost24.com/devcorn ... /index.php you can see the problem with displaying wrong usernames
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

It seems like it might have something to do with the nested foreach() but i'm not sure, just an idea that might help you help me :P
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

Well, I found the problem. The part that selects the code from the mysql database into the $users[] array had to be reset like so:

Code: Select all

<?php

$sql = "SELECT * FROM `".$prefix."chat_users` WHERE `room_active_".$roomid."` = '1'"; 

$userresult = ""; 
$userresult = mysql_query($sql) or die("User query failed"); 

$users = "";  //I had to re-set this variable for it to work

while ($row = mysql_fetch_row($userresult)) 
{ 
   $users[] = $row; 
} 


?>
So, I cant believe that made a difference, I thought that if you re-declared a variable, it re-wrote it, but for an array it might just re-write the first variables and leave the extras in there. Just letting anyone know if they are having the same kind of problem

Doom out...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

By doing:

Code: Select all

$array[] = 'somevalue';
you append to the end of the $array array if it already exists, or create the first entry in it if it doesn't. So unless you reinitalise the array you will continue to add to it, not overwrite it. This can be a real PITA, when it happens within nested loops as it can cause strange results to occur.

BTW, since you are intialising an array, I would probably change

Code: Select all

$users = "";
to

Code: Select all

$users = array();
for clarities sake.

Mac
Post Reply