Jump to content


Why do I get an error?


4 replies to this topic

#1 Friiks

    Young Padawan

  • Members
  • Pip
  • 56 posts
  • Gender:Male
  • Location:Latvia
  • Interests:Guitar, music, having all sorts of fun...and well enjoying my life (as enjoyable as it can be) as good as I can :D<br /><br />Oh yea, and of course - http://snowmoons.com

Posted 26 November 2006 - 04:46 PM

I found a script which displays users online but when I try to make them separated with comma (so the last one doesn't have one after it) I get this error

Quote

Warning: implode() [function.implode]: Bad arguments. in /home/***/public_html/upload/index.php on line 222
and this is the code I use. I never really used this function so I don't know how to fix it =/
$online = mysql_query($query); #Execute query
$row_online = mysql_fetch_assoc($online); #Grab the users
$users_online = $row_online['username'];
if (isset($row_online['username'])) { #If there is atleast one user online

do { #Do this
implode(", ", $users_online);
} while($row_online = mysql_fetch_assoc($online)); #Until all records are displayed

} else {
echo "There are no members online."; #Inform user that no one is online
}

Thanks, Matt.

#2 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 26 November 2006 - 04:57 PM

You cannot implode a single string, it must be an array. $row_online is an array but not $row_online['username'].
imploding/exploding/anything to do with what you are doing isn't a great way of achieving what you are wanting to honestly. How i would do it is add a comma prefix to all but the first username.(hence not being added to the last).

Something like this...
$i = 0;
while( $row = mysql_fetch_array($query) )
{
	echo ($i == 0) ? $row['username'] : ', ' . $row['username'];
	
	/* Ternary operator, in affect its the same as this:
	# if( $i == 0 )
	# {
	#	  echo $row['username'];
	# }
	# else
	# {
	#	  echo ', ' . $row['username'];
	# }
	*/
   
	$i++;
}

Edited by matthewJ, 26 November 2006 - 04:58 PM.


#3 Friiks

    Young Padawan

  • Members
  • Pip
  • 56 posts
  • Gender:Male
  • Location:Latvia
  • Interests:Guitar, music, having all sorts of fun...and well enjoying my life (as enjoyable as it can be) as good as I can :D<br /><br />Oh yea, and of course - http://snowmoons.com

Posted 26 November 2006 - 05:23 PM

Ok,I think Im using your code wrong too ...
$query = "SELECT * FROM online WHERE timeout > \"$timeout\""; #Check and see who is online in the last 2 minutes

$online = mysql_query($query); #Execute query
$row_online = mysql_fetch_assoc($online); #Grab the users
/*
$users_online = $row_online['username'];
if (isset($row_online['username'])) { #If there is atleast one user online

do { 
implode(", ", $users_online);
} while($row_online = mysql_fetch_assoc($online)); #Until all records are displayed

} else {
echo "There are no members online."; #Inform user that no one is online
}
*/
if (isset($row_online['username'])) {
do{
$i = 0;
while( $row = mysql_fetch_assoc($online) )
{
	echo ($i == 0) ? $row['username'] : ', ' . $row['username'];
	
	/* Ternary operator, in affect its the same as this:
	# if( $i == 0 )
	# {
	#	  echo $row['username'];
	# }
	# else
	# {
	#	  echo ', ' . $row['username'];
	# }
	*/
  
	$i++;
}
}
else {
echo "There are no members online."; #Inform user that no one is online
}
}

This is how I implemented it and this is error I get

Quote

Parse error: syntax error, unexpected T_ELSE, expecting T_WHILE in /home/***/public_html/upload/index.php on line 251
Line 251 is line with else

Edited by Friiks, 26 November 2006 - 05:24 PM.


#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 26 November 2006 - 05:31 PM

<?php

$query = "SELECT * FROM online WHERE timeout > \"$timeout\""; #Check and see who is online in the last 2 minutes

$online = mysql_query($query); #Execute query
$row_online = mysql_fetch_assoc($online); #Grab the users

if (isset($row_online['username'])) {
	$i = 0;
	while($row = mysql_fetch_assoc($online)){
		echo ($i == 0) ? $row['username'] : ', ' . $row['username'];  
		$i++;
	}
} else {
echo "There are no members online."; #Inform user that no one is online
}
?>


#5 Wolfe

    Young Padawan

  • Members
  • Pip
  • 102 posts
  • Gender:Male
  • Location:Louisiana

Posted 27 November 2006 - 06:14 PM

View PostFriiks, on Nov 26 2006, 05:23 PM, said:

Ok,I think Im using your code wrong too ...
$query = "SELECT * FROM online WHERE timeout > \"$timeout\""; #Check and see who is online in the last 2 minutes

$online = mysql_query($query); #Execute query
$row_online = mysql_fetch_assoc($online); #Grab the users
/*
$users_online = $row_online['username'];
if (isset($row_online['username'])) { #If there is atleast one user online

do { 
implode(", ", $users_online);
} while($row_online = mysql_fetch_assoc($online)); #Until all records are displayed

} else {
echo "There are no members online."; #Inform user that no one is online
}
*/
if (isset($row_online['username'])) {
do{
$i = 0;
while( $row = mysql_fetch_assoc($online) )
{
	echo ($i == 0) ? $row['username'] : ', ' . $row['username'];
	
	/* Ternary operator, in affect its the same as this:
	# if( $i == 0 )
	# {
	#	  echo $row['username'];
	# }
	# else
	# {
	#	  echo ', ' . $row['username'];
	# }
	*/
  
	$i++;
}
}
else {
echo "There are no members online."; #Inform user that no one is online
}
}

This is how I implemented it and this is error I get

Quote

Parse error: syntax error, unexpected T_ELSE, expecting T_WHILE in /home/***/public_html/upload/index.php on line 251
Line 251 is line with else

I'm sure Mr. Matt's code will work fine, but the error is caused from inbalanced brackets.

Just add another bracket before the else.

The if, do, and while are being opened and only 2 are being closed. So just add a bracket before the else and that should solve it.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users