Good morning everyone!

I've been working on a little project and I thought I was done. So, I have a bunch of pages, but I have one where there's a MYSQLi query with over 100k results, I figured it would be nice not to display all 100k+ results, so I added pagination to the site. The problem I am having is, whenever someone sorts the page and clicks next page, the sort goes away.

As I was writing this, I kind of sort of figured it out, but here's my code:

```
$sort = "id";
    if(isset($_GET['sort'])) {
        switch ($_GET['sort'] ) { 

        case 0:
                    $sort = 'id';
                    break;
        case 1:
                    $sort = 'priority DESC';
                    break;
        case 2:
                    $sort = 't_name'; 
                    break;  
        case 3:
                    $sort = 'loc'; 
                    break;
        case 4:
                    $sort = 'w_req';
                    break;
        case 5:
                    $sort = 'tel';
                    break;
        case 6:
                    $sort = 'maint_user';
                    break;
        }
    }

        // pagination
        $total_results = mysqli_num_rows(mysqli_query($mysqli,"SELECT w_status FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort"));

        if(!isset($page_number))
            $page_number = (int)$_GET['page'] <= 0 ? 1 : (int)$_GET['page']; // grab the page number

            $perpage = 15; // number of elements perpage

        if($page_number > ceil($total_results/$perpage))
            $page_number = ceil($total_results/$perpage);

        $start = ($page_number - 1) * $perpage;

        $result = mysqli_query($mysqli,"SELECT * FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort LIMIT $start, $perpage");

```

I think my issue is that $sort= "id"; and every time I click on next, the $sort gets reset to = "id" So I am thinking, maybe include the $sort in the pagination code? I appreciate any help I can get. Thank you :)

Here's the html:

```
<form action="re_assign_a.php" method="post" name="view_order">    

  <table border="0" width="100%">
    <tr>
        <td align="left"><a href="<?php echo $_SERVER['PHP_SELF'] . "?page=".($page_number-1)."'";?>" class="button_s">Previous</a></td>
        <td align="right"><a href="<?php echo $_SERVER['PHP_SELF'] . "?page=".($page_number+1)."'";?>" class="button_s">Next</a></td>
    </tr>
  </table>
  <br>           
  <table border='2' style='width: 100%; margin: auto; border-width: 1px'>
    <tr>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>" title="Click to sort by order number." class="tooltip"><span title="More">Order #</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>" title="Click to sort by Priority." class="tooltip"><span title="More">Priority</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>" title="Click to sort by Tenant Name." class="tooltip"><span title="More">Tenant Name</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>" title="Click to sort by Apartment/Location" class="tooltip"><span title="More">Apartment</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>" title="Click to sort by Work Requested" class="tooltip"><span title="More">Work Requested</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>" title="Click here to sort by Phone Number" class="tooltip"><span title="More">Resident Phone #</span></a></th>
        <th><a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>" title="Click to sort by a good date to perform work" class="tooltip"><span title="More">Assigned to</span></a></th>
        <th>Check to Re-Assign</th>
    </tr>

 <?php

 $i = 0;
 while($row = mysqli_fetch_array($result))
    {
        $test_id = $row['id'];
        echo "<tr class='table_work'>";
        echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['id'] . "</td>";
        echo
       "<td align='center'><a href='one_order.php?id=$test_id'>"; 
       if ($row['priority'] == '1') { echo "<div class='priority1'>&nbsp;</div>"; } elseif ($row['priority'] == '2') { echo "<div class='priority2'>&nbsp;</div>"; } elseif ($row['priority'] == '3') { echo "<div class='priority3'>&nbsp;</div>"; };
       echo "</a></td>
        <td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['t_name']); echo "</a></td>
        <td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['loc']); echo "</a></td>
        <td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo($row['w_req']); echo "</a></td>";
        echo "<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo2($row['tel']); echo "</a></td>";
        echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['maint_user'] . "</a></td>";
        echo "<td align='center'><input name='assign[$i]' type='checkbox' value='" . $row['id'] . "' ></td>";
        ++$i;
    }

 ?>

   </table><br>
   <table border="0" width="100%">
   <tr>
   <td align="left"><a href="<?php echo $_SERVER['PHP_SELF'] . "?page=".($page_number-1)."'";?>" class="button_s">Previous</a></td>
   <td align="right"><a href="<?php echo $_SERVER['PHP_SELF'] . "?page=".($page_number+1)."'";?>" class="button_s">Next</a></td>
   </tr>
   </table>

```