www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP search page

  1. #1
    Join Date
    Apr 2006
    Posts
    77

    PHP search page

    Hi, I admit I'm a novice at PHP, having a mild form of dyslexia doesn't help, anyway...

    I have borrowed some code for searching my database, which it does the problem is he paging doesn't work, I have searched thru the code but I cant figure it out Im probably being really dumb. heres the code. Many thanks for any help

    HTML Code:
    <form name="form" action="<? $PHP_SELF; ?>" method="get">
      <input type="text" name="q" />
      <input type="submit" name="Submit" value="Search" />
    </form>
    
    
    <?php
    
      // Get the search variable from URL
    
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    
    // rows to return
    $limit=10; 
    
    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      echo "<p>Please enter a search...</p>";
      exit;
      }
    
    // check for a search parameter
    if (!isset($var))
      {
      echo "<p>We dont seem to have a search parameter!</p>";
      exit;
      }
    
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root","mysql"); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("database") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query  
    $query = "select * from table where name like \"%$trimmed%\"  OR othername  like  \"%$trimmed%\"   
      order by field"; // EDIT HERE and specify your table and field names for the SQL query
    
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    // If we have no results, offer a google search as an alternative
    
    if ($numrows == 0)
      {
      echo "<h4>Results</h4>";
      echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
    
    // google
     echo "<p><a href=\"http://www.google.com/search?q=" 
      . $trimmed . "\" target=\"_blank\" title=\"Look up 
      " . $trimmed . " on Google\">Click here</a> to try the 
      search on google</p>";
      }
    
    // next determine if s has been passed to script, if not use 0
      if (empty($s)) {
      $s=0;
      }
    
    // get results
      $query .= " limit $s,$limit";
      $result = mysql_query($query) or die("Couldn't execute query");
    
    // display what the person searched for
    echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
    
    // begin to show results set
    echo "<p>Results  </p>";
    $count = 1 + $s ;
    
    // now you can display the results returned
      while ($row= mysql_fetch_array($result)) {
       $name = $row["name"];
      $othername = $row["detail"];
      $id = $row["id"];
    
      echo 	$count .  $name . " <a href=inddex.php?id=" . 
      			$id . " style=text-transform:capitalize; >" . 
      			$name  .   " - " . $othername .  "</a> <br /> " ;
      $count++ ;
      }
    
    $currPage = (($s/$limit) + 1);
    
    //break before paging
      echo "<br />";
    
      // next we need to do the links to other results
      if ($s>=1) { // bypass PREV link if s is 0
      $prevs=($s-$limit);
      print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;";
      }
    
    // calculate number of pages needing links
      $pages=intval($numrows/$limit);
    
    // $pages now contains int of pages needed unless there is a remainder from division
    
      if ($numrows%$limit) {
      // has remainder so add one page
      $pages++;
      }
    
    // check to see if last page
      if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
    
      // not last page so give NEXT link
      $news=$s+$limit;
    
      echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
      }
    
    $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?>

  2. #2
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    Hmm try this, and also post the errors that you get

    PHP Code:
    <form name="form" action="<?php echo $PHP_SELF?>" method="get">
    <input type="text" name="q" />
    <input type="submit" name="Submit" value="Search" />
    </form>


    <?php

    $page 
    $PHP_SELF;

    // Get the search variable from URL
    $var = @$_GET['q'] ;
    $trimmed trim($var); //trim whitespace from the stored variable

    // rows to return
    $limit=10

    // check for an empty string and display a message.
    if ($trimmed == "")
    {
        echo 
    "<p>Please enter a search...</p>";
        exit;
    }

    // check for a search parameter
    if (!isset($var))
    {
        echo 
    "<p>We dont seem to have a search parameter!</p>";
        exit;
    }

    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("localhost","root","mysql"); //(host, username, password)

    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("database") or die("Unable to select database"); //select which database we're using

    // Build SQL Query  
    $query "select * from table where name like \"%$trimmed%\"  OR othername  like  \"%$trimmed%\"   
        order by field"
    // EDIT HERE and specify your table and field names for the SQL query

    $numresults=mysql_query($query);
    $numrows=mysql_num_rows($numresults);

    // If we have no results, offer a google search as an alternative
    if ($numrows == 0)
    {
        echo 
    "<h4>Results</h4>";
        echo 
    "<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results</p>";

        
    // google
        
    echo "<p><a href=\"http://www.google.com/search?q=" 
            
    $trimmed "\" target=\"_blank\" title=\"Look up 
            " 
    $trimmed " on Google\">Click here</a> to try the 
            search on google</p>"
    ;
    }

    // next determine if s has been passed to script, if not use 0
    if (empty($s)) {
        
    $s=0;
    }

    // get results
    $query .= " limit $s,$limit";
    $result mysql_query($query) or die("Couldn't execute query");

    // display what the person searched for
    echo "<p>You searched for: &quot;" $var "&quot;</p>";

    // begin to show results set
    echo "<p>Results  </p>";
    $count $s ;

    // now you can display the results returned
    while ($rowmysql_fetch_array($result)) {
        
    $name $row["name"];
        
    $othername $row["detail"];
        
    $id $row["id"];

        echo    
    $count .  $name " <a href=index.php?id=" 
            
    $id " style=text-transform:capitalize; >" 
            
    $name  .   " - " $othername .  "</a> <br /> " ;
            
        
    $count++ ;
    }

    $currPage = (($s/$limit) + 1);

    //break before paging
    echo "<br />";

    // next we need to do the links to other results
    if ($s>=1) { // bypass PREV link if s is 0
        
    $prevs=($s-$limit);
        print 
    "&nbsp;<a href=\"$page?s=$prevs&q=$var\">&lt;&lt; 
            Prev 10</a>&nbsp&nbsp;"
    ;
    }

    // calculate number of pages needing links
    $pages=intval($numrows/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division
    if ($numrows%$limit) {
        
    // has remainder so add one page
        
    $pages++;
    }

    // check to see if last page
    if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
        
    // not last page so give NEXT link
        
    $news=$s+$limit;

        echo 
    "&nbsp;<a href=\"$page?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
    }

    $a $s + ($limit) ;
    if (
    $a $numrows) { 
        
    $a $numrows 
    }

    $b $s ;

    echo 
    "<p>Showing results $b to $a of $numrows</p>";

    ?>
    Last edited by Phill Pafford; 10-02-2008 at 12:57 PM.

  3. #3
    Join Date
    Sep 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    165

    premature question

    Hi there,

    the code itself is not as error-prone as it looks like.

    However, you should NOT query the database twice, you should NOT evaluate all those $_GET stuff thru 15 lines of code, or so, and you should get rid of that fatal error (division by zero) limit/s, and so on. I'm in the middle of a thing. If it is still not working next time I drop by, I'll send you the code.

    later and best.
    ariell
    programming is understanding

    my most recent baby The Beatles - Fab Four beatlescomplete.com (now with content!)
    if u need help with utf, watch this first Hebrew/American Chars from same table (not very nice, but a working real life example)

  4. #4
    Join Date
    Apr 2006
    Posts
    77
    Thanks guyz
    When, i try Phils code it does the same as before, it doesnt show any error messages, when you click the next button that I could give you, it literally does nothing apart from refresh the page and display: ?s=10&q=e after the url in the address bar

    Thanks Im very grateful and know Im a dunce :S

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13855 seconds
  • Memory Usage 2,903KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_html
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates