В шаблоне страницы:
Code
<div align="center">
<p>
<b>Данный пример демонстрирует использование скрипта для сортировки данных расходов за последние 30 дней</b>
</p>
</div>
<hr>
<?$PHPCODE$("http://phpexample.u coz.net/php/example002/example002.php")?>
PHP скрипт примера:
Code
<?
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>Дата</th><th>Сумма</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>Дата</th><th>Сумма</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>Дата</th><th>Сумма</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>Дата</th><th>Сумма</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('Ошибка в данных, обновите страницу')";
exit;
}
$gtime=time();
srand($gtime);
$data="<div id='sorttable'><table border='1' cellpadding='0' cellspacing='0' width='100%'><th>Дата</th><th>Сумма</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%'>Сортировать по дате [<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'>Сортировать по сумме [<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;
?>