This topic is locked

How To Rank Database Records

6/26/2007 11:20:26 AM
PHPRunner General questions
J
jebrown author

Is there a way to have a List page rank database records (1,2,3,etc) both on the initial listing and on subsequent sorts by field name?
If you link to http://www.ncmotorcyclists.com/View%20Mile...riders_list.php, you will see a list of motorcycle riders sorted by average miles per day. I would like to included another column that ranks the the riders from 1 to the end. If someone reorders the list by clicking on Miles Ridden, then the ranking would change accordingly.
I could not find anything in the forum when I did a search for "How To Rank Database Records".
Thanks!

Sergey Kornilov admin 6/26/2007

Here is how you can do this:

  1. add an additional field to the SQL query named Rank

    select [ID],

    0 as Rank,

    [Make],

    ...

    From [Cars]
  2. Proceed to the Visual Editor, make sure this field appears in the very first column and set "View as" type to Custom.

    Put the following code there:
    $_SESSION["Rank"] += 1;

    $value = $_SESSION["Rank"];
  3. Build ListOnLoad event with the following code:
    $_SESSION["Rank"]=0;
    This is it.

J
jebrown author 6/26/2007

That works for the first page but when you go to the next/other pages, it starts all over again at rank number 1. Is there a way to keep the sequence going on additional pages?