<?php
/**
* Sorting Head Builder For Dynamic Table
*
* @author Num
* @link http://web-programming-bookmark.blogspot.com
*/
class SortHead
{
protected $ccol = '';
protected $cdir = '';
protected $ncol = '';
protected $ndir = '';
protected $dfcol = '';
protected $dfdir = '';
protected $cols = array();
public $col = '';
public $dir = '';
public function __construct(array $cols,$cname,$dname,$dfcol,$dfdir){
$this->cols = $cols;
$this->dfcol = $dfcol;
$this->dfdir = $dfdir;
$this->ncol = $cname;
$this->ndir = $dname;
$this->ccol = $this->current_col();
$this->cdir = $this->current_dir($this->ccol,$dfdir);
$this->col = $this->ccol;
$this->dir = $this->cdir;
}
public function current_col(){
$ccol = empty($_GET[$this->ncol]) ? $this->dfcol : $_GET[$this->ncol];
$k = array_search($ccol,$this->cols,true);
return ($k === false) ?$this->dfcol :$this->cols[$k];
}
public function current_dir($col,$dfdir='desc'){
$odir = $dfdir == 'desc' ?'asc' :'desc';
if ($this->ccol == $col){
$dir = empty($_GET[$this->ndir]) ?$dfdir :$_GET[$this->ndir];
if ($dir != 'asc' && $dir != 'desc') $dir = 'desc';
$dir = $dir == 'desc' ?'desc' :'asc';
}else{
$dir = $dfdir;
}
return $dir;
}
public function next_dir($col,$dfdir='desc'){
$odir = $dfdir == 'desc' ?'asc' :'desc';
if ($this->ccol == $col){
$dir = empty($_GET[$this->ndir]) ?$dfdir :$_GET[$this->ndir];
if ($dir != 'asc' && $dir != 'desc') $dir = 'desc';
$dir = $dir == 'desc' ?'asc' :'desc';
}else{
$dir = $dfdir;
}
return $dir;
}
public function link($col,$dfdir='desc'){
$dir = $this->next_dir($col,$dfdir);
return '?'.$this->ncol.'='.$col.'&'.$this->ndir.'='.$dir;
}
public function dir($col,$dfdir='desc'){
return $this->current_dir($col,$dfdir);
}
}
$sh = new SortHead(array('name','title','id'),'st','sd','id','desc');
//safe for query
echo ', field=',$sh->col;
echo ', dir=',$sh->dir;
echo ', select * from tb order by '.$sh->col.' '.$sh->dir;
?>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>
<a href="<?php echo $sh->link('name','asc') ?>">name <?php echo $sh->dir('name','asc') ?></a>
</td>
<td>
<a href="<?php echo $sh->link('title') ?>">title <?php echo $sh->dir('title') ?></a>
</td>
<td>
<a href="<?php echo $sh->link('id') ?>">id <?php echo $sh->dir('id') ?></a>
</td>
</tr>
</table>
วันพฤหัสบดีที่ 5 พฤษภาคม พ.ศ. 2554
php sorting head builder class by num
ป้ายกำกับ:
oop,
php,
php function
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น