رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نحوه استفاده از کلاس paging
#1
دوستان من میخوام از کلاس paging آقای موحد تو ام ویسی که تو این پکیج آموزش داده شده استفاده کنم
لطفا راهنمائی کنید

به عنوان مثال برای نمایش اخبار
این کلاس مدل اخبارمه :

<?php

class news extends model {
    private $fields = null;
    public function __construct() {
        $this->fields = array(
            'id'=>null,
            'cat_id'=>null,
            'owner_id'=>null,
            'title'=>null,
            'description'=>null,
            'visit'=>null,
            'like'=>null,
            'dislike'=>null,
            'picture'=>null,
            'create_date'=>null,
            'update_date'=>null,
            'expire_date'=>null,
            'status'=>null,
        );    
    }
    
    public function __Get($fieldName){
        if(isset($this->fields[$fieldName])){
            return $this->fields[$fieldName];
        }
    }
    
    public function __SET($fieldName, $value){
        if(array_key_exists($fieldName, $this->fields)){
            return $this->fields[$fieldName] = $value;
        }
    }
    
    public static function findByPk($id){
        $object = null;
        $resault = Dal::ArrayQuery("SELECT * FROM `news` WHERE(`id`={$id})");
        
        if(count($resault) > 0){
            $object = new news();
            foreach($resault[0] as $fieldName => $value){
                $object->fields[$fieldName] = $value;
            }        
        }
        return $object;
    }
    
  public static function findByLatest(){
        $array = array();
        $curentDate = date("Y/m/d");        
        $data = Dal::ArrayQuery("SELECT * FROM `news` WHERE (`status` = '1') ORDER BY `id` DESC LIMIT 1 ");
        if(count($data) > 0){            
            foreach($data as $resault){
                $object = new news();
                foreach($resault as $fieldName => $value){
                    $object->$fieldName = $value.PHP_EOL;
                }
                $array[] = $object; 
            }
        }
        return $array;
    }
    
    public static function findAll($catId=NULL){
        $array = array();
        $curentDate = date("Y/m/d");        
        $data = Dal::ArrayQuery("SELECT * FROM `news` WHERE (`status` = '1') ORDER BY `id` DESC ");
        if(count($data) > 0){            
            foreach($data as $resault){
                $object = new news();
                foreach($resault as $fieldName => $value){
                    $object->$fieldName = $value.PHP_EOL;
                }
                $array[] = $object; 
            }
        }
        return $array;
    }     
    public static function findByCategoryId($id){
        $array = array();
        $data = Dal::ArrayQuery("SELECT * FROM `news` WHERE(`cat_id` = {$id}) ORDER BY `id` DESC");
        if(count($data) > 0){
            foreach($data as $resault){
                $object = new news();
                foreach($resault as $fieldName => $value){
                    $object->$fieldName = $value;
                }
                $array[] = $object;
            }
            
        }
        return $array;
    }
}

?>

پاسخ
تشکر شده توسط:
#2
اسنم کلاس paging


<?php
/**
* @author Mohsen Movahed <l3iidak@yahoo.com>
* @copyright 2014 Mohsen Movahed
* @date 19 May 2014 - 1393/2/28
* @version 1.0
* @license GPL
* @link        http://www.bidakplus.ir
*/
class Pagination
{
private $items;
private $output;

/**
* construct method
*/
public function __construct($params = null)
{
$this->items = array(
'items_per_page' => 5,         // Records per page to display
'total_records' => 74,          // Total records in database
'total_pages' => 0,            // Total number of pages
'url_address' => '',           // For example: http://www.mysite.com/?page=
'concat_to_url' => '',         // This comes after page value - [optional]
'current_page' => 1,           // Number of current page
'page_num_per_section' => 5,   // Total number of pages to display - for example CurrentPage=16 :=> [first][next]...,14,15,[16],17,18,...[prev][last]  
'mode' => true,               // Mode = true or anything => Display::  1,...,14,15,[16],17,18,...,20
);
               
               Base::pre($this->items);

// set values
if (isset($params) && count($params) > 0)
{
if (is_array($params))
{
                           
foreach ($params as $key => $value)
{
if (!empty($value))
{
 $this->$key = $value;
}
}
}
}

// run paginate method
$this->paginate();
}

/**
* get values
* @param  string $key Must be index of items array
* @return mixed|boolean if there is $key, returns array value otherwise returns false. 
*/
public function __get($key)
{
if (isset($this->items[$key]))
{
return $this->items[$key];
}
return false;
}

/**
* set values
* @param string $key Index of items array
* @param mixed $value a value for set
*/
public function __set($key, $value)
{
if (isset($this->items[$key]))
{
$this->items[$key] = $value;
}
}

/**
* get total pages
* @return integer Return the total pages
*/
private function getTotalPages()
{
$this->items_per_page = ($this->items_per_page <= 0 ? 1 : $this->items_per_page);

$total = ceil($this->total_records / $this->items_per_page);
if ($total <= 0)
{
$total = abs($total) + 1;
}
return $total;
}

/**
* this manages to display pagination
*/
private function paginate()
{
$this->total_pages = $this->getTotalPages(); // set total pages
$check = $this->checkItems(); // check item values and page number 

if ($check)
{
// start of section
$start = $this->current_page - floor($this->page_num_per_section / 2);
// maximum start
$max = $this->total_pages - floor($this->page_num_per_section / 2);
if($start <= 0 || $start > $max)
{
if ($start > $max)
$this->current_page = 1;
$start = 1;
}

// end of section
$end = $start + $this->page_num_per_section - 1;
if($end > $this->total_pages)
{
$end = $this->total_pages;
}


$this->output .= '<ul class="paging">' . PHP_EOL;

// print first page button
if ($this->mode == false && $this->current_page != 1)
{
$this->output .= '<li><a href="'. $this->url_address . 1 . $this->concat_to_url .'">First</a></li>' . PHP_EOL;
}

// print next page button
if ($this->current_page < $this->total_pages && $this->mode == false)
{
$this->output .= '<li><a href="'. $this->url_address . $this->nextPage() . $this->concat_to_url .'">Next</a></li>' . PHP_EOL;
}

// print page number
for ($i = $start; $i <= $end ; $i++)
{
// print dots in right
if ($i == $start && $start > 1 && $i != 1)
{
// print page one
if ($this->mode)
{
$this->output .= '<li><a href="'. $this->url_address . 1 . $this->concat_to_url .'">1</a></li>' . PHP_EOL;
}
$this->output .= '<li class="dot-paginator">...</li>' . PHP_EOL;
}

// print pages number
$this->output .= '<li><a class="'. ($i == $this->current_page ? 'current-page' : '') .'" href="'. $this->url_address . $i .'">'. $i .'</a></li>' . PHP_EOL;

// print dots in left
if ($i <= $this->total_pages && $i == $end && $i != $this->total_pages)
{
$this->output .= '<li class="dot-paginator">...</li>' . PHP_EOL;
}
}

// print prev page button
if ($this->current_page > 1 && $this->mode == false)
{
$this->output .= '<li><a href="'. $this->url_address . $this->prevPage() . $this->concat_to_url .'">Previous</a></li>' . PHP_EOL;
}

// print last page number
if ($this->mode && $this->total_pages != $this->current_page && $this->total_pages != $end)
{ 
$this->output .= '<li><a href="'. $this->url_address . $this->total_pages . $this->concat_to_url .'">'. $this->total_pages .'</a></li>' . PHP_EOL;
}

// print last page button
if ($this->mode == false && $this->current_page != $this->total_pages)
{
$this->output .= '<li><a href="'. $this->url_address . $this->total_pages . $this->concat_to_url .'">Last</a></li>' . PHP_EOL;
}

$this->output .= '</ul><br>' . PHP_EOL;
}
}

/**
* check item values
* @return boolean The result true if item values is not empty, false otherwise
*/
private function checkItems()
{
foreach ($this->items as $key => $value)
{
if (empty($value))
{
settype($key, 'string');
switch ($key)
{
case 'current_page':
$this->$key = 1; // not required beacause by default equal to 1
break;
case 'concat_to_url':
break;
case 'mode':
break;
default:
return false;
break;
}
}
}

if ($this->total_pages == 1)
{
return false;
}

$this->checkPageNumber();
return true;
}

/**
* check page number
*/
private function checkPageNumber()
{
$this->current_page = intval($this->current_page);
if ($this->current_page > $this->total_pages)
{
$this->current_page = $this->total_pages;
}
elseif ($this->current_page <= 0)
{
$abs = abs($this->current_page);
$this->current_page = ($this->current_page < 0 ? $abs : $abs + 1);
}
}

/**
* previous page
* @return integer
*/
private function prevPage()
{
return $this->current_page - 1;
}

/**
* next page
* @return integer
*/
private function nextPage()
{
return $this->current_page + 1;
}

/**
* show part of the records => for example: 1 - 10 of 200
*/
public function recordsInfo()
{
$var = $this->current_page * $this->items_per_page;
$sectionEnd = $var;
$sectionStart = $sectionEnd - $this->items_per_page + 1;
$sectionEnd = ($var > $this->total_records ? $this->total_records : $var);

echo 'Showing ' . $sectionStart . ' to ' . $sectionEnd . ' of ' . $this->total_records . ' entries';
}

/**
* show current page of all pages
*/
public function pagesInfo()
{
echo 'Page ' . $this->current_page . ' of ' . $this->total_pages;
}

/**
* get query limit
* @return array Return start and end section for query limit
*/
public function limit()
{
$start = $this->items_per_page * ($this->current_page - 1);
$limit = $this->items_per_page;
return array('start' => $start, 'limit' => $limit);
}

/**
* show paging
*/
public function display()
{
echo $this->output;
}
}





پاسخ
تشکر شده توسط:
#3
دوستان کسی جواب نمیده؟
پاسخ
تشکر شده توسط:
#4
خدایی خودتون حال دارین اینهمه کد رو برای حل مشکل یکی دیگه بخونین؟ بهتره فقط همون بخشی از کدها که مربوط به مسئله است رو بگذارین.
پاسخ
تشکر شده توسط: sm_pakdel
#5
پست اولودر نظر نگیرین اگر زیاده یه راهنمائی کنید از این کلاس تو همون ام وی سی که یاد دادین چطور استفاده کنم
خلاصه تر از این نمیتونستم بگم
پاسخ
تشکر شده توسط:
#6
میتونید توی کلاس مدل که والد همه مدلهاست، متدهای موردنیاز برای صفحه بندی رو با کمک این کلاس بسازین تا توی تمام مدلها قابل استفاده بشه. فکر میکنم بهترین جا برای استفاده از این کلاس، مدلها باشن.
پاسخ
تشکر شده توسط:
#7
هر دو کلاس متد های __GET و __SET دارن چی کار باید کرد؟
پاسخ
تشکر شده توسط:
#8
این الان ربطش به موضوع چی بود دقیقاً ؟!
پاسخ
تشکر شده توسط: sm_pakdel
#9
اون کلاسو توی مدل اصلی گذاشتم و بقیه مدل هارو extend کردم
به همین خاطر ارور میده که هر دو کلاس این متدها رو دارن و متفاوت هم هستن
پاسخ
تشکر شده توسط:
#10
خوب میتونید از کلاس Pagination این متدها رو حذف کنید و بجای آرایه private به اسم items عناصر داخلش رو بصورت فیلدهای public در بیارین و کد کلاس رو هم اصلاح کنید. البته بنظرم بهتره برای اینکه تداخلی پیش نیاد، متدی به اسم Paginate و... به کلاس مدل اضافه کنید و یک شئ هم از کلاس Pagination به مدل اضافه کنید و توی متدهای مربوط به صفحه بندی توی مدل، متدهای متناظر از شئ کلاس Pagination رو صدا بزنید.
پاسخ
تشکر شده توسط: sm_pakdel




کاربران در حال بازدید این موضوع: 1 مهمان