SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    search engine and prev next button

    how does one code the the previos and next buttons, when the users have almost 10check boxes to choose from and 5text fields to choose from? How do you parse your entire query from one page to the other?


    kunal
    i dunno...

  2. #2
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the same problem, but I found a few months ago this on this board in this forum:

    PHP Code:
    <?
    $limit
    =10;
    if (empty(
    $offset)){ $offset=0; }
    $result mysql_query("SELECT * FROM $table[posts] WHERE threadid='$threadid' ORDER BY post_time"); 
    $numrows mysql_result($result00); 

    //get results
    $result=mysql_query("select id,name,phone ".
                            
    "from TABLE where YOUR CONDITIONAL HERE ".
                                
    "order by WHATEVER ".
                                    
    "limit $offset,$limit");

    //now you can display the results returned
    while ($data=mysql_fetch_array($result)) {
        
    //include code to display results as you see fit
    }

    //calculate number of pages needing links
    $pages=intval($numrows/$limit);
            
    //detemine if we need to add a page for a last page with less than the full number of rows
    if ($numrows%$limit) {
        
    //If there was a remainder we add a row
        
    $pages++;
    }

    //set up variable equal to the record number of the first displayed on current page        
    $first_record $offset 1;

    //start displaying the Next/Previous links
    echo "Now viewing: $first_record -";

    //Check to see if it the last page
    if (!((($offset)/$limit)+1==$pages)) {
        
    //not last page so give so last record on page is $offset + $limit
        
    $last_record $offset $limit;
        echo 
    $last_record of $numrows items &nbsp; &nbsp; ";
    } else {
        
    //is the last page so last record on page is $numrows
        
    echo $numrows of $numrows item(s) &nbsp; &nbsp; ";
    }
                
    //next we need to do the links to other results
    if ($offset != 0) { //bypass PREV link if offset is 0
        
    $prevoffset=$offset-$limit;
        echo 
    "<a href=\"$PHP_SELF?offset=$prevoffset\">Prev</a> &nbsp; \n";
    }

    //if there is only 1 page we need no numbered links.        
    if ( $pages != ) {
        for (
    $i=1;$i<=$pages;$i++) { //loop thru when more than one page
            
    $newoffset=$limit*($i-1);
            
    //echo the number of the currently viewed page without a hyperlink
            
    if ( ((($offset)/$limit)==($i-1)) ) {
                echo 
    "$i &nbsp; \n";
            } else {
                echo 
    "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> &nbsp; \n";
            }
        }
    }
            
    //check to see if last page
    if (!((($offset)/$limit)+1==$pages) && $pages!=1) {
        
    //not last page so give NEXT link
        
    $newoffset=$offset+$limit;
        echo 
    "<a href=\"$PHP_SELF?offset=$newoffset\">Next</a><p>\n";
    }
    ?>
    I'm sorry for the autor, I don't know his name anymore, but he did post this in a previous thread!

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This piece of code was from freddy I think

    PHP Code:
    if(!isset($offset)) $offset 0
    $recordsperpage 10
    $result mysql_query("SELECT COUNT(*) as totalnum from items"); 
    $row mysql_fetch_array($result); 
    $totalrecords $row["totalnum"]; 

    $result mysql_query("SELECT * from items order by date DESC LIMIT $offset$recordsperpage"); 

    while (
    $row mysql_fetch_array($result))
        { 
        print 
    $row["title"];
        print 
    "\n <br>";
        } 

    if (
    $totalrecords $offset)
        { 
        
    $newoffset $offset $recordsperpage
        
    printf('<a href="%s?offset=%s">more &gt;&gt;</a>'$PHP_SELF$newoffset); 
        } 

    if (
    $offset >= $recordsperpage)
        { 
        
    $newoffset $offset $recordsperpage
        
    printf('<a href="%s?offset=%s">back &lt;&lt;</a>'$PHP_SELF$newoffset); 
        } 

  4. #4
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,.... thanx for the all code..but this does it only for one where condition.. what if there were more??
    i dunno...

  5. #5
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you mean??

    You can add more than one WHERE condition...


    PHP Code:
    <?
    $query 
    mysql_query("SELECT * FROM a_table WHERE condition1='1' AND condition2='2' OR condition3='3'");
    ?>

  6. #6
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you can do that for the first page... but how will you parse those variables to the next page???
    i dunno...

  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know quite see what the problem is, as long as the query give a set of results, the offset will work, say you have multiple WHERE clauses, the query will be:

    PHP Code:
    $query mysql_query("SELECT * FROM a_table WHERE condition1='1' AND condition2='2' OR condition3='3' LIMIT $offset$recordsperpage"); 
    And everytime the next link is click, it'll query MySQL and fetch the next ten results, perhaps you could further explain the problem?

  8. #8
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok.. let me explain myself a little better....

    $test = "Looking for stuff";
    $using = "And";
    $date = "3days old";

    the above is the information, that user submitted using a form...


    you run your query and get the first 10results... now, when you want to get the next 10 results.. you ask the user to click on a button, "next"... when he clicks on this button, the original mysql query button gets reset and needs to be re-written, correct? and for this re-writing, we need the orginal information that was entered by the user?

    my question, how did you parse the orginal information to the next page? using the variables in the next link, is not an option considering the amout of data they can enter in there query string.

    kunal
    i dunno...

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you'll just have to run the query every page and send the page info in the url for instance searchresults.php?page=3

    then you can work out what results to show via LIMIT as above...

    the results should always be the same if you use the same query to thedb, assuming you don't add/delete stuff from the db, but having to re-query the db should solve your probs...

    beachball

  10. #10
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wihtout reading hte previous posts in this thread..here's how you parse variables thorugh a link.

    $test = "Looking for stuff";
    $using = "And";
    $date = "3days old";

    http://www.domain.com/script.php?test=Looking%20for%20stuff&using=And&date=3days%20old
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  11. #11
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lynlimz, there is a restriction on the amount of text you can parse in the url.
    i dunno...

  12. #12
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I see what you mean

    I don't see another solution than use the querystring in your next / previous links.

    PHP Code:
    <?
    $qs 
    $QUERY_STRING;

    echo 
    "<a href=\"?$qs&offset=10\">Next</a>";
    ?>
    This should do the trick.

    Maybe there are other solutions (sessions or cookies), but you shouldn't use that, it's not recommended for a simple search... Even Yahoo takes the searchquery (p=yahoo) in the url:
    Code:
    <a href="http://search.yahoo.com/search?p=yahoo&hc=20&hs=2215&b=21&h=s">Next 20 Site Matches</a>

  13. #13
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by kunal
    lynlimz, there is a restriction on the amount of text you can parse in the url.
    Yeah. I wasn't expecting you to parse an entire paragrah or something. just the basic like action name...offset number etc.

    Another solution would be to build a form submit button, and ultilising the <input type="hidden"...> types...though it'll be ugly. cna't hink of any other way besides these 2 though.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  14. #14
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. im thinking cookies..

    but the submision but is a good idea.. one doesnt have to use the button perse right? they can use some text to submit too?
    i dunno...

  15. #15
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you need to pass is the variable names and their values, only the ones that are used in the query, it shouldn't even come close to the 255 char limit on GET strings. What is the basis of your query, you certianly aren't using 255 char long strings in yoru query are you?

    Cookies, bad idea, when all you want to do is pass some vars to each page. Search this forum there are tons of examples on hwo to achieve this.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  16. #16
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a note...about security.

    you shouldn't parse variables for your mysql_query like..

    http://domain.com/script.php?where=hello

    mysql_query("SELECT * FROM tablename WHERE='$where'");

    bad practise. as people can code directly into your mysql_query.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  17. #17
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by lynlimz
    just a note...about security.

    you shouldn't parse variables for your mysql_query like..

    http://domain.com/script.php?where=hello

    mysql_query("SELECT * FROM tablename WHERE='$where'");

    bad practise. as people can code directly into your mysql_query.

    yup... which is also one of the reasons, why I dont wanna parse the text using the link... hmmm... i think ill just use a session variable..
    i dunno...

  18. #18
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes..but then again, it isn't a neccessity to use a session variable. parsing a few simple variables via the query string is all too normal.

    what you would do is like have some error checking for the variables..
    for example, i have a script which have differnet access controls.

    while i gather the access level form the query string /or the input tyoe hidden field, back in the scirpt i also verify the access status. just some security measures.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  19. #19
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just passing variables in the url string alone is no security hazard. In fact passing things like offset and and small things like for instance City=Chicago isn't either. Furthermore like lynlimz said, you should be checking GET variables anyway, if you are not then you are asking for trouble.

    Doing something like page=index.php

    <?
    include($page);
    ?>

    is a security hazard but passing around stupid criteria for a query is not. If someone can maliciously embed some php code in a GET variable then have it screw with your database, then your script is poorly coded from the start. You should not need sessions for this type of feature.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •