How to use this Google style pagination function?

This is a discussion on "How to use this Google style pagination function?" within the PHP Forum section. This forum, and the thread "How to use this Google style pagination function? are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Nov 3rd, 2006, 17:04
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
How to use this Google style pagination function?

I found a nice function that displays pagaination links like google links but have no idea how i use the function with the database queries.

I know how to connect to the database and create the queries but don't know how to use the function. There is nothing on the site where the code came from that tells you how to use it.

Does anyone know how I can use this function to generate links from a MySQL query?

PHP: Select all

<?php 
// quickLink 
# writes out page links 
function quickLink ($linkHref$desc$accessKey$linkTitle) { 
   
$theLink '<a href="'$linkHref .'#pagination" title="'$desc .'" accesskey="'
                   
$accessKey .'">'$linkTitle .'</a>'
   return 
$theLink


// google_like_pagination.php 
function pagination($number$show$showing$firstlink$baselink$seperator) { 
    
$disp floor($show 2); 
    if ( 
$showing <= $disp) : 

        if ( (
$disp $showing) > ): 
        
$low  = ($disp $showing); 
        else: 
        
$low 1
        endif; 
        
$high = ($low $show) - 1

    elseif ( (
$showing $disp) > $number) : 

        
$high $number
        
$low = ($number $show) + 1

    else: 

        
$low  = ($showing $disp); 
        
$high = ($showing $disp); 

    endif; 
     
    
// next / prev / first / last 
    
if ( ($showing 1) > ) : 
        if ( (
$showing 1) == ): 
        
$prev  quickLink ($firstlink'Previous''''Previous'); 
        else: 
        
$prev  quickLink ($baselink $seperator . ($showing 1), 
        
'Previous''z''Previous'); 
        endif; 
    else: 
        
$prev  'Previous'
    endif; 

    
$next  = ($showing 1) <= $number 
    
quickLink ($baselink $seperator . ($showing 1), 
    
'Next''x''Next') : 'Next';     

    if ( 
$_SERVER['REQUEST_URI'] == $firstlink ): 
    
$first '<span class="sel">First Page</span>';     
    else: 
    
$first quickLink ($firstlink'First Page''''First Page'); 
    endif;     
     
    if ( 
$showing == $number ): 
    
$last '<span class="sel">Last Page</span>';     
    else: 
    
$last quickLink ($baselink $seperator $number'Last Page''''Last Page'); 
    endif; 
     
     
    
$navi '<div id="pagination"> 
    <ul>'
."\n"
    
// start the navi 

        
$navi .= '<li>'$prev ."</li>\n"
     
    
// loop through the numbers 

    
foreach (range($low$high) as $newnumber): 

           
$link = ( $newnumber == ) ? $firstlink 
                
$baselink $seperator $newnumber
           if (
$newnumber $number): 
        
$navi .= ''
        elseif (
$newnumber == 0): 
        
$navi .= ''
        else: 
        
$navi .= ( $newnumber == $showing ) ? 
            
'<li class="sel">'$newnumber .'</li>'."\n" 
            
'<li>'quickLink ($link'Page '$newnumber''$newnumber) ."</li>\n"
        endif;                    
    endforeach;     
     
    
// end the navi first line 
     
        
$navi .= '<li>'$next ."</li>\n"
             
    
$navi .= '</ul>'."\n"
    
// second line 
    
$navi .= '<p>'$first ."\n | Showing page "$showing .' of '$number ."\n | "
            
$last .'</p> 
    </div>'

     
    return 
$navi;         


/* 
## fake content example 
                  
foreach (range(1, 301) as $newnumber): 
## fake content example 
$content []=  'this is page '. ($newnumber - 1); 
endforeach; 
                  
## Settings 
                  
$showing   = !isset($_GET["view"]) ? 1 : $_GET["view"]; 

    $firstlink = $_SERVER['PHP_SELF']; 
    $seperator = '?view='; 


## $baselink can be used if for example when the main script was index.php not the filename 
    $baselink  = $firstlink; 
     
## $show must be an odd number for this to work correctly 
## This is the number of links between next and previous links 
    $show = 3; 

echo $content[$showing]; 
echo pagination(count($content), $show, $showing, $firstlink, $baselink, $seperator);                  
*/ 
?>
I found the function here http://cms.227net.com/php/google_like_pagination
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Nov 5th, 2006, 11:46
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to use this Google style pagination function?

I don't know the concept and don't have time to read the script, sorry. But if you can describe how it works and the database you want to use with it, I'll try to help.

Please let me know if you get it solved -- otherwise I'm going to try to come back at some point and read the script, which I imagine will take some hard work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Nov 5th, 2006, 12:09
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to use this Google style pagination function?

I actually gave up with the script and found one that was much easier to use......much simpler and does the same thing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Nov 5th, 2006, 17:07
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to use this Google style pagination function?

Quote:
Originally Posted by AdRock View Post
I actually gave up with the script and found one that was much easier to use......much simpler and does the same thing
Hmmm ... I'm probably a bit late with this simple sample then:

Code: Select all
<?php

mysql_connect("localhost","wellho","wawawawa");
mysql_select_db("wellho");

$perpage = 10;
$lynx = $html = "";
$startat = $_REQUEST[page] * $perpage;

$q = mysql_query("select count(entry_id) from mt_entry");
$row = mysql_fetch_array($q);
$pages = ($row[0] + $perpage - 1) / $perpage;

$q = mysql_query("select * from mt_entry order by entry_id desc limit $startat,$perpage");

while ($row = mysql_fetch_assoc($q)) {
        $text = strip_tags($row[entry_text]);
        $text = substr($text,0,300);
        $html .= "<dt>$row[entry_id] - <a href=/mouth/$row[entry_id]_.html target=pix>$row[entry_title]</a></dt>";
        $html .= "<dd>$text ....<br><br></dd>";
        };

for ($k=0; $k<$pages; $k++) {
        if ($k != $_REQUEST[page]) {
         $lynx .= " <a href=$PHP_SELF"."?page=$k>".($k+1)."</a>";
        } else {
         $lynx .= " <b>--".($k+1)."--</b>";
        }
}
?>
<html><head>
<title>Showing blog entries</title>
<body><h2>Here are the entries you selected - page <?= $_REQUEST[page]+1 ?>:</h2><br>
<?= $html ?>
Please choose the next page you want to view:
<?= $lynx ?>
</body>
That code paginates through my blog - most recent entries first. The only change I have made is to alter the password that's embedded in the code. If you want to see how it runs it's at http://www.wellho.net/demo/mqchunks.php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Nov 6th, 2006, 12:59
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to use this Google style pagination function?

Ah, I see. I actually did the code for the pages I'm working on, including a blank to pick the number of pages, but I didn't understand what you wanted.

Not quite so tidy as Graham's, as expected , and there may be some noob errors in it. Credit to some unknown person for some of the ideas on pagination, although most of the code is made from scratch. The file is huge (at least for me) and I've tried to redact the really long parts, using * * * for omissions.

This is the $_GET results page for a search (by 'name' field) using one to three words for the search and 1 to 999 results per page. Applicable part of the form is at the bottom.

Code: Select all
<?php
// search.php
require_once('../../../mysql_foodvalues_connect.php');
$pagetitle = 'DHR: Display Nutrient Content of Specific Foods ';
$headline = 'Results of Search for Nutrient Content<br /> of Specific Foods';
$b = 'result_search_food';
$photo = "nutrient.gif";
include_once('./header_article_food_stripped.inc.php');

// rows to return
if (isset($_GET['limit']))  {
$limit = $_GET['limit'];
}  else  {
$limit=30; 
}

require_once('./../../../mysql_foodvalues_connect.php');
$var = @$_GET['name'] ;
$var1 = @$_GET['name1'] ;
$var2 = @$_GET['name2'] ;
$limit = @$_GET['limit'];

$t = NULL;
$t1 = NULL;
$t2 = NULL;
$u = 'qqqq';
$u1 = 'qqqqq';
$u2 = 'qzqzqz';  
(These are just placeholders to initiate the variables, I didn't want them floating around loose before I used them)

 if (!isset($var))  {
  echo "Please enter the name of a food in the first box and SUBMIT.";
exit();
  } else {
  $t = trim($var); 
}

if (isset($var1)) $t1 = trim($var1);
if (isset($var2)) $t2 = trim($var2);
if (isset($limit)) $limit = trim($limit);

if ($t2 == 'frozen') $u2 = 'frz'; if ($t1 == 'frozen') $u1 = 'frz'; if ($t == 'frozen') $u = 'frz';
if ($t2 == 'baked') $u2 = 'bkd'; if ($t1 == 'baked') $u1 = 'bkd'; if ($t == 'baked') $u = 'bkd';
if ($t2 == 'boiled') $u2 = 'bld'; if ($t1 == 'boiled') $u1 = 'bld'; if ($t == 'boiled') $u = 'bld';

* * * (about 90 more rows like this - the database uses a lot of abbreviations)

 
if (!preg_match("/^[A-Za-z]{1,20}$/", $t)) {
    die("<div class=\"column23r \"><h4>Alert!</h4><br>Each food search term
 must be one word composed of letters only. No hyphens, quotation marks,
 numbers, etc.  We apologize for this restriction.  Please re-enter. <br><br>If
 you are searching for a possessive name, such as Stubb's, just enter the name
 (Stubb) and the possessive form will be automatically included in the search.
  Also, make sure to fill the boxes in order: if you just have one word, use box 1,
 and if you just have two terms, use the first two boxes.</div>"); 
    }

*  *  * (regex for other input omitted)

if (!preg_match("/^[0-9]{1,3}$/", $limit)) {
    die("<div class=\"column23r \"><h4>Alert!</h4><br>Please enter a number between 1 and 999 in the last box.</div>");
}

if (isset($t2, $t1, $t))  {
$query = "SELECT id FROM usda WHERE (name LIKE \"%$t%\" OR name LIKE
  \"%$u%\") AND (name LIKE \"%$t1%\" OR name LIKE  \"%$u1%\") AND (name
 LIKE \"%$t2%\" OR name LIKE  \"%$u2%\") ";  
}elseif 

*  *  * (other clauses omitted)
  }

$numresults=mysql_query($query);
if ($numresults)  {
$numrows=mysql_num_rows($numresults);
}  else  {
echo '<span class="b">We are sorry, but your search returned no results.  Try]
 again with a broader search.</span>';
}

if (empty($s)) {
  $s=0;
}
 *** (this sets an artificial variable $s to enable variable pagination)

echo '<br class="sp25"><div class="center_panel"><h3>Results</h3><br><span class="b">You searched for &nbsp';
if (isset($var2, $var1, $var)) { 
echo ("\"$var $var1 $var2\".&nbsp;This search returned $numrows results.</span> ");
}elseif (

* * *
echo '<br />For a new search, you can use the form at the bottom of this page.';
if (isset($var2, $var1, $var))  {
$query = "SELECT id, name, cat FROM usda WHERE (name LIKE \"%$t%\" OR
 name LIKE  \"%$u%\") AND (name LIKE \"%$t1%\" OR name LIKE  \"%$u1%\")
 AND (name LIKE \"%$t2%\" OR name LIKE  \"%$u2%\") ORDER BY cat, name
 LIMIT $s,$limit";

* * *
  }
$result = @mysql_query($query) or die (mysql_error());

//RESULTS DISPLAY

$ab = <<< END
<br /><br />If you see USDA abbreviation you don't recognize, click here for a
 popup window with a list of abbreviations and meanings.<br />
<form>
<input class="submitusda" type="button" onClick="myPopup()" value="USDA abbreviations">
<noscript> <a href="./usda_abbr.html">USDA</a></noscript>
</form>
END;

echo $ab;

$count = (1 + $s);
$a = $s + ($limit);
if ($a > $numrows) $a = $numrows;
$b = ($s + 1);
 
if ($numrows > 0)  echo "<br><span class=\"b\">Showing results $b to $a
 of $numrows:</span><br><br />"; 
 
if ($numrows == 0)  {     
     echo "<div class=\"column23r \">If you entered more than one word, try
 using just the single most important word.  You might also try browsing through
 the entire <a href=\"searchbycategory.php\">category.</a> If all else fails, you
 can try to find the food by using the Google search box, above.  If you will, send
 us an email telling us what food you were unable to find, and we\'ll see if we
 can add it.</div>";
}


while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$food_name = $row['name'];
$ar = explode(',',$food_name);
$br = implode(' ',$ar); 

 ***(Puts spaces after commas for database entries. I've since learned an easier way to do this)

echo "<span class=\"ls\">$count.)&nbsp;<a href=\"result_display.php?id=" . $id . "\">$br</a></span><br>"; 

  $count++ ;
          }


$current_page = (($s/$limit) + 1);
  echo "<br />";
  $a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  
  if ($s>=1) { 
  $prevs=($s-$limit);
        if (isset($var2, $var1, $var))  {
         echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&name1=$var1&name2=$var2&
limit=$limit\"> &lt;&lt;  Previous $limit</a></div>";
        }elseif (isset($var1, $var))  {
  echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&name1=$var1&limit=$limit\"> &lt;&lt;
  Previous $limit</a>>/div>";
        }  else  {
  echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&limit=$limit\"> &lt;&lt; Previous $limit</a></div>";
        }
  }

  $pages=intval($numrows/$limit);
  if ($numrows%$limit) {
  $pages++;
  }

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) 
  $news=$s+$limit;
  
   if ($current_page < $pages) {
      if (isset($var2, $var1, $var))  {
      echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&name1=$var1&name2
=$var2&limit=$limit\">Next $limit &gt;&gt;</a></div><br>";
    }elseif (isset($var1, $var))  {
      echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&name1=$var1&limit=$limit\">
Next $limit &gt;&gt;</a></div><br>";
    }  else  {
      echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&limit=$limit\">Next $limit
 &gt;&gt;</a></div><br>";
    }
}
 ?>
<div class = "clear"></div></div>

<!-- FORM -->
<div class="center_panel">
<h3>Search Foods By Name</h3>
<form name="form" action="result_search_food.php" method="get" >

* * * Substantive input form 

    <div class="fl">How many results would you like to display per page? (We suggest 30.)</div>
<div class="fr"><input type="text" name="limit" size="3" maxlength="3" value="
<?php
 if (isset($_GET['limit'])) { 
     $limsafe = htmlentities($_GET['limit']);
     echo $limsafe;
 }else{ 
 echo '30'; 
 } 
 ?>
 "> </div><br><br><br><div><input type="submit" class="submit" name="Submit" value="Search"></div>
        </form>

*  *  *
<p>The last box should contain a number between 1 and 999.</p>
<div class = "clear"></div></div>

<?php  
  include_once('./footer_article_food.inc');
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
pagination

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to tell IE7 to use a pariticular style in a single style sheet figo2476 Web Page Design 5 May 25th, 2007 14:23
Need small help with pagination ofi PHP Forum 0 Feb 7th, 2007 11:44
Pagination without MySQL Catie PHP Forum 3 Jan 21st, 2007 23:06
RSS feed pagination deesto Web Page Design 0 Dec 24th, 2006 02:31
PHP pagination robertboyle PHP Forum 1 Jul 30th, 2006 18:08


All times are GMT. The time now is 02:23.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42