I need Help with Pagination

I am new to php programming and I need help paginating a page. I have tried different codes I have found online, yet I have had no luck. Can someone please help me??? Here is the core of my source code.

[code]<?php
include(“admin/includes/config.inc.php”);

mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

$sql=“SELECT * FROM $tbl_name ORDER BY year DESC, month DESC, day DESC”;
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);

?>

<?php while($rows=mysql_fetch_array($result)){ ?> <?php } ?>
Date Title mp3
Download
<? echo $rows['month']; ?>/<? echo $rows['day']; ?>/<? echo $rows['year']; ?>
<? echo $rows['title']; ?>
<? echo $rows['scripture']; ?>
<? echo $rows['ser_series']; ?>
<? echo $rows['speaker']; ?>
Listen
<?php mysql_close(); ?>[/code]

What you’re going to need is the LIMIT clause for a SQL query (see mysql.com for more information on how to select a maximum number of records).
You can use that, and $_GET to define which page you’re on (with a default of page 1).

[code]$numrows = 15; // Number of data rows to show per page

$current_page = $_GET[‘page’];
$sql_offset = ($current_page) * $numrows;

// MySQL connection here

$sql = “SELECT title FROM $table_name”;
$result = mysql_query($sql);
$numrecords = mysql_num_rows($result);
$numpages = ceil($numrecords / $numrows);

$sql = "SELECT * FROM $table_name ORDER BY year DESC, month DESC, day DESC LIMIT 15, ".$sql_offset;
$result = mysql_query($sql);

while ($rows = mysql_fetch_assoc($result)) {
// Frontend code here
}

while ($i = 0; $i < $numpages; $i++) {
// Pagination code here
}
[/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service