Problem with IF statement?

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
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Problem with IF statement?

Post by Vaughanjb »

Hi guys,

A fairly new to php and have come up with a problem during my code.
The code gets events from my database and filters then depending on the 'type'. The problem i'm having is that if the results come back zero. The page doesn't appear. I understand there needs to be an IF statement in there somewhere but each time I have tried I have come across no success.

Code: Select all

<?
require("connection.php");
if(!(isset($_GET['page']))){
	$page = 1;
} else { 
	$page = $_GET['page'];
}

$type = $_GET['type'];
$result = mysql_query("SELECT * FROM events WHERE type LIKE '%$type%' AND date_order >= CURDATE()  ORDER BY date_order");

$rows = mysql_num_rows($result);
$limit = 3;
$last = ceil($rows/$limit);
if ($page < 1) {
	$page = 1;
} elseif ($page >$last) {
	$page = $last;
}

$max = "LIMIT " . ($page - 1) * $limit . "," . $limit;
$result = mysql_query("SELECT * FROM events WHERE  type LIKE '%$type%' AND date_order >= CURDATE()  ORDER BY date_order $max") or die();

?>
If anyone has any help, it would be much appreciated
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Re: Problem with IF statement?

Post by Skiddles2010 »

The problem i'm having is that if the results come back zero. The page doesn't appear.
What does appear? Completely blank page? Error message? What's the url of the resulting page?
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: Problem with IF statement?

Post by Vaughanjb »

Thanks for the response, Sorry its unclear
What does appear?
Completely blank page
Error message?
No error messages at all.
What's the url of the resulting page?
It stays on this page, the page I want it to be. But it seems wherever I enter the IF statement. It just breaks.

I just need a statement that says( IF $rows = 0; echo "No Events" ) in the body.

Any ideas?
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Re: Problem with IF statement?

Post by Skiddles2010 »

I don't have anything to test this on here at work... but did you try putting the if statement directly after obtaining mysql_num_rows()? Seems like the most logical solution to me.

Code: Select all

$rows = mysql_num_rows($result);
If ($rows != 0)
// do all this stuff...

$limit = 3;
$last = ceil($rows/$limit);
if ($page < 1) {
        $page = 1;
} elseif ($page >$last) {
        $page = $last;
}

$max = "LIMIT " . ($page - 1) * $limit . "," . $limit;
$result = mysql_query("SELECT * FROM events WHERE  type LIKE '%$type%' AND date_order >= CURDATE()  ORDER BY date_order $max") or die();
}

else {
   // got nothing
   echo "No Events";
}
I suspect that this line...

Code: Select all

$result = mysql_query("SELECT * FROM events WHERE  type LIKE '%$type%' AND date_order >= CURDATE()  ORDER BY date_order $max") or die();
...could be what's causing your blank screen. Try adding "mysql_error()" to the "or die()" syntax to get an error description.
Vaughanjb
Forum Commoner
Posts: 26
Joined: Thu Jul 08, 2010 4:56 am

Re: Problem with IF statement?

Post by Vaughanjb »

Skiddles2010. Thanks for your advice. Made me look at the code differently.
Managed to fix the problem now. Just by adding this further down the code in the body.

Code: Select all

<? if ($page < 1){
	echo "There are no events in the <strong>" .$type. "</strong> section currently. <br/> Please try another.";
}else{ ?>
Thanks for all you help. :D
Post Reply