...

View Full Version : Pagination for beginners



funnymoney
03-18-2009, 12:20 AM
Easy pagination script i want to share with rest of the world is maybe a first script i wrote that works from start till end :)

So here's the code, i explaind in the comments.

Can't wait to hear your comments!


<?php

//TEST ARRAY $pagination We use for this script
//Can be generated with mysql_fetch_row, or some sort of explode() functions
//even can be flat file
$pagination = array(1 => "Page1", 2 => "Page2", 3 => "Page3", 4 => "Page4", 5 => "Page5");


//simple "preloader" although it has nothing to do with the
//preloading, i just love to use that name, that and ZeroCool..
if (isset($_GET['id'])) {
$id =$_GET['id'];
if ($id > count($pagination)) {
$id = 1;
}
}
else {
$id = 1;
}

///
///Some random HTML file used to generate output..
///we could call that pagination.html

$page = file_get_contents("paginationtest.html");

### paginationtest.html ###
/*
<style type="text/css">
#site {
width: 400px;
}
#content {
font-size: 20px;
width: 380px;
margin: 0 auto;
background: #cccccc;
}
#links {
text-align: center;
}
#links p.left {
width: 190px;
float: left;
text-align: right;
text-indent: 10px;
}
#links p.right {
width: 190px;
float: left;
text-align: left;
text-indent: 10px;
}
</style>
<div id="site">
<div id="content">
%content%

<div id="links">
<p class="left">%left%</p><p class="right">%right%</p>
</div>
</div>
</div> */
#############################

//just a variable to MATCH content with
$MATCHcontent = "/%content%/";
//real content from VALUE of pagination array
$VALUEcontent = $pagination[$id];
//preg_replace to switch MATCH with real VALUES for CONTENT
$page = preg_replace($MATCHcontent, $VALUEcontent, $page);

//preloader function as above, just this one is used to
//generade $_GET['id'] variable
$left = $id-1;
if ($left<=0) {
$left = "";
}
else {
//and to asign some fine text so we could
//generate links
$left = '<a href="?id='.$left.'">LEFT</a>';;
}
//preloader function as above but for oposite
//getting next button
$right = $id+1;
$max = count($pagination);
$right = $id+1;
if ($right>$max) {
$right = "";
}
else {
$right = '<a href="?id='.$right.'">RIGHT</a>';;
}
//and similar to above just this time array to spare some typing
$MATCHleft = ("/%left%/");
$MATCHright = ("/%right%/");

$page = preg_replace (array($MATCHleft, $MATCHright), array($left, $right), $page);
##page variable should have some paggination
print $page;


?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum