Page 1 of 1

Problem with IF statement?

Posted: Thu Jul 08, 2010 5:02 am
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

Re: Problem with IF statement?

Posted: Thu Jul 08, 2010 9:11 am
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?

Re: Problem with IF statement?

Posted: Thu Jul 08, 2010 9:54 am
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?

Re: Problem with IF statement?

Posted: Thu Jul 08, 2010 10:46 am
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.

Re: Problem with IF statement?

Posted: Fri Jul 09, 2010 6:34 am
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