PHP Classes

File: example2.php

Recommend this page to a friend!
  Classes of Usman Didi Khamdani   Easy Paging Class   example2.php   Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: this example is used to obtain a number of data with display limitation into the pages, with $_GET variable(s)
Class: Easy Paging Class
Show navigation links for listings split in pages
Author: By
Last change:
Date: 13 years ago
Size: 3,882 bytes
 

Contents

Class file image Download
<?php

/*
this example is used to obtain a number of data with display limitation into the pages, with $_GET variable(s)
author: usman didi khamdani
author's email: usmankhamdani@gmail.com
author's phone: +6287883919293
*/

?>
<!doctype html>
<html lang="en">
<head>
<title>Paging Class :: Page with $_GET variable(s)</title>
</head>
<body>
<h3>Page with $_GET variable(s)</h3>
<?php

$host
= "localhost"; // database host name
$user = "root"; // database user name
$password = ""; // database user password
$db = "test"; // database name

$host_conn = mysql_connect($host,$user,$password); // connect to host
if(!$host_conn) {
    die(
mysql_error());
}
$db_conn = mysql_select_db($db, $host_conn); // connect to database
if(!$db_conn) {
    die(
mysql_error());
}

if((isset(
$_GET['cat']) && $_GET['cat']!="") && (isset($_GET['auth']) && $_GET['auth']!="")) {

   
// define first, prev, next and last thumbnail
   
define("_FIRST","&lt;&lt; ");
   
define("_PREV","&lt;");
   
define("_NEXT","&gt; ");
   
define("_LAST","&gt;&gt;");

    include(
"paging.class.php");

   
// current page
   
if(isset($_GET['page']) && $_GET['page']!="") {
       
$page = @$_GET['page'];
    } else {
       
$page = 1;
    }

   
// setting params
   
$param1 = $_GET['cat'];
   
$param2 = $_GET['auth'];

   
$param = "cat=$param1&auth=$param2";

   
$query1 = "SELECT no FROM book WHERE Category = '$param1' AND Author = '$param2'";
   
$check = mysql_query($query1);

    if(!
$check) {
        die(
mysql_error());
    }

   
$sum_data = mysql_num_rows($check); // sum of data
   
   
$max_row = 5; // maximum number of rows of data to be displayed in a page
   
$num = 10; // number of page thumbnails

   
$d = new Paging($page,$max_row,$sum_data);

   
// get limit;
   
$query2 = "SELECT * FROM book WHERE Category = '$param1' AND Author = '$param2' ORDER BY Title ".$d->limit();
   
// create thumbnail;
   
$thumbnail = $d->thumbnail($num,$param);

    echo
"<div id=\"display_content\"><p>current page = $page<br />number of page = ".ceil($sum_data/$max_row)."<br />maximum number of rows of data to be displayed in a page = $max_row<br />sum of data = $sum_data<br />number of page thumbnails = $num</p><p>mysql_query = <strong>$query2</strong></p><hr />";

   
$display = mysql_query($query2);

    if(!
$display) {
        die(
mysql_error());
    }

   
$check2 = mysql_num_rows($display);

    if(
$check2==0) {
        echo
"<h3>There's no list of $param1 Books by $param2</h3>";
    } else {
        echo
"<h3>List of $param1 Books by $param2</h3><table id=\"content\"><tr><th>No.</th><th>Title</th><th>Published Year</th></tr>";
       
$no = 0;
        while(
$data = mysql_fetch_array($display)) {
           
$no++;
           
$n = (($page-1)*$max_row)+$no;
            echo
"<tr><td style=\"text-align:right\">$n.</td><td>".$data['Title']."</td><td>".$data['Published_Year']."</td></tr>";
        }

        echo
"</table></div>$thumbnail";
    }

}

?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="get">
<?php

$cat_query
= "SELECT DISTINCT(Category) FROM book ORDER BY Category";
$cat_list = mysql_query($cat_query);

$auth_query = "SELECT DISTINCT(Author) FROM book ORDER BY Author";
$auth_list = mysql_query($auth_query);

if(!
$cat_list || !$auth_list) {
    die(
mysql_error());
}

$cat_sum = mysql_num_rows($cat_list);
$auth_sum = mysql_num_rows($auth_list);

?>
<p>Category <select name="cat"><?php if($cat_sum>0) while($c=mysql_fetch_array($cat_list)) { echo '<option value="'.$c[0].'">'.$c[0].'</option>'; } ?></select> Author <select name="auth"><?php if($auth_sum>0) while($a=mysql_fetch_array($auth_list)) { echo '<option value="'.$a[0].'">'.$a[0].'</option>'; } ?></select> <input type="submit" value="Show Book" /></p>
</form>
<hr />
<p><a href="example1.php">Example 1: Page without $_GET variable plus CSS Rules</a><br />
<a href="example3.php">Example 3: Page with jQuery .load() plus CSS Rules</a></p>
</body>
<html>