Page 1 of 1

Ahem, could this be shorter?

Posted: Tue Jan 24, 2006 7:43 am
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 = "";
	}
}
?>

Posted: Tue Jan 24, 2006 8:05 am
by duk
yes if you use a cicle for :)

Posted: Tue Jan 24, 2006 8:24 am
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>';
    }
}

Posted: Tue Jan 24, 2006 9:34 am
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

Re: Ahem, could this be shorter?

Posted: Tue Jan 24, 2006 9:34 am
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>';
	} 
}