Log in

View Full Version : Resolved blank screen when no requests!



liamallan
04-12-2010, 07:21 PM
on my friends request screen, when i have no new friend requests, it echoes a blank screen, i have tried tirelessly to insert an 'else' statement to echo 'You have no new friend requests' but failed everytime.

i was wondering if u guys could help?
this is newfriends.php:

<?
include("include/session.php");
session_start(); // starts sessions
include "config.php"; // inlcudes config

if ($session->logged_in) { //checks user is logged in
switch ($_GET[friends]) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{

echo ( "<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]" ); //displays requests and shows accept delete links
}
break;


case 'accept': //accept page
if ($_GET[user]) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$session->username') "); // add to your friends list
$add2 = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$session->username' , '$_GET[user]') "); // fix friend bug
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]'"); // deletes friend request
echo ( "$_GET[user] has been added as a friend! [<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>] [<a href='newfriends.php'>More Requests?</a>] [<a href='index.php'>Home</a>]" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been Ignored!
[<a href='index.php'>Home</a>] " ); // echos completion
}
break; //ends delete page
} // ends switch
} else {
echo ( "You need to be logged in!" ); // not logged in
}
?>
any help would be appreciated! thanx

djr33
04-12-2010, 07:48 PM
Is the "blank screen" due to no output or to an error?
Sometimes PHP will not reach the end of the code and not display ANYTHING.

Add to the end of your code echo 'it works'; so that you can see if it is working, or if there is a problem with the PHP. If it gets all the way to the end, then it is time to debug the script specifically; if not, there is a bigger problem.

liamallan
04-12-2010, 07:57 PM
yeah, echoed 'it works' with no problem

djr33
04-13-2010, 12:22 AM
What are all of these actions? I mean, what is $_GET['friends'] and in which case(s) is it not working? It's hard to fix a lot of code with little information.

Where do you want to insert "you have no friends"?


The switch statement is hard to read. Usually if statements are simpler and easier to use unless the switch is simple. Switches get VERY messy once there is a lot going on inside them.

Also, in a switch statement the default is supposed to go at the end of everything. Maybe that's part of it? I don't know if it must go at the end, but I know that it can go at the end-- case, case, ...., default

liamallan
04-13-2010, 01:22 AM
the code works fine, only that there is nothing to echo 'no requests' if the user has no new requests. this section below echoes the friend request and accept, delete and view profile links:

while ($reqs = mysql_fetch_array($get))
{

echo ( "<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]" ); //displays requests and shows accept delete links
}
break;
i was thinking the else statement would be in this section of code, i dont know if it would be before the break or after.

Nile
04-13-2010, 01:28 AM
Would mysql_num_rows work?

if(mysql_num_rows($query) <= 0)
Or would it be


[PHP]if(mysql_num_rows($query) <= -1)
djr33 I need your help... haha

djr33
04-13-2010, 03:00 AM
if (mysql_num_rows($query)==0)
http://www.php.net/manual/en/function.mysql-num-rows.php

It returns false on failure. This means if == 0/False, so that should work. Alternatively, if (mysql_num_rows($query)>=1) or >0, depending on what you want to do.

liamallan
04-13-2010, 11:59 AM
thanx for that guys it now echoes 'you have no new friend requests' if a user has none, but now i get a blank error screen if a user has a request.
this is the code i have now:

while ($reqs = mysql_fetch_array($get))

$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' ");
if (mysql_num_rows($get)==0){
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>You have no new friend requests");
}
else{
echo ( "<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]" ); //displays requests and shows accept delete links
}
break;
considering what you said earlier daniel, there must be something wrong in the code between echoing 'you have no new friend requests' and echoing the actual friend request.
any ideas?

djr33
04-13-2010, 05:52 PM
if (...) { //this works }
else { //this does not work }

That doesn't make sense. What if you replace the echo with echo 'test'; to be sure there is no problem with the contents of that echo.

liamallan
04-13-2010, 06:15 PM
it cant be the echo, still getting blank error screen after changing to test.

this is the full code:

<?
if ($session->logged_in) { //checks user is logged in
switch ($_GET[friends]) { //allows multiple pages
default:
$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' "); //gets requests
while ($reqs = mysql_fetch_array($get))

$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' ");
if (mysql_num_rows($get)==0){
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>You have no new friend requests");
}
else{
echo ( "<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]" ); //displays requests and shows accept delete links
}
break;


case 'accept': //accept page
if ($_GET[user]) { //get username

$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$session->username') "); // add to your friends list
$add2 = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$session->username' , '$_GET[user]') "); // fix friend bug
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]'"); // deletes friend request
echo ( "$_GET[user] has been added as a friend! [<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>] [<a href='newfriends.php'>More Requests?</a>] [<a href='index.php'>Home</a>]" ); // echos the completion
}
break; //ends accept page

case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been Ignored!
[<a href='index.php'>Home</a>] " ); // echos completion
}
break; //ends delete page
} // ends switch
} else {
echo ( "You need to be logged in!" ); // not logged in
}
?>

liamallan
04-13-2010, 07:08 PM
i finally managed to get it to echo the request, BUT, there are hundreds of the same request!

this is my code so far:

$get = mysql_query("SELECT * FROM `friend_requests` WHERE `username` = '$session->username' ");
if (mysql_num_rows($get)==0){
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>You have no new friend requests");
}else{
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]"); //displays requests and shows accept delete links
}
}
break;
Any ideas why this is happening? have i created some kind of loop or something?

liamallan
04-13-2010, 09:03 PM
i got the request echo loop thing sorted, now it wont echo ' you have no new requests'!
this is severely hurting my brain lol
this the code i got now:

if (mysql_num_rows($get)==0){
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>You have no new friend requests");
}else{
echo ("<font color='black' face='Arial'><u><b>Friend Requests</b></u></font><br><br>
<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]"); //displays requests and shows accept delete links
}
break;
}
can anyone help me plz?

liamallan
04-14-2010, 01:11 AM
i been working on this all day now, just cant seem to get it to work! it is now echoing the request no problem, its just the echoing of 'you have no new requests' bit!

has anyone got any ideas, or can you help? thanx

djr33
04-14-2010, 02:15 AM
I don't understand what is wrong here. the IF works, but the ELSE does not? That doesn't make any sense.

The way to debug this is to take the code apart piece by piece-- start with the switch-- remove that and find out what's really going on.

Alternatively (probably easier) you can track the variables. Run the script and echo all of the variables when you can to see what they are. For example, start with $_GET['friends'] and see what happens in all cases. Then try another variable.

liamallan
04-14-2010, 08:33 AM
i tried echoing the variables, but no value was returned and the script worked as normal.
i also tried removing the switch, which resulted in a blank error screen.

i dont know if this will help, but i got the script from:RMB-Scripting (http://rmb-scripting.com/tutorials.php?tutorial&tid=282&page=1)

djr33
04-14-2010, 05:11 PM
This is why working with existing (broken) scripts is hard. Sometimes it's easier to just start over.
The only way to figure it out from here is to test different versions-- find where the code is broken. Something doesn't make sense with the data, so that should be the best way to find out what's wrong-- some variable, somewhere, is not what you expect and things aren't working. I can't see anything wrong with the code-- it must be in the logic/organization of information.

liamallan
04-15-2010, 07:42 PM
i finally sussed it, i took on what you said about writing my own script so i started writing my own using the original as a template, rearranged the friend request section and it worked.

this is how the working version looks:

$get = mysql_query( "SELECT * FROM `friend_requests` WHERE `username` = '$session->username' "); //gets requests
while ($reqs = mysql_fetch_array($get))
{
echo ( "<font color='#003399' face='Arial'><b>$reqs[by]</b></font><b></b> wants to be friends with you!<br>
[<a href='newfriends.php?friends=accept&user=$reqs[by]'>Accept</a>]
[<a href='newfriends.php?friends=delete&user=$reqs[by]'>Ignore</a>]
[<a href='userinfo.php?user=$reqs[by]' target='blank'>View Profile</a>]<br><br>" );
}
if (mysql_num_rows($get)==0){
echo ("You have no new friend requests");
}
break;
thanx for all your help daniel :D and sorry for being a pest lol :p