Lapozás
Egy lapozás ban kérem a segítségeteket, nagy kezdő vagyok, nem rég kezdtem el tanulni a PHP világát. Félig meddig segítséggel sikerült megoldani de még nem tökéletesen működik. A program lényege sql ből kérdez le kereséssel, azt táblákba írja ki. A lapozó része pedig két legördülő listából az adott oldalra lehet ugorni, illetve a be állított értékeknek megfelelően lehet állítani az oldalon megjelníthető sorok számát.
A hiba pedig az, hogy ha a következő oldalra kattintok akkor nem jeleníti meg benne az adott értékeket, sorokat, + a böngésző feltűnően sokat malmozik. Ennek a problémának a megoldásában kérem a segítségeteket.
A kód: Kereső részenA lapozórész kódja:
■ A hiba pedig az, hogy ha a következő oldalra kattintok akkor nem jeleníti meg benne az adott értékeket, sorokat, + a böngésző feltűnően sokat malmozik. Ennek a problémának a megoldásában kérem a segítségeteket.
A kód: Kereső részen
- <?php
- session_start();
- //error_reporting( 0 );
- ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="hu" lang="hu">
- <head>
- <title>Keresés</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link rel="stylesheet" href="style.css"/>
- </head>
- <form name="frmSearch" method="get" action="<?php echo $_SERVER['SCRIPT_NAME'];
- ?>">
- <table width="599" border="1">
- <tr>
- <th>keresett szó<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo isset( $_GET["txtKeyword"] )?$_GET["txtKeyword"]:'';
- ?>">
- <input type="submit" value="Search"></th>
- </tr>
- </table>
- </form>
- <?php include ( "./page.php" );
- if ( isset( $_GET["txtKeyword"] ) ) {
- // adatbázis kapcsolódás
- $link = mysql_connect( "localhost", "root", "" );
- if ( !$link ) {
- die( "Nem lehet csatlakozni a MySQL szerverhez!: " . mysql_error() );
- }
- if ( mysql_select_db( "paleo", $link ) == false )
- die( "hiba az adatbázis kiválasztásban!" . mysql_error() );
- // Search By hosszu or elo
- $strSQL = "SELECT * FROM kutnev WHERE (rovid LIKE '%" . $_GET["txtKeyword"] . "%' or hosszu LIKE '%" . $_GET["txtKeyword"] . "%' ) ";
- $objQuery = mysql_query( $strSQL ) or die ( "Error Query [" . $strSQL . "]" );
- $Num_Rows = mysql_num_rows( $objQuery );
- $Per_Page = 1; // Per Page
- $Page = $_GET["Page"];
- if ( !$_GET["Page"] ) {
- $Page = 1;
- }
- if ( !isset( $_GET["ipp"] ) )
- $ipp = 10;
- else
- $ipp = $_GET["ipp"];
- if ( $ipp != 'All' ) {
- $strSQL = $strSQL . " LIMIT " . $Page . "," . $ipp;
- $objQuery = mysql_query( $strSQL ) or die ( "Error Query [" . $strSQL . "]" );
- $Per_Page = $_GET['ipp'];
- } else
- $Per_Page = $Num_Page;
- echo $strSQL;
- $Prev_Page = $Page-1;
- $Next_Page = $Page + 1;
- $Page_Start = ( ( $Per_Page * $Page ) - $Per_Page );
- if ( $Num_Rows <= $Per_Page ) {
- $Num_Pages = 1;
- } else if ( ( $Num_Rows % $Per_Page ) == 0 ) {
- $Num_Pages = ( $Num_Rows / $Per_Page ) ;
- } else {
- $Num_Pages = ( $Num_Rows / $Per_Page ) + 1;
- $Num_Pages = ( int )$Num_Pages;
- }
- ?>
- <table width="600" border="1">
- <tr>
- <th width="91"> <div align="center">rovid </div></th>
- <th width="98"> <div align="center">hosszu </div></th>
- <th width="198"> <div align="center">elo </div></th>
- </tr>
- <?php
- while ( $objResult = mysql_fetch_array( $objQuery ) ) {
- ?>
- <tr>
- <td><div align="center"><?php echo $objResult["rovid"];
- ?></div></td>
- <td><?php echo $objResult["hosszu"];
- ?></td>
- <td align="right"><input type="checkbox" name="elo" value="2" <?php echo ( $objResult["elo"] == 1 )?'checked="checked"':'';
- ?>></input> </td>
- </tr>
- <?php
- }
- ?>
- </table>
- <br>
- Total <?php echo $Num_Rows;
- ?> Record : <?php echo $Num_Pages;
- ?> Page :
- <?php
- $pages = new Paginator;
- $pages->link = $_SERVER["PHP_SELF"];
- $pages->txtKeyword = isset( $_GET["txtKeyword"] )?$_GET["txtKeyword"]:'';
- $pages->items_total = $Num_Rows;
- // $pages->items_total = 2;
- $pages->mid_range = 7;
- $pages->paginate();
- echo $pages->display_pages();
- echo $pages->display_jump_menu();
- echo $pages->display_items_per_page();
- ?>
- <?php
- mysql_close( $link );
- }
- ?>
- </body>
- </html>
- <?php
- class Paginator {
- var $items_per_page;
- var $items_total;
- var $current_page;
- var $num_pages;
- var $mid_range;
- var $low;
- var $high;
- var $limit;
- var $return;
- var $default_ipp = 10;
- var $client;
- var $txtKeyword;
- var $link;
- function Paginator()
- {
- $this->current_page = 1;
- $this->mid_range = 5;
- $this->items_per_page = ( !emptyempty( $_GET['ipp'] ) ) ? $_GET['ipp']:$this->default_ipp;
- }
- function paginate()
- {
- if ( isset( $_GET['ipp'] ) && $_GET['ipp'] == 'All' ) {
- $this->num_pages = ceil( $this->items_total / $this->default_ipp );
- $this->items_per_page = $this->default_ipp;
- } else {
- if ( !is_numeric( $this->items_per_page ) OR $this->items_per_page <= 0 ) $this->items_per_page = $this->default_ipp;
- $this->num_pages = ceil( $this->items_total / $this->items_per_page );
- }
- $this->current_page = isset( $_GET['page'] ) ? ( int ) $_GET['page'] : 1; // must be numeric > 0
- if ( $this->current_page < 1 Or !is_numeric( $this->current_page ) ) $this->current_page = 1;
- if ( $this->current_page > $this->num_pages ) $this->current_page = $this->num_pages;
- $prev_page = $this->current_page-1;
- $next_page = $this->current_page + 1;
- if ( $this->num_pages > 3 ) {
- $this->return = ( $this->current_page != 1 And $this->items_total >= 3 ) ? "<a class=\"paginate\" href=\"$this->link?txtKeyword=$this->txtKeyword&cid=$client&page=$prev_page&ipp=$this->items_per_page\"><img src='../../images/arrow_left.gif' border='0' /></a> ":"<span class=\"inactive\" href=\"#\"><img src='../../images/arrow_blank.gif' border='0' /></span> ";
- $this->start_range = $this->current_page - floor( $this->mid_range / 2 );
- $this->end_range = $this->current_page + floor( $this->mid_range / 2 );
- if ( $this->start_range <= 0 ) {
- $this->end_range += abs( $this->start_range ) + 1;
- $this->start_range = 1;
- }
- if ( $this->end_range > $this->num_pages ) {
- $this->start_range -= $this->end_range - $this->num_pages;
- $this->end_range = $this->num_pages;
- }
- $this->range = range( $this->start_range, $this->end_range );
- for( $i = 1;$i <= $this->num_pages;$i++ ) {
- if ( $this->range[0] > 2 And $i == $this->range[0] ) $this->return .= " ... ";
- // loop through all pages. if first, last, or in range, display
- if ( $i == 1 Or $i == $this->num_pages Or in_array( $i, $this->range ) ) {
- $this->return .= ( $i == $this->current_page And $_GET['page'] != 'All' ) ? "<a title=\"Go to page $i of $this->num_pages\" class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" title=\"Go to page $i of $this->num_pages\" href=\"$this->link?txtKeyword=$this->txtKeyword&cid=$client&page=$i&ipp=$this->items_per_page\">$i</a> ";
- }
- if ( $this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1] ) $this->return .= " ... ";
- }
- $this->return .= ( ( $this->current_page != $this->num_pages And $this->items_total >= 2 ) And ( $_GET['page'] != 'All' ) ) ? "<a class=\"paginate\" href=\"$this->link?txtKeyword=$this->txtKeyword&cid=$client&page=$next_page&ipp=$this->items_per_page\"><img src='../../images/arrow_right.gif' border='0' /></a>\n":"<span class=\"inactive\" href=\"#\"><img src='../../images/arrow_blank.gif' border='0' /></span>\n";
- } else {
- for( $i = 1;$i <= $this->num_pages;$i++ ) {
- $this->return .= ( $i == $this->current_page ) ? "<a class=\"current\" href=\"#\">0$i</a> ":"<a class=\"paginate\" href=\"$this->link?txtKeyword=$this->txtKeyword&cid=$client&page=0$i&ipp=$this->items_per_page\">0$i</a> ";
- }
- }
- $this->low = ( $this->current_page-1 ) * $this->items_per_page;
- $this->high = ( $_GET['ipp'] == 'All' ) ? $this->items_total:( $this->current_page * $this->items_per_page )-1;
- $this->limit = ( $_GET['ipp'] == 'All' ) ? "":" LIMIT $this->low,$this->items_per_page";
- }
- function display_items_per_page()
- {
- $items = '';
- $ipp_array = array( 1, 2, 10, 25, 50, 100, 'All' );
- foreach( $ipp_array as $ipp_opt ) $items .= ( $ipp_opt == $this->items_per_page ) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n":"<option value=\"$ipp_opt\">$ipp_opt</option>\n";
- return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='$this->link?txtKeyword=$this->txtKeyword&cid=$client&page=1&ipp='+this[this.selectedIndex].value;return false\">$items</select>\n";
- }
- function display_jump_menu()
- {
- for( $i = 1;$i <= $this->num_pages;$i++ ) {
- $option .= ( $i == $this->current_page ) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
- }
- return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='$this->link?txtKeyword=$this->txtKeyword&cid=$client&page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page';return false\">$option</select>\n";
- }
- function display_pages()
- {
- return $this->return;
- }
- }
- ?>
Ajánlom figyelmedbe az
Megelőztél,
Ne reméld!
Néhány jó tanács:
- Felhasználótól érkező adatot soha ne írj közvetlenül query-be! Legalább a
mysql_real_escape_string()
függvényt használd.- Lapozásra elég jól használható a query végén a
LIMIT 1, 10
, itt a számok azt jelentik: hányadik rekordtól melyik rekordig kéred az eredményt.- Legközelebb emeld ki a kódodból a problémásnak vélt (rövid!) részt, egy PHP vagy MySql gond kapcsán nem kíváncsi senki sem a HTML-re. Míg elbíbelődsz a válogatással, lehet te is megtalálod a hibát.
- Ne írj ilyen átláthatatlan, számodra is kusza kódot! Vedd külön az adatbázisműveleteket, a beérkező adatok feldolgozását a kimenetgenerálástól! Te is jobban átlátod később is, hogy mit-miért csináltál, és könnyebb a hibakeresés is.
- Az elején mire való a
session_start()
? Nem láttam nyomát, hogy bármire is használnád.Szerk.:
!!!LIMIT-et rosszul írtam: az első szám azt adja meg, hogy hány rekordot kell átugrani, a második pedig a megjelenítendő rekordok számát!
Szervezd át
Köszi az észrevételeket, a
található funkciót szeretném megvalósítani egy kereséssel egybe kötve.
Nem forráskódgyár
Javaslat:
Olvass el (itt, a WL-en) minél több cikket, fórumtémát ezzel kapcsolatban (használd a keresőt), olvasd sokat a PHP manual-t, majd kezdd el megírni a progit te magad. Akkor, ha ezeket betartod és mégis elakadsz, "tolakodva" fogunk segíteni.
Formázd itt (is) írásaidat, "rendesebb embernek" gondolunk, ha ügyelsz a külalakra és a helyesírásodra - és nem hisszük, hogy lusta vagy hozzá. Ha linkelsz, az legyen link.