Solved

HOW TO CARRY SORT ONTO THE NEXT PAGE VIA PAGINATION.

Posted on 2007-04-05
8
219 Views
Last Modified: 2013-12-12
Hello Experts,

Can anyone help understand how I can pass a SORT variable onto next page via pagination. This is my SORT column headings:

//SORT COLUMN HEADER ==================================
/*
      echo
      '<table border=0 width=100%>
      <tr>
      <td align=center width=5% align=center>
        <a href="'  . $_SERVER['PHP_SELF'] . '?tableorder=user_id">ID</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=Last_Name">&nbsp;LAST NAME</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=First_Name">&nbsp;FIRST NAME</a>
      </td>
      <td align=center width=10%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=status">&nbsp;STATUS</a>
      </td>
      <td align=center width=40%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=Department">&nbsp;DEPARTMENT</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=date_created">&nbsp;DATE CREATED</a>
      </td>
      </tr>
      </table>';
*/
//SORT COLUMN HEADER ==================================

I then GET sort variable:

$sort = $_GET['tableorder'];


And this is my pagination which is a for LOOP:

  echo '<table border="0" width="100%">
   <tr bgcolor=#ffcc99>
   
   <td align="right">PAGES: ';

  for($i = 1; $i <= $total_pages; $i++)
  {
      if($i == $page)
       {
        echo'&nbsp;';
      echo($i. '&nbsp;');
     }
       else
       {
     echo("<a href=\"$_SERVER[PHP_SELF]?page=$i&amp;search=$search\"> [$i]&nbsp; </a>");
     }
  }
echo
  "</td>
   </tr>
   </table>";

?>


How would I include the SORT variable into the FOR LOOP so when user clicks on the page number, SORT is carried onto next page.

Thanks.
0
Comment
Question by:combustion007
  • 3
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 18857016
Use:

echo("<a href=\"$_SERVER[PHP_SELF]?page=$i&amp;search=$search&amp;tableorder=$sort\"> [$i]&nbsp; </a>");

-r-
0
 

Author Comment

by:combustion007
ID: 18857884
Hello Roonaan,

Thank you help me out. I have plugged in the line and the page simply fails upon loading it, and if I keep the existing code, page loads up just fine. So I then took the variables from your suggested code and inserted into my line which does not fail but still cannot seem to carry out the SORT.

THIS IS THE ENTIRE CODE:

$limit                         =       25;
$sql                        =      "SELECT * FROM $tblname";
$result                        =      mysql_query($sql);
$total_rows                  =      mysql_num_rows($result);
      

$page=isset($_GET['page']) ? (int) $_GET['page'] : 1;
$order = isset($_GET['tableorder']? $_GET['tableorder'] : 'user_id';

            
      $limitvalue = $page * $limit - ($limit);
      $sql = "SELECT * FROM `$tblname` "; //Base query
      $sql .= " ORDER BY `$order` ASC "; // add Order BY
      $sql .= "LIMIT $limitvalue, $limit";
      $result = mysql_query($sql);  // do query

      $total_pages = $total_rows / $limit;
      $total_pages = ceil($total_pages);

      if (!$result)
      {
         die('Could not query:' . mysql_error());
      }
 //SORT COLUMN HEADER ==================================
      echo
      '<table border=0 width=100%>
      <tr>
      <td align=center width=5% align=center>
        <a href="'  . $_SERVER['PHP_SELF'] . '?tableorder=user_id">ID</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=Last_Name">&nbsp;LAST NAME</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=First_Name">&nbsp;FIRST NAME</a>
      </td>
      <td align=center width=10%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=status">&nbsp;STATUS</a>
      </td>
      <td align=center width=40%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=Department">&nbsp;DEPARTMENT</a>
      </td>
      <td align=center width=15%>
      <a href="' . $_SERVER['PHP_SELF'] . '?tableorder=date_created">&nbsp;DATE CREATED</a>
      </td>
      </tr>
      </table>';
//=======================================

      while ($row = mysql_fetch_array($result))
      {
      $userid = $row['user_id'];
      $lname = "<a href=test.php?id="      .$row['user_id'] . ">". $row['Last_Name']."</a>";
      $fname = $row['First_Name'];
      $status = $row['status'];
      $department = $row['Department'];
      $date_created = $row['date_created'];
      $date_created = date("m \- j \- Y", strtotime($date_created));
      }      
      echo "</table>";

      
      echo '
      <table border=0 width=100%>
      <tr align=right bgcolor=#ffcc99>
      <td>';
      
//===============PAGINATION STARTS HERE==================================
      for ($i = 1; $i <= $total_pages; $i++)
      {
            if($i == $page)
            {
            echo $i . "&nbsp";
            }
            else
            {

//========PAGINATION PAGE NUMBER, I ADDED THE VARIABLES HERE AS YOU HAD SUGGESTED
echo "
<a href='{$_SERVER['PHP_SELF']}?page=". $i . "&amp;search=$search&amp;tableorder=$sort'\> [". $i . "]&nbsp;</a>&nbsp;";
                  
            
            }
      }
      
      echo '
      </td>
      </tr>
      </table>';
?>

THANKS A LOT
0
 

Author Comment

by:combustion007
ID: 18858879
I have figured out the my glitch and have resolved this question. I would like this post to be closed.

Thanks.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18872094
I have no objections Vee_Mod.
0
 

Accepted Solution

by:
combustion007 earned 0 total points
ID: 18882629
Hello Vee Mod,

As you can see my original code, I was passing the search variable like this:

echo("<a href=\"$_SERVER[PHP_SELF]?page=$i&amp;search=$search\"> [$i]&nbsp; </a>");

and in my code I had declared $tableorder variable for the column sort links.

The only thing that I needed to do was to declare the sort with GET which I had not done. This is What I needed to do:

$search = isset($_Get['tableorder']) ? $_Get['tableorder'] : 'user_id';

And I was able to carry the SORT throught the pagination.

Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question