php.de

Zurück   php.de > Webentwicklung > PHP Einsteiger > PHP Tipps 2004-2

 
 
LinkBack Themen-Optionen Thema bewerten
Alt 06.12.2004, 12:06  
Gast
 
Beiträge: n/a
Standard [Erledigt] Problem beim Suchen Ergebnisse

Hi, everybody

I am tring to build a search function with PHP and keyword matching(without mysql) for my homepage. The search function has already worked. however, the search results are just displayed in one page. So I want to change it to represent in multipage. for example 5 results pre page. 12 results will be arranged in 3 pages.

But now I met a big problem. It shows nothing if I click the second or third page. I am not sure if the code is correct. Any help and suggestions are welcome. Thanks in advance.

PHP-Code:
<?php
$found 
"0"
   
$file_type ".html"
   
$directory opendir('.'); 
   
$limit 5

   
$page $_REQUEST['page']; 
   
$query $_POST['query']; 


   if (!isset(
$_GET['page'])) { 
    
$page 1
   } 

   if(
$page 1){ 
    
$query $_REQUEST['query']; 
   } 

    if(
strlen($search) < 3){ 
    echo 
"Please enter more characters in this field."
               exit(); 
   } 
?>
... searching process

PHP-Code:
<?php
if($found != "0"){ 
           echo 
"$found Results found for \" $search \"

"

           
$arr explode("\n"$arr); 
           
natsort($arr); 

           
reset($arr); 

           
$arr array_reverse($arr); 

       
// create multipages for Search Results 
       
$num_rows sizeof($arr); 
       
$sOut "<center>

 - "

       
$num_pages ceil($num_rows $limit); 


      
$first = ($page-1) * $limit
      
$last $first $limit

       
// output the results saved in Array 
       
for($i=$first$i<$last$i++) { 
        
$output .= $arr[$i]."\n"
       } 
       print 
$output

       for(
$x=1;$x<=$num_pages;$x++){ 
        
$pos_page = ($x 1) * $limit

  if(
$pos_page != $page){ 
     
$sOut .= "<a href=\"$PHP_SELF?query=$query&page=".($pos_page 1)."\">$x</a> - "
   } 
           else{ 
        
$act_page $x
        
$sOut .= "<font style=\"font-weight: bold\">$x</font> - "
     } 
       } 
   
$sOut .= "
[b]Page $act_page from $num_pages pages[/b]
"

       
$sOut .= "</p></center>"
       echo 
$sOut

?>
 
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 06.12.2004, 12:32  
Erfahrener Benutzer
 
Registriert seit: 18.09.2003
Beiträge: 13.598
PHP-Kenntnisse:
Fortgeschritten
imported_Ben ist zur Zeit noch ein unbeschriebenes Blatt
Standard

hi,

i actually have no possibility to test the code, so it maybe contains errors.
try to understand it and ask if you have problems with it.

PHP-Code:
<?php 
   
// start the session 
   
session_start(); 

   
// number of results per page 
   
$perpage 2

   if( isset(
$_GET['start'],$_SESSION['result']) 
       && 
       
is_numeric($_GET['start']) 
       && 
       
$_GET['start'] > 
       
&& 
       
$_GET['start'] <= count($_SESSION['result']) 
     ) 
   { 
      
// show more pages 
      
      
for( $i $_GET['start']; $i < ($_GET['start']+ $perpage); $i++ ) 
      { 
          if( 
$i count($_SESSION['result']) ) 
          { 
               echo (
$i+1) . '. ' $_SESSION['result'][$i] . '
'

          } 
      } 

      
// check if another 'next'-link is necessary 

      
if( count($_SESSION['result']) > ($_GET['start'] + $perpage) )
      { 
          echo 
'
'

          echo 
'[url="' $_SERVER['PHP_SELF'] . '?start=' . ($_GET['start'] + $perpage) . '"]next page[/url]'
      } 


   } 
   else 
   { 
      
// fill the resultset 
      // replace this with your search process 

      
$_SESSION['result'] = array( 'first'
                                   
'second'
                                   
'third'
                                   
'fourth'
                                   
'fifth'
                                   
'sixth'
                                   
'seventh' ); 


      
// show the first page of results 
      
for( $i 0$i $perpage$i++ ) 
      { 
          if( 
$i count($_SESSION['result']) ) 
          { 
               echo (
$i+1) . '. ' $_SESSION['result'][$i] . '
'

          } 
      } 


      
// check if a 'next'-link is necessary 

      
if( count($_SESSION['result']) > $perpage 
      { 
          echo 
'
'

          echo 
'[url="' $_SERVER['PHP_SELF'] . '?start=' $perpage '"]next page[/url]'
      } 

   } 

?>

this is only an idea to solve your problem
maybe you can improve it?

have fun and success.

ben


edited:
i corrected the code. it contained several errors sorry for that.
now you should be able to work with it.

@all:
hmm .. i posted wrong code and nobody realized it? :shosk:
why you do not try to answer questions that are written in English language?
maybe some of us should think about that.

und nein .. mein englisch ist wirklich nicht gut ^^
imported_Ben ist offline  
Alt 06.12.2004, 20:40  
Gast
 
Beiträge: n/a
Standard

Hi Ben

Ich danke dir für die ausführliche Erklärung und Hilfen.

Es funktioniert schon! Davon habe ich auch gelehrt, kleinigkeit muss man immer aufpassen.
 
Alt 06.12.2004, 22:55  
Gast
 
Beiträge: n/a
Standard

The function of Pagination works, but there is always a bug.
if I try to come back to the first page, the search result turns to zero(a blank page). what's the matter with it?

I looked through the query seems also correct.

../search.php?query=test&page=1 <- it doesn't work.
../search.php?query=test&page=2 <- it works, if I click the second page
../search.php?query=test&page=3 <- it also works

PHP-Code:

    
if (!isset($_GET['page'])){
        
$page 1;
    }

// Here the first page can be displayed correctly when
// I change it to $page >=1) 
// However, it doesn't work again while the new search.

    
if ($page 1) {
              
$query $_GET['query'];
                      
$search $query;
    } 
Thanks in advance.
 
 


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
[Erledigt] Galerie Problem... coraplanet PHP Tipps 2008 4 06.06.2008 13:42
Sessions Problem StephenKing PHP Tipps 2008 3 16.10.2007 08:30
MATCH .... AGAINST ... Problem Denise Datenbanken 4 21.03.2007 20:05
datensätze defekt oder problem mit dem einlesen? Ministry Datenbanken 4 06.07.2006 18:42
problem!!! PHP Tipps 2006 6 08.02.2006 11:06
[Erledigt] wieder ein Problem bei phpmailer und smtp PHP Tipps 2006 24 07.02.2006 01:07
Problem mit Weiterleitung PHP Tipps 2004-2 16 22.12.2004 17:49
problem mit ausgabe bei einer "tmp"-table... nautiluS PHP Tipps 2004-2 0 20.12.2004 15:12
Smarty und PHP-Skript Problem PHP Tipps 2004-2 2 03.12.2004 22:27
Problem der richtigen Daten-Ausgabe in einer Tabelle Heart PHP Tipps 2004-2 0 25.11.2004 18:57
[Erledigt] PHP Upload (Master Value/Local Value Problem) PHP-Fortgeschrittene 5 23.11.2004 07:21
Problem mit alter JavaScript-Funktion woods PHP Tipps 2004 1 13.08.2004 13:34
[Erledigt] Problem mit Timestamp! PHP Tipps 2004 24 08.06.2004 19:51
Login Problem PHP Tipps 2004 4 04.06.2004 18:46
foreach problem mAy^daY PHP Tipps 2004 3 02.06.2004 20:29


Alle Zeitangaben in WEZ +2. Es ist jetzt 23:19 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.