It is shown in this example how to sort random data with the help of php-scripts. Script creates a table with the generated random meanings of the time/date for the past 30 days and the sum on the page It imitates the expenses for the indicated period at the exact time. Any data of the two fields can be sorted by increase and decrease values on the page by pressing certain buttons. This script can be also used for sorting data, entered by the user. But it has to be slightly changed. It can be done by means of the form. The source code of this example is shown below.
In the template of the page:<div align="center"> <p> <b>This example shows the way of using the script to sort the data on expenses for the past 30 days</b> </p> </div> <hr> < ?$PHPCODE$("http://phpexample.ucoz.com/php/example002/example002.php")?>
PHP-script of this example:
< ? if ((count($_POST)>0) && $_POST['bdata']) { $___notjson=1; $arr=split("\|",$_POST['bdata']); if ($_POST['sorttype']==11) { foreach ($arr as $line) { trim($line); list($value,$key) = split(",",$line,2); $sortarr[$key]=$value; $sorttoorigarr[$value]=$key; } sort($sortarr,SORT_NUMERIC);
$data="<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><th>Date</th><th>Sum</th>";
foreach ($sortarr as $val) { if ($val) $data.="<tr><td align=\"center\" width=\"50%\">".date("Y-m-d H:m:s",$val)."</td><td align=\"center\">".$sorttoorigarr[$val].".00 USD</td></tr>"; } $data.="</table>"; echo "$('#sorttable').html('$data')"; } elseif ($_POST['sorttype']==12) { foreach ($arr as $line) { trim($line); list($value,$key) = split(",",$line,2); $sortarr[$key]=$value; $sorttoorigarr[$value]=$key; } rsort($sortarr,SORT_NUMERIC);
$data="<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><th>Date</th><th>Sum</th>";
foreach ($sortarr as $val) { if ($val) $data.="<tr><td align=\"center\" width=\"50%\">".date("Y-m-d H:m:s",$val)."</td><td align=\"center\">".$sorttoorigarr[$val].".00 USD</td></tr>"; } $data.="</table>"; echo "$('#sorttable').html('$data')"; } elseif ($_POST['sorttype']==21) { foreach ($arr as $line) { trim($line); list($key,$value) = split(",",$line,2); $sortarr[$key]=$value; $sorttoorigarr[$value]=$key; } sort($sortarr,SORT_NUMERIC);
$data="<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><th>Date</th><th>Sum</th>";
foreach ($sortarr as $val) { if ($val) $data.="<tr><td align=\"center\" width=\"50%\">".date("Y-m-d H:m:s",$sorttoorigarr[$val])."</td><td align=\"center\">".$val.".00 USD</td></tr>"; } $data.="</table>"; echo "$('#sorttable').html('$data')"; } elseif ($_POST['sorttype']==22) { foreach ($arr as $line) { trim($line); list($key,$value) = split(",",$line,2); $sortarr[$key]=$value; $sorttoorigarr[$value]=$key; } rsort($sortarr,SORT_NUMERIC);
$data="<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><th>Date</th><th>Sum</th>";
foreach ($sortarr as $val) { if ($val) $data.="<tr><td align=\"center\" width=\"50%\">".date("Y-m-d H:m:s",$sorttoorigarr[$val])."</td><td align=\"center\">".$val.".00 USD</td></tr>"; } $data.="</table>"; echo "$('#sorttable').html('$data')"; }
else echo "$('#sorttable').html('Data Error, reload the page')"; exit; } $gtime=time(); srand($gtime);
$data="<div id='sorttable'><table border='1' cellpadding='0' cellspacing='0' width='100%'><th>Date</th><th>Sum</th>\n"; for ($i=0; $i<20; $i++) { $tdate=rand($gtime-86400*30,$gtime); $summ=rand(1,5000); date("Y-m-d H:m:s",$tdate); $data.="<tr><td align='center' width='50%'>".date("Y-m-d H:m:s",$tdate)."</td><td align='center'>$summ.00 USD</td></tr>\n"; $hdata.="$tdate,$summ|"; } $data.="</table></div> <form name='sortform' action='/php/example002/example002.php' method='post' onsubmit=\"_uPostForm(this,{dataType:'script'});return false;\" onerror=\"alert('Error');\"> <table border='0' cellpadding='0' cellspacing='0' width='100%'>
<tr><td align='center' width='50%'>Sort by date [<input type='submit' value='A->Z' onclick='document.sortform.sorttype.value=11'> <input type='submit' value='Z->A' onclick='document.sortform.sorttype.value=12'>]</td>
<td align='center'>Sort by sum [<input type='submit' value='A->Z' onclick='document.sortform.sorttype.value=21'> <input type='submit' value='Z->A' onclick='document.sortform.sorttype.value=22'>]</td></tr></table>\n <input type='hidden' name='bdata' value='$hdata'> <input type='hidden' name='sorttype' value=0> </form>"; echo $data; ?>