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!
Hey guys I have this awkward function and I am sure the syntax is not right because I get different errors once in a while. For example I get that $count is undefined...How is this possible... The function's goal is to print the users online on each page that is listed. I was just wondering if someone could proof it for me.
function usersOnline($pgcod,$td)
{
$qryuserscount="SELECT * FROM scrusersonline WHERE usersonlineusrip!='1' AND td='$td'";
$userscount=mysql_query($qryuserscount);
while ($row = mysql_fetch_assoc($userscount)) {
$ip= $row['usersonlineusrip'];
$qryuserscount1="SELECT * FROM active_users WHERE ip='$ip'";
$userscount1=mysql_query($qryuserscount1);
$count = mysql_num_rows($userscount1);
}
$pageuserscount=usersCount($pgcod,$td);
echo "$pageuserscount Total viewer(s) online on this page and $count User(s) online on this page";
}
$first_query="SELECT * FROM topic WHERE sd ='1'";
$result=mysql_query($first_query) or die('Error, select query failed');
while ($row = mysql_fetch_assoc($result)) {
$tc= $row['te'];
$td= $row['td'];
echo "<br><a href=\"ce.php?td=$td\">$tc</a>";echo usersOnline(1,$td);
}
Thank you for the advice however I did what you said and $count is still undefined (changed echo to return)...maybe I misunderstood... how could the loop not run that's what I cannot understand. For example if the database returns no results shouldn't $count still be defined just as a blank space?
scarface222 wrote:For example if the database returns no results shouldn't $count still be defined just as a blank space?
Why would it be? There's no code that says that should happen.
PHP isn't psychic: it won't notice that there was a variable left undefined simply because some code didn't run. For all it knows, you planned it that way.
(Except you didn't.)
You got any idea what I should do with regards to this problem the script? Sorry man I am kind of new to php so I have a somewhat limited understanding. I appreciate your help thus far. And also have you ever launched a site before? I asked because you seem experienced. I know its off topic but I am in the process of doing so and would appreciate any advice. I am planning on consulting a lawyer for copyright, trademark, and a disclaimer contract but do not have a final decision for a service provider or ad provider or idea of what I will need for monitoring and maintenance in the future.
What is $count supposed to track? Putting it in the loop there means it will only have the value from the last user. If you want a total number then you should be adding all the counts together: that means setting $count=0 before the loop and adding onto it each time.
What i was meaning to do was for each entry where usersonlineusrip!='1' (does not equal 1 and will be equal to the user's ip) and td=$td (represents unique page) I want to count the total for the page in question. So for each value of td I want the total count of entries not equal to 1. So should I still just set $count equal to 0 before counting the rows? Will that achieve the result I need? Thanks again.
If $count is undefined, it is because the loop did not run. And if the loop did not run, it is probably because the database query returned no records.
Why not set $count = 0; before you run the loop?