Solved

HOW TO CARRY SORT ONTO THE NEXT PAGE VIA PAGINATION.

Posted on 2007-04-05
8
216 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

770 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