Re: navigatie doesn't work in this script

From: Date: Mon, 30 Sep 2002 03:45:15 +0000
Subject: Re: navigatie doesn't work in this script
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-118138@lists.php.net to get a copy of this message
Hi, Monday, September 30, 2002, 5:20:48 AM, you wrote: d> I made a MySQL database with a dictionary in it. Above there is a form in d> wich you can specify some parameters. The problem is that the navigation d> doesn't work well. I always get the first 5 results. Can somebody help me d> out? There are about 56 records in the database. d> The (not quite) working thing is on: d> http://www.oostendseverhalen.be/test_met_navigatie3.php d> <html> d> <head> d> <title>Untitled Document</title> d> <meta http-equiv="Content-Type" content="text/html; d> charset=iso-8859-1"> d> </head> d> <body> d> <?php d> /* hieronder volgt het formulier */ d> echo ' d> <table width="100%" border="0" align="center" d> cellpadding="2"> d> <tr> d> <td height="198"> <form name="form1" method="post" d> action=""> d> <table width="100%" height="89" border="0" d> align="center" d> cellpadding="2" summary="interactief woordenboek Oostends nederlands, Engels en Frans.">> d> <caption> d> <font color="#003399" size="6" d> face="Tahoma">Ostensche d> encyclopedie </font> d> </caption> d> <tr align="left" valign="middle" d> bordercolor="#000033" bgcolor="#CCFFFF">> d> <td width="33%" height="47" align="left" valign="middle">><em><strong><font size="2" face="Tahoma"> d> <label> d> <input name="taalkeuze" type="radio" d> value="woord_ost" checked>> d> Oostends-Drooghenbroodt</label> d> <br> d> <label> d> <input type="radio" name="taalkeuze" d> value="woord_des"> d> Oostends-desnerck</label> d> </font></strong></em></td> d> <td width="22%" valign="middle"><p> d> <em><strong><font size="2" face="Tahoma">> d> <label> d> <input type="radio" name="taalkeuze" d> value="woord_nl"> d> Nederlands</label> d> <br> d> <label> d> <input type="radio" name="taalkeuze" d> value="woord_fr"> d> Frans</label> d> </font></strong></em></p></td> d> <td width="25%"><em><strong><font d> size="2" face="Tahoma"> d> <label> d> <input type="radio" name="taalkeuze" d> value="woord_eng"> d> Engels</label> d> <br> d> <label> d> <input type="radio" name="taalkeuze" d> value="verklaring"> d> Verklaring</label> d> </font></strong></em></td> d> <td width="20%"><div align="center"><font d> size="2" face="Tahoma">>Hulp d> bij het zoeken</font></div></td> d> </tr> d> <tr align="left" valign="middle" d> bordercolor="#000033" bgcolor="#FFFFFF">> d> <td height="33" colspan="4" align="center" d> valign="top" bgcolor="#FFFFCC">><p><font size="2" face="Tahoma"> d> <input name="zoekwoord" type="text" d> id="zoekwoord2" value="" d> size="50" maxlength="40"> d> <input type="submit" name="Submit" d> value="Zoekn"> d> </font><font size="2" face="Tahoma"> d> </font></p> d> <blockquote> d> </blockquote></td> d> </tr> d> </table> d> </form> d> </tr> d> </table>'; d> if ($taalkeuze === NULL) d> { d> $taalkeuze = "woord_ost"; d> } d> $van = 0; d> $tot = 5; d> mysql_connect("**.**.**.**", "******", "********") or die d> (mysql_error()); d> $Query_beperkte_records = "SELECT woordenboek.* FROM woordenboek WHERE " d> .$taalkeuze. " LIKE '%" .$zoekwoord. "%' ORDER BY d> woordenboek.woord_ost ASC d> LIMIT ".$van.",".$tot; d> $Query_alle_records = "SELECT woordenboek.* FROM woordenboek WHERE " d> .$taalkeuze. " LIKE '%" .$zoekwoord. "%'"; d> $result = mysql("oostends",$Query_beperkte_records) or die(mysql_error()); d> $query = mysql("oostends",$Query_alle_records) or die(mysql_error()); d> $num3 = mysql_num_rows($query); d> $num2 = $num3 / $tot;. d> $num = ceil($num2);. ...snip Here is a class that will create a google like pagination of results <? class page_class { var $count = 0; //total pages var $start = 0; //starting record var $pages = 0; //number of pages available var $page = 1; //current page var $maxpages; //shows up to 2 * this number and makes a sliding scale var $show; //number of results per page function page_class($count=0,$show=5,$max=9){ $this->count = $count; $this->show = $show; $this->maxpages = $max; ($this->count % $this->show == 0)? $this->pages = intval($this->count/$this->show) :$this->pages = intval($this->count/$this->show) +1; if(!empty($_GET['search_page'])){ $this->page = $_GET['search_page']; $this->start = $this->show * $this->page - $this->show; } } function get_limit(){ $limit = ''; if($this->count > $this->show) $limit = 'LIMIT '.$this->start.','.$this->show; return $limit; } function make_head_string($pre){ $r = $pre.' '; $end = $this->start + $this->show; if($end > $this->count) $end = $this->count; $r .= ($this->start +1).' - '.$end.' of '.$this->count; return $r; } function make_page_string($words,$pre='Result Page:'){ $r = $pre.' '; if($this->page > 1){ $y = $this->page - 1; $r .= '<a href="'.$_SERVER['PHP_SELF'].'?search_page='.$y.$words.'">Previous</a>&nbsp;'; } $end = $this->page + $this->maxpages-1; if($end > $this->pages) $end = $this->pages; $x = $this->page - $this->maxpages; $anchor = $this->pages - (2*$this->maxpages) +1; if($anchor < 1) $anchor = 1; if($x < 1) $x = 1; if($x > $anchor) $x = $anchor; while($x <= $end){ if($x == $this->page){ $r .= '<span class="s">'.$x.'</span>&nbsp;'; } else{ $r.= '<a href="'.$_SERVER['PHP_SELF'].'?search_page='.$x.$words.'">'.$x.'</a>&nbsp;'; } $x++; } if($this->page < $this->pages){ $y = $this->page + 1; $r .= '<a href="'.$_SERVER['PHP_SELF'].'?search_page='.$y.$words.'">Next</a>&nbsp;'; } return $r; } } //Usage mysql_connect("**.**.**.**", "******", "********") or die (mysql_error()); $Query_alle_records = "SELECT COUNT(*) AS cnt FROM woordenboek WHERE ".$taalkeuze. " LIKE '%" .$zoekwoord. "%'"; $query = mysql("oostends",$Query_alle_records) or die(mysql_error()); $row = mysql_fetch_array($result); $count = $row['cnt']; if($count > 0){ $page = new page_class($count,5,10); //total number of results,number of results to show,max number of pages on a sliding scale (ends up as 2x this number..ie 20) $limit = $page->get_limit(); $Query_beperkte_records = "SELECT woordenboek.* FROM woordenboek WHERE ".$taalkeuze. " LIKE '%" .$zoekwoord. "%' ORDER BY woordenboek.woord_ost ASC ".$limit; $result = mysql("oostends",$Query_beperkte_records) or die(mysql_error()); $hstring = $page->make_head_string('Results'); $pstring = $page->make_page_string("&amp;zoekwoord=".$zoekwoord."&amp;whatever=".$whatever);// add the other variables to pass to next page in a similar fashion echo "<table><tr><td>".$hstring."</td></tr>"; while($row = mysql_fetch_array($result)){ echo "<tr><td>".$show_data_here."</td></tr>"; } echo "<tr><td>".$pstring."</td></tr></table>"; } ?> this is of course untested with your code :) Note: the search variables on subsequent pages will be passed by GET method -- regards, Tom

« previous php.general (#118138) next »