Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2009
    Location
    MA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP MySQL search engine issues

    A site of mine has now grown enough to where I needed to implement a search feature, so people can simply type a word/phrase into the search field and it will bring up every post with what they searched for.

    I am having some issues with it. It does work, but the pagination is screwy. It seems to only be the pagination links (such as: Next, Previous, First, Last, etc.). Anyway, I am pretty sure you guys understand what the pagination is supposed to do so, here's some code:

    The simple form:
    Code:
    <form method="post" action="search.php" id="searchform">
    <input type="text" name="keywords" />
    <input type="submit" name="searchsubmit" value="search" />
    </form>
    the search.php (this is what puts the functions together and displays the info):
    PHP Code:
    // connect to the db
    db_connect();
    $keywords = $_POST['keywords'];
    // And we remind them what they searched for
    echo '<h1 style="padding: 0px 0px 8px; color: #F68A20; font-size: 24px;"><b>Searched For</b> 
    <span style="font-size: 12px; font-weight: normal; color: #000000;">[ '. $keywords .' ]</span></h1>';
    ?>
    <div class="pagination">
        <?php
        
    echo pagination_search();
        
    ?>
    </div>
    <?php
    $submit 
    $_POST['searchsubmit'];

    if (
    $submit)
        {
        echo 
    "Results:<br />";
        
    $keywords strip_tags($keywords);
        
    $keywords trim($keywords);
        if (
    $keywords == "")
            {
            echo 
    "You forgot to enter a search term.<br />";
            }
        
    // if they did not enter a search term, give them an error
        
    $sql "SELECT * FROM `posts` WHERE `content` LIKE '%$keywords%'";
        
    $result mysql_query($sql);
        
    $anymatches mysql_num_rows($result);
        if (
    $anymatches 0)
            {
            
    // now we search for our search term
            
    echo get_entries_search();
            }
        else
            {
            echo 
    "Sorry, but we could not find an entry to match your search.<br /><br />";
            }
        }
    and the functions, one to get the entries, one for pagination:
    PHP Code:
    function get_entries_search()
        {
        
    $connection db_connect();
        
        
    /****** pagination ******/
        // find out how many rows are in the table
        
    $keywords $_POST['keywords'];
        
    $sql "SELECT COUNT(*) FROM `posts` WHERE `content` LIKE '%$keywords%'";
        
        
    $testsearch $_REQUEST['testsearch'];
        if (
    $testsearch != "")
            {
            
    $keywords $testsearch;
            }

        
    $result mysql_query($sql$connection);
        
    $r mysql_fetch_row($result);
        
    $numrows $r[0];

        
    // number of rows to show per page
        
    $rowsperpage 10;
        
    //find out total pages
        
    $totalpages ceil($numrows $rowsperpage);

        
    // get the current page or set a default
        
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
            {
            
    // cast var as int
            
    $currentpage $_GET['currentpage'];
            }
        else
            {
            
    // default page num
            
    $currentpage 1;
            }
            
        
    // if current page is greater than total pages...
        
    if ($currentpage $totalpages)
            {
            
    // set current page to last page
            
    $currentpage $totalpages;
            }

        
    // if current page is less than first page...
        
    if ($currentpage 1)
            {
            
    // set current page to first page
            
    $currentpage 1;
            }

        
    // the offset of the list, based on current page
        
    $offset = ($currentpage 1) * $rowsperpage;
        
    /****** end pagination ******/
        
        // get info from the db
        
    $query "SELECT posts.id, posts.content, posts.name, posts.datetime, posts.category, posts.comments, posts.votes1, posts.votes2, posts.votes3, categories.category
                  FROM `posts` LEFT OUTER JOIN `categories`
                  ON posts.category = categories.cat_id "
    ;

        if (
    $cat_id $_REQUEST['cat_id'])
            {
            if (
    is_int($cat_id))
                {
                
    $query .= " WHERE categories.cat_id = "$cat_id;
                }
            else
                {
                
    $query .= " WHERE categories.category = \"";
                
    $query .= mysql_real_escape_string($cat_id);
                
    $query .= "\"";
                }
            }
        
        
    $query .= " WHERE posts.content LIKE '%$keywords%'";
        
    $query .= " ORDER BY `id` DESC LIMIT "$offset .", "$rowsperpage;
        
    $result mysql_query($query$connection);

        return 
    create_output($result);
        }

    function 
    pagination_search()
        {
        
    $connection db_connect();

        
    /****** pagination ******/
        // find out how many rows are in the table
        
    $keywords $_POST['keywords'];
        
    $sql "SELECT COUNT(*) FROM `posts` WHERE `content` LIKE '%$keywords%'";

        
    $testsearch $_REQUEST['testsearch'];
        if (
    $testsearch != "")
            {
            
    $keywords $testsearch;
            }

        
    $result mysql_query($sql$connection);
        
    $r mysql_fetch_row($result);
        
    $numrows $r[0];

        
    // number of rows to show per page
        
    $rowsperpage 10;
        
    //find out total pages
        
    $totalpages ceil($numrows $rowsperpage);

        
    // get the current page or set a default
        
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
            {
            
    // cast var as int
            
    $currentpage $_GET['currentpage'];
            }
        else
            {
            
    // default page num
            
    $currentpage 1;
            }
                
        
    // if current page is greater than total pages...
        
    if ($currentpage $totalpages)
            {
            
    // set current page to last page
            
    $currentpage $totalpages;
            }

        
    // if current page is less than first page...
        
    if ($currentpage 1)
            {
            
    // set current page to first page
            
    $currentpage 1;
            }

        
    // the offset of the list, based on current page
        //$offset = ($currentpage - 1) * $rowsperpage;

        /******  build the pagination links ******/
        // if not on page 1, don't show back links

        
    if ($currentpage 1)
            {
            
    // show << link to go back to page 1
            
    echo "<div class='paginleft'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=1'>First</a></div>";
            }
        else
            {
            echo 
    "<div class='paginleft'>First</div>";
            }

        
    // if not on last page, show forward and last page links
        
    if ($currentpage != $totalpages)
            {
            
    // get next page
            
    $nextpage $currentpage 1;
            
    // echo forward link for next page
            
    echo "<div class='paginright'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=$nextpage'>Next</a></div>";
            }
        else
            {
            echo 
    "<div class='paginright'>Next</div>";
            }
        
        
    // if not on page 1, don't show back links
        
    if ($currentpage 1)
            {
            
    // get previous page num
            
    $prevpage $currentpage 1;
            
    // show < link to go back to 1 page
            
    echo "<div class='paginright'>|</div>";
            echo 
    "<div class='paginright'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=$prevpage'>Previous</a></div>";
            }
        else
            {
            echo 
    "<div class='paginright'>|</div>";
            echo 
    "<div class='paginright'>Previous</div>";
            }

        
    // range of num links to show
        
    $range 5;
        
    $range2 6;

        
    // loop to show links to range of pages around current page
        
    for ($x = ($currentpage $range); $x < (($currentpage $range2) + 1); $x++)
            {
            
    // if it's a valid page number...
            
    if (($x 0) && ($x <= $totalpages))
                {
                
    // if we're on current page...
                
    if ($x == $currentpage)
                    {
                    
    // highlight it but don't make a link
                    
    echo "<div class='paginx'><b>$x</b></div>";
                    }
                
    // if not current page
                
    else
                    {
                    
    // make it a link
                    
    echo "<div class='paginx'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=$x'>$x</a></div>";
                    }
                }
            }

        if (
    $currentpage != $totalpages)
            {
            if (
    $totalpages 7)
                {
                if (
    $totalpages < (($currentpage $range2) + 1))
                    {
                    echo 
    "";
                    }
                else
                    {
                    echo 
    "<div class='paginx'> ... </div> ";
                    echo 
    "<div class='paginx'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=$totalpages'>$totalpages</a></div> ";
                    }
                }
            
    // echo forward link for lastpage
            
    echo "<div class='paginlast'><a href='search.php?action=get_entries_search&testsearch=$keywords&currentpage=$totalpages'>Last</a></div> ";
            }
        else
            {
            echo 
    "<div class='paginlast'>Last</div>";
            }
        } 
    As I said, I believe the only problem is with the linkage in the pagination.. it's the only part I'm not quite sure about.

  • #2
    New Coder
    Join Date
    Jun 2009
    Location
    MA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've solved it on my own


  •  

    Posting Permissions

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