SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I'm trying to make a search script to search my mySQL database.

    Here's my code:
    PHP Code:
    if ($searchwords) { 
    //Set this to the number of records per page 
    $limit 10


    //Set the initial offset 
    if (!isset($offset)) $offset 0

    $fieldstosearch = array("body"); 
    for(
    $i=0;$i<count($fieldstosearch);$i++) { 
    $clause .= sprintf("(%s LIKE '%%%s%%') OR "$fieldstosearch[$i], urldecode($searchwords)); 

    if(
    count($fieldstosearch) > 1

    $clause "(".substr($clause0, -4).")"

    else { 
    $clause substr($clause0, -4); 


    //print $clause; 

    $sql "SELECT COUNT(*) as totalcount from content WHERE $clause $where"
    $countq mysql_query($sql); 
    if(
    mysql_num_rows($countq) > 0) { 
    $totalcount mysql_result($countq0); 


    $realsql "SELECT LEFT(body,200), id, title, type from content WHERE $clause $where LIMIT $offset$limit"
    $result mysql_query($realsql); 
    if (
    mysql_num_rows($result) > 0) { 
    while(
    $row mysql_fetch_array($result)) { 

    $sresults $row["LEFT(body,200)"]; 
    $id $row["id"];
    $title $row["title"];
    $type $row["type"];
    $sresults eregi_replace("$searchwords","<b>$searchwords</B>",$sresults);

    echo 
    "<a href=article.php?action=content&type=$type&id=$id>$title</a><br>$sresults....
    "
    ;



    else { 
    echo 
    "Sorry No Records Found"


    print 
    get_nav($offset$limit$totalcount$searchwords); 



    function 
    get_nav($offset$limit$totalnum$searchwords "") { 
    global 
    $PHP_SELF
    if (
    $totalnum $limit) { 
    // calculate number of pages needing links 
    $pages intval($totalnum/$limit); 

    // $pages now contains int of pages needed unless there is a remainder from division 
    if ($totalnum%$limit$pages++; 

    if (
    $offset != 0
    {
        
    $boffset $offset-$limit;
    echo 
    "<a href=\"search.php?offset=$boffset&searchwords=$searchwords\"><<</a> ";
    }
    //the trouble part
    $noffset $pages*$limit-$limit;
    if (
    $offset != $noffset
    {
        
    $boffset $offset+$limit;
    echo 
    "<a href=\"search.php?offset=$boffset&searchwords=$searchwords\">>></a>";
    }

    //end of trouble part
    for ($i=1$i <= $pages$i++) { // loop thru 
    $newoffset=$limit*($i-1); 
    if (
    $newoffset != $offset) { 
    $return .= sprintf('<a href="%s?offset=%s&searchwords=%s">%s</a>%s'$PHP_SELF$newoffseturlencode($searchwords), $i"\n"); 

    else { 
    $return .= sprintf(' %s %s'$i"\n"); 

    }
    $return .= sprintf(''); 


    else { 
    $return ""

    return 
    $return

    I'm trying to move the truble part after the numbers but I can't? It works fine in front of them. Every where I ty either gives me an error or doesn't work.

    Thanks,
    Justin Sampson
    Last edited by Justin Sampson; Feb 23, 2001 at 13:36.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On the lines where you echo those links you have to escape the double quotes

    Code:
    echo "<a href="search.php?offset=$boffset&searchwords=$searchwords"><<</a> ";
    should be

    Code:
    echo "<a href=\\"search.php?offset=$boffset&searchwords=$searchwords\\">&lt;&lt;</a> ";
    Dang the forums keeps taking my espace characters off the post

    What specific errors are you getting
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang the forums keeps taking my espace characters off the post

    Annoying isn't it...
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah, I was going to fix that, just wasnt my major problem

    Curntly if the search results are more then three pages it looks like this at the bottom:

    (say your on page 2)

    << >> 123

    I want it to look like:

    << 123 >>

    The arrows are the next and previuse page links and so are the numbers in the middle.

    The previuse page arrows work fine just the next page arrows are in the wrong spot.
    Last edited by Justin Sampson; Feb 23, 2001 at 14:08.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhhh,

    Yes you need to put the part that makes the numbers inside the two sets of arrows Also the way the code originated(I know because I wrote it) was for the arrow links getting returned with the numbered links and then printing the function call. So those echo lines should be

    PHP Code:
    if ($offset != 0
    {
        
    $boffset $offset-$limit;
    $return .= "<a href="search.php?offset=$boffset&searchwords=$searchwords"><<</a> ";
    }
    //the trouble part
    $noffset $pages*$limit-$limit;
    if (
    $offset != $noffset
    {
        
    $boffset $offset+$limit;
    $return .= "<a href="search.php?offset=$boffset&searchwords=$searchwords">>></a>";


    Since you mixed it up returning some and printing some inside the function they have gotten mucked up.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see Thanks once again freddy!


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
  •