I have a PHP-based sorting method with a drop-down menu to sort rows. It works. I have another sorting links to sort id and title. It also works. But together they do not work.
When I sort (say by title) using links, the result gets sorted by title, then if I sort rows using my drop-down menu, rows get sorted, but the result gets set back to the default of id sort.
My sorting code for ID and title:
if ($orderby == 'title' && $sortby == 'asc')
{echo " <li id='scurrent'><a href='?rpp=$rowsperpage&order=title&sort=asc'>title-asc:</a></li> ";}
else {echo " <li><a href='?rpp=$rowsperpage&order=title&sort=asc'>title-asc:</a></li> ";}
if ($orderby == 'title' && $sortby == 'desc')
{echo " <li id='scurrent'><a href='?rpp=$rowsperpage&order=title&sort=desc'>title-desc:</a></li> ";}
else {echo " <li><a href='?rpp=$rowsperpage&order=title&sort=desc'>title-desc:</a></li> ";}
if ($orderby == 'id' && $sortby == 'asc')
{echo " <li id='scurrent'><a href='?rpp=$rowsperpage&order=id&sort=asc'>id-asc:</a></li> ";}
else {echo " <li><a href='?rpp=$rowsperpage&order=id&sort=asc'>id-asc:</a></li> ";}
if ($orderby == 'id' && $sortby == 'desc')
{echo " <li id='scurrent'><a href='?rpp=$rowsperpage&order=id&sort=desc'>id-desc:</a></li> ";}
else {echo " <li><a href='?rpp=$rowsperpage&order=id&sort=desc'>id-desc:</a></li> ";}
?>
My sorting codes for rows:
<form action="is-test.php" method="get">
<select name="rpp" onchange="this.form.submit()">
<option value="10" <?php if ($rowsperpage == 10) echo 'selected="selected"' ?>>10</option>
<option value="20" <?php if ($rowsperpage == 20) echo 'selected="selected"' ?>>20</option>
<option value="30" <?php if ($rowsperpage == 30) echo 'selected="selected"' ?>>30</option>
</select>
</form>
This method passes only rows per page (rpp) into url. I want it to pass order, sort, and rpp. Is there a way to pass these multiple values in form fields.
Are you allowing the user to change ordering as well?
If so, add another input for it of some input type.
If not, add a hidden input
<input type="hidden" name="sort" value="asc">or simply have the options manipulate the URL and not submit a form.Another Stackoverflow solution has something that works really similarly to this: https://stackoverflow.com/a/1091005/319066