hbhero 发表于 2011-4-20 14:30:56

此分页类如何倒叙输出


<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pager {
public $length = 5;
public $prevLabel = '';
public $nextLabel = '';
public $first = 0; //第一页
public $slider = 3;
public $perpage = 3;
public $query;
public $table;
public $count;
public $obj;
public $current;


function init($table,$perpage,$segment=3){

$this->table = $table;
$this->obj =& get_instance();
$this->obj->load->database();
$this->current = $this->obj->uri->segment($segment);
$this->perpage = $perpage;   
return $this;

}


//使用AR

function ar(){

$this->count = $this->obj->db->count_all($this->table);
$this->query = $this->obj->db->get($this->table, $this->perpage,$this->perpage*$this->current);

return $this;

}



//使用 自己写的SQL

function sql($query,$count){
$this->count = $count;   
$this->query = $query;
return $this;

}


function page($url,$current){
$perpage = $this->perpage;
$last = ceil($this->count/$perpage);
$prev = $current - 1; //上一页
$next = $current + 1;    //下一页
$output = "<ul class='pagenav'>";
    if ($current == $this->first) {

      $output .= "<li class=\"disabled\">首页 ".$this->prevLabel."</li>";

    } else {
          $output .= "<li><a href='".site_url($url.'/')."'>首页".$this->prevLabel."</a></li>";   
    }

$mid = intval($this->length / 2);

    if ($current < $this->first) {

      $current = $this->first;

    }

    if ($current > $last) {

      $current = $last;

    }
    $begin = $current - $mid;

    if ($begin < $this->first) { $begin = $this->first; }

    $end = $begin + $this->length - 1;


    if ($end >= $last) {

      $end = $last-1;

      $begin = $end - $this->length + 1;

      if ($begin < $this->first) { $begin = $this->first; }

    }

    if ($begin > $this->first) {

      for ($i = $this->first; $i < $this->first + $this->slider && $i < $begin; $i++) {

            $page= $i;

            $in = $i + 1;
            $urls=site_url($url.'/'.$page);

            $output .= "<li><a href=\"{$urls}\">{$in}</a></li>";

      }
      if ($i < $begin) {

            $output .= "<li class=\"none\">...</li>";

      }

    }   

    for ($i = $begin; $i <= $end ; $i++) {

      $page = $i;

      $in = $i + 1;

      if ($i == $current) {

            $output .= "<li class=\"current\">{$in}</li>";

      } else {

            $urls=site_url($url.'/'.$page);
            $output .= "<li><a href=\"{$urls}\">{$in}</a></li>";

      }

    }

    if ($last - $end > $this->slider) {

      $output .= "<li class=\"none\">...</li>";
      $end = $last - $this->slider;

    }


    for ($i = $end + 1; $i < $last; $i++) {

      $page = $i;
      $in = $i + 1;
                $urls=site_url($url.'/'.$page);
      $output .= "<li><a href=\"{$urls}\">{$in}</a></li>";

    }

    if ($current == $last-1) {

      $output .= "<li class=\"disabled\">".$this->nextLabel." 末页</li>";

    } else {
      $page = $last-1;
                $urls=site_url($url.'/'.$page);
      $output .= "<li><a href=\"{$urls}\">".$this->nextLabel." 末页</a></li>";
    }
    $output .= "</ul>";

echo $output;
}

}


view

<div class="news">

       
<?php foreach($query->result() as $row){?>
<div class="student">
<div id="stu">
<div id="stu_img"><img src="<?php echo $row->pic;?>" height="100" width="100" alt="<?php echo $row->title;?>"/></div>
                <span style="font-size:14px; font-weight:bold"><?=anchor('shufa/student_view/'.$row->id,$row->title)?></span><br /><br />
      情景介绍:<?php echo $row->descr;?>
</div>
</div>
<?php }?>
       

<div style="width:460px; text-align:center; border:0px solid; float:right; padding-top:15px">

<?php $this->pager->page('shufa/student',$current);?>
</div>
</div>

hbhero 发表于 2011-4-20 15:35:44

其实我是想知道$this->db->get(table)这个方法如何如何能够加进去order by id desc

lijun14992560 发表于 2011-4-20 18:00:39

$this->db->order_by("id", "desc");
$this->db->get(table);
页: [1]
查看完整版本: 此分页类如何倒叙输出