?
Solved

HOW TO CARRY SORT ONTO THE NEXT PAGE VIA PAGINATION.

Posted on 2007-04-05
8
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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