Solved

HOW TO CARRY SORT ONTO THE NEXT PAGE VIA PAGINATION.

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

728 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