SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    {Passing a Query with PREV / NEXT

    Hello,

    I use the following PREV / NEXT code:

    PHP Code:

    // get results
    $result=mysql_query("select * from gtex where ID>1000 order by ID limit $offset,$limit");

    // now you can display the results returned
    $i 1;
    echo (
    ' <div align="center"> <table border="0" cellpadding="4" cellspacing="0" width="100%"> ');
    while ( 
    $row mysql_fetch_array$result ) )


    extract($row);


    echo (
    '<tr>');
    echo (
    "<td width='33%'><a href=../print_resume.php?ID=$ID target='_blank' style='color: #000080'><font face='Verdana' size='1'>$name</font></a></td>
    "
    );
    echo (
    '<td width="45%"><font face="Verdana" size="1">'.$row["description"].'</font></td>');
    echo (
    "<td width='22%' align='right'><a href=../mycoverletter.php?ID=$ID target='_blank' style='color: #CC3300'><font face='Verdana' size='1'>See
                  Cover Letter</font></a></td>"
    );
    echo (
    '</tr>');

    $i++;
    }
    echo 
    "</table>";

    // next we need to do the links to other results

    if ($offset==1) { // bypass PREV link if offset is 0
        
    $prevoffset=$offset-20;
        print 
    "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> &nbsp; \n";
    }

    // 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++;
    }

    for (
    $i=1;$i<=$pages;$i++) { // loop thru
        
    $newoffset=$limit*($i-1);
        print 
    "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> &nbsp; \n";
    }

    // check to see if last page
    if (!(($offset/$limit)==$pages) && $pages!=1) {
        
    // not last page so give NEXT link
        
    $newoffset=$offset+$limit;
        print 
    "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
    }

    ?> 
    The code works fine when I use this query :

    PHP Code:
    select from gtex where ID>1000 order by ID 
    But I want to use this query: (Before displaying the results, I want to give my users the possibility to choose search criteria : $category and $state)


    PHP Code:
    if($category<>"0" and $state<>"0"){
      
    $condition "state='$state' and category='$category'";
    }

    if(
    $category<>"0" and $state =="0" ){
      
    $condition "category='$category'" ;
    }

    // My Query
    $result=mysql_query("SELECT * FROM gtex WHERE $condition limit $offset,$limit"); 
    When I use the PREV / NEXT code with this query, it works for page #1, but the query is not passed on the other pages.
    What should I do to make it work for all the pages?
    Thank you !
    Ginie G.

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <form action="images.php" method="post">
      
    Max # to show: <input type="text" size="3" name="show"><br>
      
    Start with #: <input type="text" size="3" name="start"><br>
      
    <input type="submit">
    </
    form>

    // images.php
    $r mysql_query("SELECT * FROM images LIMIT '$start','$show'");

    if (
    $start == 0) {
      
    $n $start $show;
      echo 
    "Previous> &nbsp; ";
      echo 
    "<a href=\"" .$PHP_SELF"?start=" .$n"\">Next</a>";
    } else {
      
    $sm $start $show;
      
    $sa $start $show;
      echo 
    "<a href=\"" .$PHP_SELF"?start=" .$sa"\">Previous</a> &nbsp; ";
      echo 
    "<a href=\"" .$PHP_SELF"?start=" .$sa"\">Next</a>";

    Something like that shouls work.
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your answer, notepade_coder.

    When I do that, the query

    PHP Code:
    if($category<>"0" and $state<>"0"){
      
    $condition "state='$state' and category='$category'";
    }

    if(
    $category<>"0" and $state =="0" ){
      
    $condition "category='$category'" ;
    }

    // My Query
    $result=mysql_query("SELECT * FROM gtex WHERE $condition limit $offset,$limit"); 

    is still not passed onto the next pages...
    What can I do?
    Ginie G.

  4. #4
    SitePoint Addict Viral's Avatar
    Join Date
    Nov 2001
    Location
    Washington DC
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by notepad_coder
    PHP Code:
      echo "<a href=\"" .$PHP_SELF"?start=" .$sa"\">Next</a>"
    Ginieg - see that? when you click Next, it passes back to the same script and creates a variable called $start that contains the starting point of that current page. It will use that var to create more Next/Prev buttons based on its current starting place. It should work fine.

    btw, just a coding tip, you don't have to dump out of a double quoted echo or print to use vars.:
    echo "<a href='$PHP_SELF?start=$sa'>Next</a>";
    It's MUCH faster and easier to read that way.

    --Viral
    A computer without Windows is like a chocolate cake without mustard.

  5. #5
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Viral,

    Thank you for your answer! Huummm. It still doesn't work... Here is my complete code:

    PHP Code:
    <?php 

    if (!isset($search)):

    ?>

    <html>
    <head>
    <form method="POST" ACTION="<?PHP_SELF?>" >

    // Choose category
    <select name="category">
    <OPTION VALUE="Accounting">Accounting</OPTION>
    <OPTION VALUE="Arts">Arts</OPTION>
    </SELECT
    <br>

    // Choose State
    <select name="state" >
    <option VALUE="0">Any State</option>
    <option value="AL">AL</option>
    <option value="WY">WY</option>
    </select>       

    <input type="submit" name="search" 
    </form>
           
    </body>
    </html>

    <?php

    else:

    dbConnect('database');

    if(
    $category<>"0" and $state<>"0"){
      
    $condition "state='$state' and category='$category'";
    }

    if(
    $category<>"0" and $state =="0" ){
      
    $condition "category='$category'" ;
    }

    $show=20// rows to return
    $numresults=mysql_query("select * FROM gtex WHERE $condition");

    $numrows=mysql_num_rows($numresults);

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

    // get results
    $result=mysql_query("select * FROM gtex WHERE $condition limit $start,$show");

    // now you can display the results returned
    $i 1;
    while ( 
    $row mysql_fetch_array$result ) )


    // ... display the results ....

    $i++;
    }


    if (
    $start == 0) {
      
    $n $start $show;
      echo 
    "Previous> &nbsp; ";
      echo 
    "<a href=\"" .$PHP_SELF"?search=1&start=" .$n"\">Next</a>";
    } else {
      
    $sm $start $show;
      
    $sa $start $show;
      echo 
    "<a href=\"" .$PHP_SELF"?search=1&start=" .$sa"\">Previous</a> &nbsp; ";
      echo 
    "<a href=\"" .$PHP_SELF"?search=1&start=" .$sa"\">Next</a>";
    }

    endif;

    ?>
    I am not sure about the $search=1 in the
    PHP Code:
     echo "<a href=\"" .$PHP_SELF"?search=1&start=" .$sa"\">Next</a>"
    Can you help?
    Thank you!
    Ginie G.

  6. #6
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use " .$var. " for easier viewing.

    ginieg
    You either have to change the names on the form or change the variables in your script, when I did that example I use the form:
    Code:
    <form action="images.php" method="post">
      Max # to show: <input type="text" size="3" name="show"><br>
      Start with #: <input type="text" size="3" name="start"><br>
      <input type="submit">
    </form>
    Notice "strat" and "show".
    Last edited by notepad_coder; May 1, 2002 at 14:54.
    - the lid is off the maple syrup again!

  7. #7
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been looking at your code and you want to let users select the amount of information outputted and you dont have anyplace in there to let them do so?

    I would do:
    PHP Code:
    <?
    if (!IsSet($search)) {
    ?>
    <html>
      <head></head>
      <body>
      <form action="<? echo $PHP_SELF?>" method="post">
        // Choose category
        <select name="category">
          <option value="Accounting">Accounting</option>
          <option value="Arts">Arts</option>
        </select>
        <br>
        // Choose State
        <select name="state">
          <option VALUE="other">Any State</option>
          <option value="AL">AL</option>
          <option value="WY">WY</option>
        </select>
        <br>
        Rows per Page:<input type="text" name="pp" size="3">
        Start on:<input type="text" name="list" size="3">
        <input type="text" name="search" value="1">      
        <input type="submit"> 
      </form>       
      </body>
    </html>
    <?
    } else {
      
    dbConnect('database');
      if(
    $state != "other"){
        
    $condition "state='$state' AND category='$category';
      } else {
        
    $condition = "category='$category'";
      }
      
    $show = $pp; // rows to return
      
    $numresults = mysql_query("SELECT FROM gtex WHERE '$condition'");
      
    $numrows = mysql_num_rows($numresults);
      // next determine if offset has been passed to script, if not use 0
      if (IsSet(
    $list)) {
        
    $list = $list + $show;
      } else {
        
    $list = $list;
      }
      // get results
      
    $result = mysql_query("SELECT FROM gtex WHERE '$condition' LIMIT $list,$show");
      // now you can display the results returned
      
    $i = 1;
      while (
    $row = mysql_fetch_array($result)) {
        // ... display the results ....
        
    $i++;
      }
      if (
    $list == 0) {
        
    $n = $list + $list;
        echo "
    Previous &nbsp";
        echo "
    <a href=\"" .$PHP_SELF"?search=1&list=" .$n"\">Next</a>";
      } else {
        
    $p $list $show;
        
    $n $list $show;
        echo 
    "<a href=\"" .$PHP_SELF"?search=1&list=" .$prev"\">Previous</a> &nbsp; ";
        echo 
    "<a href=\"" .$PHP_SELF"?search=1&list=" .$next"\">Next</a>";
      }
    }
    ?>
    And why are you using:
    PHP Code:
    $numresults mysql_query("SELECT * FROM gtex WHERE '$condition'");
    and 
    then
    $result 
    mysql_query("SELECT * FROM gtex WHERE '$condition' LIMIT $list,$show"); 
    Wouldnt it be better to only use it once?
    Also check out http://sitepointforums.com/showthrea...815#post428815 , I went ove this there too.
    Last edited by notepad_coder; May 1, 2002 at 23:15.
    - the lid is off the maple syrup again!


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
  •