Hello everyone

I have a hard time making this work..

I have some data that i wish to sort by date , and i would like to sort them asc and desc using a jump menu.

I know that within the jump menu i have to call 2 queries to the database (asc and desc) but i came to the conclusion that i dont know how to do that.

Please im begginig for you help :)

Dani AI

Generated

Nice progress — ’s suggestion to echo selected fixed the menu state. Below are two focused, practical ways to keep the same page when switching sort order, plus a short note on keeping the same records rather than the same page.

  1. Preserve the current page (the simplest)
  • Keep the current page (and any other GET filters) in the option URL so the browser stays on that page after changing sort.
  • Build the option URLs from the current query string (safer than hard‑coding). Example pattern:
<?php
$params = $_GET;
$params['dateorder'] = 'asc';
$asc = $_SERVER['PHP_SELF'].'?'.http_build_query($params);
$params['dateorder'] = 'desc';
$desc = $_SERVER['PHP_SELF'].'?'.http_build_query($params);
?>
<select onchange="location = this.value;">
  <option value="<?=htmlspecialchars($asc)?>" <?=($_GET['dateorder']=='asc')?'selected':''?>>OLD => NEW</option>
  <option value="<?=htmlspecialchars($desc)?>" <?=($_GET['dateorder']=='desc')?'selected':''?>>NEW => OLD</option>
</select>
  1. Keep showing the same physical records after reversing order (optional)
  • If you want the same items to remain visible after flipping the sort, compute the new page from the total row count:
    $per = 10;                  // rows per page
    $old = max(0,(int)$_GET['page']);   // zero-based
    $start = $old * $per;
    $total = /* SELECT COUNT(*) FROM kiniseis */;
    $newPage = (int) floor((($total - 1) - $start) / $per);
  • Use $newPage when building the dateorder URLs.

Quick cautions

  • Never inject raw $_GET into SQL ORDER BY. Whitelist the direction: $dir = ($_GET['dateorder']==='asc') ? 'ASC' : 'DESC'.
  • Escape URLs with htmlspecialchars() and cast page counts to ints.
  • Test edge cases: very small result sets and last page boundaries.

This ties ’s selected-state fix with a robust URL-building approach and an optional mapping calculation if the goal is to preserve the exact records rather than just the page number.

Recommended Answers

All 6 Replies

<?php
  $query="select * from table order by date {$_REQUEST['qryorder']}";
.
.
.
.
.
.
?>

<form name=frm action='thispage.php'>
<select name=qryorder onchange='document.frm.submit()'>
<OPTION VALUE='asc'>Ascending
<OPTION VALUE='desc'>Descending
</SELECT>
</form>
.
.
.
.
.

or

<?php
  $query="select * from table order by date {$_REQUEST['qryorder']}";
.
.
.
.
.
?>

<a  href='thispage.php?qryorder=asc' >Ascending order</a>
<a  href='thispage.php?qryorder=desc' >Descending order</a>
.
.
.
..

It kinda worked but i have a problem...

It works only the first time the page loads and after the jump menu doesnt change its values.

This is my code , i changed it a bit to match my needs :

// mysql query
$query_kiniseis = "SELECT * FROM kiniseis ORDER BY `date` {$_REQUEST['dateorder']}";

// form java script 
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>

//form it self

<form name="form" id="form">
  <select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
    <option value ="date.php?dateorder=asc">ASC</option>
    <option value ="date.php?dateorder=desc">DESC</option>
    </select>
</form>

When i change the values from the browser ( dateorder=asc dateorder=desc ) the data are sorting as expected but the jump menu is not functional.
Im using dreamweaver.

Thanks in advance

I dont know what you are asking for but I guess that you want to set selection box to the value selected by user. you may achive it using following code

<option value ="date.php?dateorder=asc" 
<?php echo ($_REQUEST['dateorder']=='asc')?"selected":"";?> >ASC</option>    
<option value ="date.php?dateorder=desc" 
 <?php echo ($_REQUEST['dateorder']=='desc')?"selected":"";?> >DESC</option>

Thank you very much it worked :)

One last thing for this to be perfect ,

I would like when im changing order , not the data to be displayed from the begining but in the current page.

Say for example im in page 10th of the data with the desc ordering , i would like to change to asc , but the page to keep showing me the 10th page.

My exact code is this

<form method="get" name="form" id="form">
    <select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
      <option value="Hmerisies_Kiniseis.php?dateorder=asc"
          <?php echo ($_REQUEST['dateorder']=='asc')?"selected":"";?>
          >OLD => NEW</option>
      <option value="Hmerisies_Kiniseis.php?dateorder=desc"
          <?php echo ($_REQUEST['dateorder']=='desc')?"selected":"";?>
          >NEW => OLD</option>
      </select>
  </form>

I have a feeling i must use something like %s?page=%d%s but i dont know where

You need to learn about "limit" clause of mysql

in user form

.
.
.

<input type=hidden name=page value=<?php echo $_REQUEST['page'];?> >
</form>

in query

$recperpage=10;
if ($_REQEUST['page']=="")
   $_REQEUST['page']=0;
$startfrom=$_REQEUST['page']*$recperpage;

$query=" SELECT * FROM kiniseis 
 order by date {$_REQUEST['dateorder']} 
 limit {$startfrom},{$recperpage} ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.