<?php if( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Com_model extends Model
{
	var $uri_segnum = 2;
	function Com_model()
	{
		parent::Model();
	}
	
	function get_nav($arr)
	{
		$defarr = array('nav_str'=>'','nav_title'=>'');
		foreach($arr as $k=>$v)
		{
			$$k = $v;
		}
		unset($arr);
		if( ! isset($catalog_id) || !isset($nav_title) || ! isset($base) || ! isset($tb)) return $defarr;
		$str='';
		$base = rtrim($base,'/').'/';
		if($catalog_id > 0)
		{
			$query = $this->common_model->get_record('SELECT queue FROM '.$this->db->dbprefix.$tb.' WHERE cid = ?',array($catalog_id));
			if( ! $query) return $defarr;
			$queue = $query->queue;
			$queue = trim($queue,',');
			
			$query = $this->common_model->get_records('SELECT cat_name,cid FROM '.$this->db->dbprefix.$tb.' WHERE id in('.$queue.')');
			$i = 1;
			foreach($query as $row)
			{
				if($is_list)
				{
					if(count($query) == $i)
					{
						$str.=' &gt; '.$row->cat_name;
						$nav_title = $row->cat_name;
					}
					else
					{
						$str.=' &gt; <a href="'.site_url($base).'?cid='.$row->cid.'">'.$row->cat_name.'</a>';
					}
				}
				else
				{
					$str.=' &gt; <a href="'.site_url($base).'?cid='.$row->cid.'">'.$row->cat_name.'</a>';
				}
				$i += 1;
			}
			if( ! $is_list) $str.=' &gt; '.$nav_title;
			return array('nav_str'=>$str,'nav_title'=>$nav_title);
		}
		else
		{
			return array('nav_str'=>' &gt; '.$nav_title,'nav_title'=>$nav_title);
		}
	}
	
	function get_page_nav($t,$c,$p)
	{
		$str = '';
		if( ! $c) $c = 1;
		if($t > 0 && $p > 0)
		{
			$page_total = ceil($t / $p) ;
			if($c > $page_total) $c = $page_total;
			if($page_total > 1)
			{
				$r_uri = '';
				if(isset($_SERVER["REQUEST_URI"]) && ! empty($_SERVER["REQUEST_URI"]))
				{
					$r_uri = $_SERVER["REQUEST_URI"];
				}
				else if(isset($_SERVER['PATH_INFO']) && ! empty($_SERVER["PATH_INFO"]))
				{
					$r_uri = $_SERVER['SCRIPT_NAME'].$_SERVER["PATH_INFO"];
					if($_SERVER['QUERY_STRING']) $r_uri .= '?' . $_SERVER['QUERY_STRING'];
				}
				$r_uri='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$r_uri;
				$r_uri = preg_replace('/&page_no\=(\d+)?/is','',$r_uri);
				if(strpos($r_uri,'?') === FALSE) $r_uri .= '?'; 
				else $r_uri .= '&';
				if(($s = $c - 4) < 0) $s = 1;
				if(($e = $c + 4) > $page_total) $e = $page_total;
				if($c > 1) $str .= '<a href="'.$r_uri.'page_no=1">&lsaquo;首页 </a> ';
				if($c > 1) $str .= '<a href="'.$r_uri.'page_no='.($c-1).'">上一页</a> ';
				for($i = $s; $i <= $e ; $i++)
				{
					if($i == $c) $str .= '<a href="'.$r_uri.'page_no='.$i.'" class="active" style="color:#ffffff">'.$i.'</a> ';
					else $str .= '<a href="'.$r_uri.'page_no='.$i.'">'.$i.'</a> ';
				}
				if($c < $page_total) $str .= '<a href="'.$r_uri.'page_no='.($c+1).'">下一页</a> ';
				if($c < $page_total) $str .= '<a href="'.$r_uri.'page_no='.($page_total).'">尾页 &rsaquo;</a>';
			}
		}
		return $str;
	}
}
?>