5c0807b7be028f17826027377e5a9952

I want to setapart paging list from content.
Thanks alot

<?php
function cat_paging()
{
    $cid = get_url();
    $perPage = 6;
    $current_page = $_SERVER["PHP_SELF"];
    $page = is_numeric($_GET["page"]) ? $_GET["page"] : 1;
    //$start = ($page * $perPage) - $perPage;
    if ($page)
        $start = ($page - 1) * $perPage;
    else
        $start = 0;
    $sql = sprintf("SELECT id, title, abstract, content_image FROM content WHERE catID = %d ORDER BY id DESC LIMIT %d, %d",
        $cid, $start, $perPage);
    $news_array_paging = table_to_array($sql);
    foreach ($news_array_paging as $key => $value) {
        $txt = '<ul class="contentPaging">
            <li>
                <img src="images/' . $value['content_image'] .
            '" width="100" height="100">
                <h2>' . $value['title'] . '</h2>
                <a href="news.php?contID=' . $value['id'] . '">' . $value['abstract'] .
            ' <span>Read More...</span></a>
                 
            </li>
        </ul>';
        echo $txt;
    }


    $totalCount = sprintf("SELECT COUNT(*) as 'Total' FROM content WHERE catID = %d",$cid);
    $count = mysql_query($totalCount);
    $rowCount = mysql_fetch_array($count);
    $totalRow = $rowCount['Total'];
    $totalPage = ceil($totalRow / $perPage);
    $prev = $page - 1;
    $next = $page + 1;

    $paging = '<ul class="paging">';
    if ($page > 1) {
        $paging .= '<li><a href="' . $current_page . '?cID=' . $cid . '&page=' . $prev .
            '">Önceki</a></li>';
    }

    for ($i = 0; $i < $totalPage; $i++) {
        $j = $i + 1;
        $prev = $j - 1;


        if ($_GET['page'] == $j) {
            $class = "pCurrent";
        } else {
            $class = "";
        }
        $paging .= '<li><a href="' . $current_page . '?cID=' . $cid . '&page=' . $j .
            '" class="' . $class . '">' . $j . '</a></li>';
    }
    if ($page < $totalPage) {
        $paging .= '<li><a href="' . $current_page . '?cID=' . $cid . '&page=' . $next .
            '">Sonraki</a></li>';
    }
    $paging .= '</ul>';

    return $paging;

}
?>

Refactorings

No refactoring yet !

94f928c5cde29a190e2062fc7bb7fbdb

Kathy Wu

June 6, 2010, June 06, 2010 22:01, permalink

No rating. Login to rate!

How large is the content table. Have you considered using jquery?
http://www.geckonewmedia.com/blog/2009/8/20/simplepager---jquery-paging-plugin--updated

94f928c5cde29a190e2062fc7bb7fbdb

kathy-wu.myopenid.com

June 7, 2010, June 07, 2010 01:17, permalink

No rating. Login to rate!
<html>
	<header></header>
	<body>
		<ul class="contentPaging">
			<?php foreach($contentList as $content): ?>
			<li>
				<img src="images/<?=$content->image; ?>" width="100" height="100">
	            <h2><?=$content->title; ?></h2>
	            <?=$content->brief; ?> &nbsp;
	            <a href="news.php?contID=<?=$content->id; ?>">
	            	<span>Read More...</span>
	            </a>
			</li>
			<?php endforeach; ?>
		</ul>
		<?php if($hasPreviousPage): ?>
			<a href="index.php?catid=<?=$catid; ?>&page=<?=$page-1;?>">
				Previous
			</a>
		<?php endif; ?>&nbsp;

		<?php if($hasNextPage): ?>
			<a href="index.php?catid=<?=$catid; ?>&page=<?=$page+1;?>">
				Next
			</a>
		<?php endif; ?>&nbsp;
	</body>
</html>
<?php
class Content extends Controller {
	public $catid;
	public $limit = 6;
	public $page;

	function Content()
	{
		parent::Controller();
	}

	function index()
	{
		$this->catid = isset($_GET['catid']) ? $_GET['catid'] : 0;
		$this->page = isset($_GET['page']) ? $_GET['page'] : 0;

		$numberOfRecords = $this->GetNumberOfRecords();

		$data['contentList'] = $this->GetContents();
		$data['numberOfRecords'] = $numberOfRecords;
		$data['hasPreviousPage'] = $this->HasPreviousPage($numberOfRecords);
		$data['hasNextPage'] = $this->HasNextPage($numberOfRecords);
		$data['catid'] = $this->catid;
		$data['page'] = $this->page;
		$this->load->view('content_view', $data);
	}

	function GetOffset()
	{
		return $this->limit * $this->page;
	}

	function GetContents()
	{
		$CI =& get_instance();
		$this->load->model('ContentRecord');
		$contentList = $CI->ContentRecord->GetContents(
							$this->catid
							, $this->limit
							, $this->GetOffset());

		return $contentList;
	}

	function GetNumberOfRecords()
	{
		$CI =& get_instance();
		$this->load->model('ContentRecord');
		return $CI->ContentRecord->GetNumberOfRecords($this->catid);
	}

	function HasPreviousPage($numberOfRecords)
	{
		return ($this->GetOffset() > 0 && $numberOfRecords > 0);
	}

	function HasNextPage($numberOfRecords)
	{
		return ($this->GetOffset() + $this->limit) <= $numberOfRecords;
	}
}
?>
<?php
 class ContentRecord extends Model
 {
	private $DB_ID = 'id';
	private $DB_CATID = 'catID';
	private $DB_TITLE = 'title';
	private $DB_ABSTRACT = 'abstract';
	private $DB_IMAGE = 'content_image';

	public $id;
	public $catid;
	public $title;
	public $brief;
	public $image;

 	function ContentRecord()
 	{
 		parent::Model();
 	}

	function GetContents($catid, $limit, $offset)
	{
 		$this->db->where($this->DB_CATID, $catid);
 		$this->db->order_by($this->DB_ID, "asc");
 		$query = $this->db->get('content', $limit, $offset);

		$contentList = array();
		foreach($query->result_array() as $row)
 		{
 			$content = new ContentRecord();

 			$content->id = $row[$this->DB_ID];
 			$content->catid = $row[$this->DB_CATID];
 			$content->title = $row[$this->DB_TITLE];
 			$content->brief = $row[$this->DB_ABSTRACT];
 			$content->image = $row[$this->DB_IMAGE];

 			array_push($contentList, $content);
 		}

		$query->free_result();

		return $contentList;
 	}

	function GetNumberOfRecords($catid)
	{
		$this->db->where($this->DB_CATID, $catid);
		$query = $this->db->get('content');

		return $query->num_rows();
	}
 }
?>

Your refactoring





Format Copy from initial code

or Cancel