This option will reset the home page of this site. Restoring any closed widgets or categories.

Reset

Simple Pagination Using PHP Script

Introduction:

Very simpleĀ  tutorial that shows you how to paginate content from Mysql database in the simplest and most effective way using very simple php script

Step 1 - The Database:

The first step is to create the table to hold the data. Execute the following code in phpMyadmin

Sql Code:

CREATE TABLE `data` (

`id` int(255) NOT NULL auto_increment,

`title` text NULL default '',

`story` text NULL,

`author` varchar(255) NOT NULL default '',

PRIMARY KEY  (`id`),

) ENGINE=MyISAM;

Step 2 - The Script:

The Code:data.php

< ?php
$host = "your_host"; //your sql host, usually 'localhost'
$user = "username"; //username to connect to database
$pass = "password"; //password to connect to database
$db = "your_database"; //the name of the database
mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
mysql_select_db($db) or die("ERROR DB:".mysql_error());
$max = 25; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
//view the news article!
if(isset($_GET['act']) && $_GET['act'] == "view")
{
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM data WHERE id = '$id'");
while($r = mysql_fetch_array($sql))
{
$title = $r['title'];
$story = $r['story'];
$author = $r['author'];
echo "<div><p>$title</p><p>$author</p><p>$story</p>";
}
}else{
//view all the news articles in rows
$sql = mysql_query("SELECT * FROM data LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM data"),0);
//the total number of pages (calculated result), math stuff...
$totalpages = ceil($totalres / $max);
//the table
echo "<table><tr><td>Title</td><td>Author</td></tr><tr>";
while($r = mysql_fetch_array($sql))
{
$id = $r['id'];
$title = $r['title'];
$author = $r['author'];
echo "<td><a href='data.php?act=view&id=$id'>$title</a></td><td>$author</td>";
}
//close up the table
echo "</tr></table>";
for($i = 1; $i < = $totalpages; $i++){
//this is the pagination link
echo "<a href='data.php?p=$i'>$i|";
}
}
?>

Thanks vipercreations

VN:F [1.5.7_846]
Rating: 6.3/10 (3 votes cast)
VN:F [1.5.7_846]
Rating: 0 (from 0 votes)

Related posts:

  1. Simple PHP Login Script
  2. Database Connection And Pagination In PHP Using Symfony Framework
  3. Simple Visitor Counter Using php
  4. Simple Ajax with PHP click tracker
  5. Get Method and Array Manipulation In Symfony
  6. How to run a SQL query in phpMyAdmin
  7. Simple shell script to FTP file from your Desktop to FTP server

Leave a Reply

Comments (required)

Spam Protected