Hi there,
I'm attempting to write a pagination script. So far i've got it working - sorta..
It will work, to an extent, this being that instead of displaying say.. 1 [2] 3 etc.. it will display 0 1 2, when it should only display 1 and 2. hope this makes sense.
Anyway, here is my code:
// set number of results per page
$per_page = 2;
// get or set position to start
$pos = $_GET['pos'];
if ($pos == "") {
$pos = "0";
}
// get total number of rows
$query = " SELECT * FROM news";
mysql_query($query);
$num_rows = mysql_affected_rows();
$totalrow = mysql_query("select * from news") or die (mysql_error());
$total = mysql_num_rows($totalrow);
$total = $total/$per_page;
if ($total%2) {
$total = round($total);
} else {
$total = $total-1;
$total = round($total);
}
if ($pos == 0) {
$query = " SELECT * FROM news LIMIT $pos,$per_page";
} elseif ($pos == 1) {
$ppos = $pos;
$ppos = $ppos + 1;
$query = " SELECT * FROM news LIMIT $ppos,$per_page";
//echo " SELECT * FROM news LIMIT $ppos,$per_page";
} else {
$ppos = $pos;
$ppos = $ppos + 2;
$query = " SELECT * FROM news LIMIT $ppos,$per_page";
//echo " SELECT * FROM news LIMIT $ppos,$per_page";
}
$result = mysql_query($query) or die(mysql_error());
$affected_rows = mysql_affected_rows();
// ##### Query result code goes here #####
for ($i = 0; $i <= $total; $i++) {
if ($i == $pos) {
echo "[".$i."]";
} else {
echo " <a href=\"index.php?action=asdf&pos=$i\">".$i."</a> ";
}
}
Thanks for any help 🙂