This topic is locked

How to rank records on the list page

1/17/2011 11:08:50 PM
PHPRunner Tips and Tricks
admin

In this tutorial we'll show how to change the order of database records on the list page by clicking up or down arrow button in selected row.

  1. As a first step you need to add a numeric field named Rank to database table in questions. It makes sense to populate this field with some initial values. Besides that ranking makes he most sense when data is sorted by Rank field.

SELECT

....

Rank

FROM Cars

ORDER BY Rank


2. Proceed to the Visual Editor and set 'View as' type of Rank field to 'Custom'. Paste the following code there:



$keycolumn="ID";

global $strTableName;

$value = '<a class="tablelinks" href="'.$strTableName.'_list.php?a=return&custom=up&recid=' .$data["$keycolumn"] . '">

<img style="border: 0px;float: right; clear: right;margin: 2 2 2 8;" src="images/up1.png"/></a>

<a class="tablelinks" href="'.$strTableName.'_list.php?a=return&custom=down&recid=' .$data["$keycolumn"] . '">

<img style="border: 0px;float: right; clear: right;margin: 2 2 2 8;" src="images/down1.png"/></a>

<p>' . $value . '</p>';


This code displays the value of Rank field plus two hyperlinked images. Hyperlinks looks like this:

Cars_list.php?a=return&custom=up&recid=3

Cars_list.php?a=return&custom=down&recid=3
custom points either 'up' or 'down', recid contains the ID of record to be moved.
3. Proceed to Events editor and post the following code to BeforeSQLQuery event. That's where we see if parameters were passed via URL and change ranks of two records sitting next to each other.



global $strTableName;

$previd="";
if ($_REQUEST["custom"])

{

$keycolumn="ID";

$rankcolumn="Rank";

$rs = CustomQuery($strSQL);

$data = db_fetch_array($rs);

while($data)

{

if ($data[$keycolumn]==$_REQUEST["recid"])

{

if ($_REQUEST["custom"]=="up" && $previd!="")

{

CustomQuery("update ".AddTableWrappers($strTableName)." set $rankcolumn=".$data[$rankcolumn]." where $keycolumn=".$previd);

CustomQuery("update ".AddTableWrappers($strTableName)." set $rankcolumn=$prevrank where $keycolumn=".$data[$keycolumn]);

}

else if ($_REQUEST["custom"]=="down")

{

$previd=$data[$keycolumn];

$prevrank=$data[$rankcolumn];

$data = db_fetch_array($rs);

if ($data)

{

CustomQuery("update ".AddTableWrappers($strTableName)." set $rankcolumn=".$data[$rankcolumn]." where $keycolumn=".$previd);

CustomQuery("update ".AddTableWrappers($strTableName)." set $rankcolumn=$prevrank where $keycolumn=".$data[$keycolumn]);

}

}

break;

}
$previd=$data[$keycolumn];

$prevrank=$data[$rankcolumn];

$data = db_fetch_array($rs);

}
}


4. Download

and

images and save them in 'images' directory under output directory (right click on image and choose 'Save image as').
This is it. Just make sure you use correct rank and key column names in the beginning of each of above events.
Tags: move rows up and down, change records order, re-arrange rows

romaldus 1/22/2011

ERROR on your demo project when i clicked the top arrow of Rank 1 (take a look at screenshot)


[size="3"]
ERROR details :
[/size]

admin 1/23/2011

Thanks, uploaded correct version.

M
mrcaseyman 7/3/2016

Using this tip on a version 9 project I found that the update query gave the error of 'no such table exists' because it was using the name of the View, not the actual table.

By substituting the name of the actual table for the variable $strTableName I got the whole thing working.

Not sure if this is a version 9 thing as I have never used this tip before in any other versions.

lefty 7/7/2016



Using this tip on a version 9 project I found that the update query gave the error of 'no such table exists' because it was using the name of the View, not the actual table.

By substituting the name of the actual table for the variable $strTableName I got the whole thing working.

Not sure if this is a version 9 thing as I have never used this tip before in any other versions.



The above solution is great . If you just need basic Rank of one field without arrows and little coding you can use this as your query on a view of original table where T is a temporary table. You should create a view of original table .
SELECT

(SELECT

count(*) + 1

FROM copyoriginaltable

WHERE CDbl(pointsfieldbelow) > CDbl(T.pointsfieldbelow)

) AS Rank,

CDbl(T.numericfieldname),

points,

fldname,

ID

FROM copyoriginaltable AS T

ORDER BY CDbl(T.pointsfieldbelow) DESC
This will give you rank also . MSACCESS tried and works . Not sure with MYSQL.
Copy of Results
Rank / Points / Name / Date / ID

1 32.3333 Russ 2015-09-13

2 25.3333 Gra 2015-09-13

3 21 Jone 2015-09-13

4 20 Rescsan 2015-08-09

5 12.3333 Gall 2015-07-19

6 10 Peti 2015-09-06

7 9 Manc 2015-05-24

8 8 Barg 2015-08-23

9 5 Fiore 2015-06-14

10 1 Bonen 2015-06-14
Just realized this was used in ASP but I'm sure will still work with PHP . Just CDBL needs to be changed to number_format function. something like FORMAT(SUM(T.pointsfieldbelow),2) > FORMAT(SUM(T.pointsfieldbelow),2) AS Rank