[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

HOW TO CARRY SORT ONTO THE NEXT PAGE VIA PAGINATION.

Posted on 2007-04-05
8
Medium Priority
?
237 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
5 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 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