Advanced Search

Results 1 to 10 of 10

Thread: Create A Simple Search Page

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create A Simple Search Page

    I am trying to create a simple search page which can search from a table in my database called 'tblmember', I want to have a filed where I can type antyhing and it will search the entire table but not sure how to achive this... Any ideas?

  2. #2
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What you can do is create a script that searches for whatever field you want (ex: first name). The script will then search all the first names for a matching name. Here is a search script I made to search jobs by title or user. It will need to be changed around a bit to fit your needs, but here is what I have (sorry about the sloppy coding, made it a while back):
    PHP Code:
    <?php

    // If form is not active
    if(!$_POST['search']) {
    ?>
    <form method="post" action="">
        <b>Search Jobs:</b>
            <br />
        <input type="text" name="search_field" class="input" />
    <br />
        Search By: <select name="searches" class="input">
          <option value="title" selected="selected">Title</option>
          <option value="username">Username</option>
                   </select>
          <input type="submit" name="search" value="Search Jobs" />
    </form>
    <?
    }

    // If form is active
    if($_POST['search']) {
        
    $data $_POST['search_field'];
        
        
    // Check fields
        
    if(!$search_field) {
            echo 
    '
                <meta http-equiv="refresh" content="2;URL=index.php?act=search" />'
    ;
            
            die(
    '
                <div class="message_urgent">
                    Please fill in something to search for
                </div>
            '
    );
        }
        
    ########## Get What To Search By ##########
    // If search set to title
        
    if($_POST['searches'] == "title") {
            
    // Query to get results
            
    $sql "SELECT * FROM `jobs` WHERE title = '$data' ORDER BY id DESC";
            
    $result mysql_query($sql) or die ('Error Searching Jobs! <br />' .mysql_error());
            
    $results mysql_num_rows($result);
        }
    // If search set to username
        
    if($_POST['searches'] == "username") {
            
    $sql "SELECT * FROM `jobs` WHERE username = '$data' ORDER BY id DESC";
            
    $result mysql_query($sql) or die ('Error Searching Jobs! <br />' .mysql_error());
            
    $results mysql_num_rows($result);
        }
    ########## End Search Type Script ##########



    // If there are no results
    if($results 1) {
        echo 
    '<span style="color: #FF0000">There were no jobs in the database with the title or username <b><i>'.$data.'</i></b></span>.
            <br />
        <a href="'
    .$_SERVER['REQUEST_URI'].'">Search Again</a>';
    }

    // If results are found
    if($results >= 1) {
        echo 
    '
            <div class="message">
                Showing search results for <b>'
    .$_POST['search_field'].'</b> searched in <i>'.$_POST['searches'].'</i>
            </div>
                <br />
        '
    ;
        
        while(
    $r mysql_fetch_array($result)) {
            
        echo 
    '<a href="index.php?act=manage&action=view&id='.$r['id'].'"><b>'.$r['title'].'</b></a> - '.$r['date'].' - <i>'.$r['price'].'</i>
            <br />
                '
    .$r['description'].'
            <br /><br />'
    ;
        }
    }
    }

    ?>
    Hope that helps, if you need something explained, just let me know.
    Thanks DD, you saved me countless times

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
        echo '<a href="index.php?act=manage&action=view&id='.$r['id'].'"><b>'.$r['title'].'</b></a> - '.$r['date'].' - <i>'.$r['price'].'</i>
    That won't do anything unless the index.php is hooked up properly to the according variables.
    - Mike

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt View Post
    That won't do anything unless the index.php is hooked up properly to the according variables.
    Yes, I didn't see a reason to post the view page or calling scripts because it wouldn't really help him out
    Thanks DD, you saved me countless times

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Also, for a more productive search, why not try this in your sql query:

    Code:
    $sql = "SELECT * FROM `jobs` WHERE `username` LIKE '&#37;$data%' ORDER BY id DESC";
    That way, if the user types in ad in the text field, it will return any values with that in it (such as "adam", "mad","glad", etc).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post PHP Tutorials

    Dear Sir I want to learn PHP from the basic. I am a new comer in this forum. Please tell me the best website which teach me PHP step by step lessons.

    Thanks

    Allah Hafiz

    Mubashar Gilani

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What should the page index.php contain? Can you maybe provide an example if possible? I added:
    PHP Code:
    <?php
        $data 
    mysql_connect('localhost''...''...') or die (mysql_error ());
        
    mysql_select_db ('...'$data) or die (mysql_error ());
        
    ?>
    before. Do I need to add anything else?
    Last edited by ski; 12-02-2007 at 11:46 PM.

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    That all depends on what it is that you are trying to do exactly. If you are making a search, you also need to have a query to the "search" table in there.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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