Solved

Installing a php PAGINATION SCRIPT

Posted on 2008-06-23
12
1,242 Views
Last Modified: 2013-12-13
I would like to know how to install this script and also if this script is correct for my website.  I don't know much about php but I would like an easy way to go from page to page.  Here's the scrite I think I need:

 
<h1>PAGINATION SCRIPT BY <a href="http://www.webune.com">WWW.WEBUNE.COM</a></h1>

<?

  $num_of_items = 200;

  $items_per_page = 9;

  $max_links = 10;

  if($_GET['page_num']){

  $page_num = $_GET['page_num'];

  } else {

  $page_num = 1;

  }

  ?>

<h1>PAGE: <? echo $page_num; ?></h1>

<strong>example:</strong> <br>

<strong>number of items</strong> = <? echo $num_of_items; ?><br>

<strong>number of items per page</strong> = <? echo $items_per_page; ?><br>

<strong>number of links per page</strong> = <? echo $max_links; ?><br>

<?

  ############################################ PAGINATION FUNCTION ########################################

  ############################################ PAGINATION FUNCTION ########################################

  function pagination_link($id, $page_num)

  {

  

  return $_SERVER['PHP_SELF'].'?page_num='.$page_num;

  }

  function pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)

  {

  $total_pages = ceil($num_of_items/$items_per_page);

  if($page_num) {

  if($page_num >1){ 

  $prev = ' &nbsp; <a href="'.pagination_link($id, ($page_num -1 )).'">&lt; PREV</a> &nbsp; '; 

  $first = '<a href="'.$_SERVER['PHP_SELF'].'">&lt;&lt;</a>'; 

  }

  }

  if($page_num <$total_pages){ 

  $next = ' &nbsp; <a href="'.pagination_link($id, ($page_num+1)).'">NEXT &gt;</a> &nbsp; '; 

  $last = ' &nbsp; <a href="'.pagination_link($id, $total_pages).'">&gt;&gt;</a> &nbsp; ';

  }

  echo $first;

  echo $prev;

  $loop = 0;

  if($page_num >= $max_links) {

  $page_counter = ceil($page_num - ($max_links-1));

  } else {

  $page_counter = 1;

  }

  

  if($total_pages < $max_links){

  $max_links = $total_pages;

  }

  do { 

  if($page_counter == $page_num) {

  echo ' &nbsp; <strong>'.$page_counter.'</strong> &nbsp; '; 

  } else {

  echo '<a href="'.pagination_link($id, ($page_counter)).'">'.$page_counter.'</a> &nbsp; ';

  } 

  $page_counter++; $current_page=($page_counter+1);

  $loop++;

  } while ($max_links > $loop);

  echo $next;

  echo $last;

  }
 

?>

<div align="center">

<?

  pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)

  ?> 

</div><br>

<br>

<div align="center"><a href="http://www.webune.com">PHP/MYSQL WEB HOSTING<br>

<img src="http://www.webune.com/images/headers/default_logo.jpg" border="0"></a></div>

Open in new window

0
Comment
Question by:Freejoy
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 1

Accepted Solution

by:
oku86 earned 168 total points
ID: 21844686
Pagination works best using a database driven website - i've attached some code that is alot less confusing to look at then what you have posted :)

First of all this code will show 25 records per page using $max - this can be changed to whtever you wish

Then a query is run to select all the information you want to display

the pagination links will be like

1 - 2 - 3 - 4 - 5 etcttc

<?php
 

$host = "localhost"; 

$user = "username"; 

$pass = "password"; 

$db = "database";
 
 

mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

mysql_select_db($db) or die("ERROR DB:".mysql_error()); 
 
 

$max = 25; 

$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 tablename WHERE id = '$id' ORDER BY domain_name ASC");

while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$example1 = $r['example1'];

$example2 = $r['example2'];

$example3 = $r['example3'];

$example4 = $r['example4'];
 
 

echo "<div><p>$example1</p><p>$example2</p><p>$example3</p></div>"; //ignore this

}
 

}else{
 
 

$sql = mysql_query("SELECT * FROM tablename LIMIT ".$limits.",$max") or die(mysql_error());
 

$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM tablename"),0);	
 

$totalpages = ceil($totalres / $max); 
 

//what will be displayed
 

echo "
 

<table width='320' border='0'>

  <tr>

    <td class='style21'><div align='center'><b>example</b></div></td>

    <td class='style21'><div align='center'><b>example1</b></div></td>

  </tr>";

while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$example1 = $r['example1'];

$example2 = $r['example2'];

$example3 = $r['example3'];

$example4 = $r['example4'];
 

echo "<tr>

    <td class='style19'>$example1/a></td>

    <td class='style19'><div align='center'>$example2</div</td>

  </tr>";

}

//close up the table

echo "</tr></table><BR>";
 
 

for($i = 1; $i <= $totalpages; $i++){ 

//this is the pagination link

echo "<span class='style19'><a href='link.php?p=$i'>$i</a> - </span>";
 

}

}
 

?>

Open in new window

0
 

Author Comment

by:Freejoy
ID: 21845542
Thank You,
How do I put this into my page?  How does it work?  Do I have to name the pages "...page1.php," "...page2.php" ect.?

0
 
LVL 9

Assisted Solution

by:Sander Stad
Sander Stad earned 168 total points
ID: 21845568
Maybe this is not really what you want but there are several cms programs that have this functionality build in. Some of these systems are CMSMS, Drupal and Joomla. Maybe you should take a look at that.
0
 

Author Comment

by:Freejoy
ID: 21846209
What I would like to do is turn pages.  That's all for now.  Say I have 20-30 pages, I would like to find some kind of script to turn pages.  All my stuff will already be on the pages.  These scripts seem close but I don't need a qty items per page at this time.  But I don't understand how to get them to turn the page..  I would really like something like this at the top and bottom of my pages that is easy to use:

<<< 1 2 3 4 5 >>>

That's all

Thanks
0
 
LVL 9

Assisted Solution

by:oliverpolden
oliverpolden earned 168 total points
ID: 21846383
Hi this question comes up under my Drupal filter.  If a Drupal answer is what you're after, you can use the Paging module: http://drupal.org/project/paging  Very handy, you can set your own break tag such as <!-- break --> and Drupal will split the pages for you.  If you have a rich text editor with a page break button, you can look at the code the button generates and set that as the page break tag.  If you or anyone need any help setting this up, feel free to ask or visit my site at: www.e3m.co.uk 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:oku86
ID: 21846424
Well for example if you were to use the method i posted you would need to set up a MySQL database and set up a table within the database - this is quite simple for a beginner to do and is quite fun to learn

http://www.tizag.com/mysqlTutorial/

Each record is given an ID - so imagine each record being a page

so as you follow the tutorial - linked above you will learn how to insert information into the mysql database

So if you set the $max = 1 - it will return 1 result per page and so if u have 30 pages of data it will looke like

1 2 3 4 5 6 7 etc etc

if this script is set to the index.php - each page will be index.php?p=1,  index.php?p=2 etc etc

But a content management system may make life easier but if u want to learn how to do this yourself its ahella lot more fun then installing an automated script
0
 
LVL 1

Expert Comment

by:oku86
ID: 21846655
I apologize for double posting

here is my example in action - just quickly set it up

http://verecia.info/
0
 

Author Comment

by:Freejoy
ID: 21847544
oliverpolden:
I downloaded the   linkfilter-6.x-1.1.zip. from your site and opened it.  This is the first thing it says to do:

1) Unzip the linkfilter.zip kit in the modules directory of your Drupal
    setup, usually sites/all/modules

I have no idea what they mean here, for one I don't have a Drupal setup.

Is there actually a working example of this?

******************************************
oku86:

I know I could spend time learning all about this.  I need something in the hurry up!
Oh yea, when i clicked on your link red flags went off and One Care popped up warring me about a Trojan.

I've been up all night I going to sleep on this.  good night


0
 
LVL 1

Expert Comment

by:oku86
ID: 21850233
im not sure how my domain sparked a trojan
0
 
LVL 9

Expert Comment

by:oliverpolden
ID: 21961668
Hey, sorry, didn't realise you had replied!

Are you still looking for a solution.  Drupal would be a complete change to your website. You would need to put everything into it, but if your site is basic then you could have it up and running in a couple of hours.  You could have most of the stuff on www.e3m.co.uk up and running including a theme of your choice.

I can't find any examples of paging and I don't have time right now to set it up but I could in a few hours (after work) if you would like.  There is a teeny bit here however: http://drupalzilla.com/module/paging

I'm keen to write a tutorial on setting up Drupal from a new user perspective so I'm willing to give you help setting it up.
0
 
LVL 1

Expert Comment

by:oku86
ID: 22080299
Your best option then would be to use Drupal - as its virtually all done for you - even thought it may take a few hours to figure out it will take less time + frustration coding it manually
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now