Ahem, could this be shorter?

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
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Ahem, could this be shorter?

Post by eyespark »

Hi there!

New to the forum and with a problem. Articles on my site are divided in max 3 pages. If I have more than one page, I would like next and previous links (next on the bottom and previous on the top of the article). Here is what I came up with. It works but I would like to know if there is a better way. 8O

Thanks in advance from Slovenia

Code: Select all

<?
if ($pageCount == 2) {
	if (!isset($_GET['page'])) {
	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Next</a>';
	$prev = "";
	} elseif ($_GET['page'] == "1") {
	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'">Previous</a>';
	$next = "";
	}
} elseif ($pageCount == 3) {
	if (!isset($_GET['page'])) {
	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Next</a>';
	$prev = "";
	} elseif ($_GET['page'] == 1) {
	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'">Previous</a>';
	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=2">Next</a>';
	} elseif ($_GET['page'] == "2") {
	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Previous</a>';
	$next = "";
	}
}
?>
Last edited by eyespark on Tue Jan 24, 2006 8:13 am, edited 3 times in total.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

yes if you use a cicle for :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You can set the $prev and $next vars to '' before the if statement. this will save you a couple lines...

Code: Select all

$next = '';
$prev = '';

if ($pageCount == 2) {
    if (!isset($_GET['page'])) 
    	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Next</a>';
    elseif ($_GET['page'] == "1")
    	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'">Previous</a>';
    
} elseif ($pageCount == 3) {
    if (!isset($_GET['page'])){
    	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Next</a>';
    }elseif ($_GET['page'] == 1)
    	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'">Previous</a>';
    	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=2">Next</a>';
    } elseif ($_GET['page'] == "2") {
    	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Previous</a>';
    }
}
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Post by eyespark »

Thank you. For now. But what if there would be 10 or 20 pages? For loop sounds promising. Can somebody help me with that?

Thanks
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Ahem, could this be shorter?

Post by Roja »

Code: Select all

<?
$next = '';
if (!isset($_GET['page'])) // This conditional was the same in both pagecount settings, so do it first. {
	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Next</a>';
	$prev = "";
} else {
	$prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'">Previous</a>';
	if ($pageCount == 3 && $_GET['page'] == "2") {
	    $prev = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=1">Previous</a>';
	}
}

if ($pageCount == 3 && $_GET['page'] == 1) // This conditional was a unique combination, so do it seperately {
	$next = '<a href="index.php?action=article&aid='.$_GET['aid'].'&page=2">Next</a>';
	} 
}
Post Reply