<?php

/*mysql_connect('127.0.0.1','root','root') or die('连接数据库:'.mysql_error());
mysql_select_db('webzhuoshang');
mysql_query('SET NAMES gb2312');*/

class dataclass{

var $s_baseUrl = "hostname_db"; //数据库的连接地址
var $s_baseUser = "username_db"; //数据库用户名
var $s_userPass = "password_db"; //数据库密码
var $s_baseName = "name_data"; //数据库名
  private function connectbase(){
      $connec=mysql_connect($this->s_baseUrl,$this -> s_baseUser,$this -> s_userPass) or die ("连接数据库出错:".mysql_error());
	mysql_select_db($this->s_baseName);
	mysql_query ("SET NAMES gb2312");
     }

  function __construct(){
  	$this->connectbase();
  }

	  
	  function data_select($table="",$where="",$orderby="",$limit="",$p="")
	 {
		if ($table!="")
		{
		 $sql="select * from ".$table;
		}  
		if ($where!="")
		{
		$sql.=" where ".$where;
		} 
		if($orderby!="")
		{
		$sql.=" order by ".$orderby;
		}
		if($limit!="")
		{
		$sql.=" limit 0,".$limit;
		}
		if(!empty($p))
		{
			$selBase = mysql_query($this->genpage($sql,$p));
		}
		else
		{
			$selBase = mysql_query($sql);
		}
		
		return $selBase;
	 }
	
	
	
	
		function data_work($sql)
	{
		mysql_query ("SET NAMES gb2312");  
		$resultselBase = mysql_query($sql) or die ("数据库语句无法执行$s_selSQL:".mysql_error()); 
		return $resultselBase;
	}
	 
	 
	 
	//smarty输出数组数据方法

   public  function get_rows($fields,$table,$where="",$order="",$limit="",$p=""){
		$items = array();
		$rows = $this -> data_select($table,$where,$order,$limit,$p);
		while($row = mysql_fetch_array($rows)) {
			$item = array();
			foreach ($fields as $field) $item[$field] = $row[$field];
			array_push($items, $item);
		}
		return $items;
	}
	
	
	
	/** PHPDoc注释
  *  
  * page:Smarty分页方法，此方法有两个参数。
  *
  * @param $key   string  section标记的loop数组变量名
  * @param $arraye  string	section标记的loop数组变量值
  */

	public function page($key,$arraye,$size,$php,$temp,$news_class)
	{
         require_once('smarty/Smarty.class.php');



$smarty = new Smarty(); 
$smarty->template_dir = "./templates/style1";
$smarty->compile_dir = "./templates_c";
$smarty->left_delimiter = "<{"; 
$smarty->right_delimiter = "}>";

	$smarty->assign($key,$arraye);//把数组指向模板
	// get_template_vars返回一个指定的已赋值的模板变量的值
	$smartyArr = $smarty->get_template_vars($key);
	//print_R($smartyArr);
	//记录总数，每页显示记录条数
	$pager_Total = count($smartyArr);
	//$pager_Total = count($news);//总记录数
	$pager_Size = $size;//设定每页显示的记录数
	$pager_Number = ceil($pager_Total/$pager_Size);//得到总页数，如果有小数就进一步取整
	$pager_URL = $php.".php?action=View&news_class=".$news_class;
	//获得当前页的页数，从REQUEST获得
	if(isset($_GET['pager_PageID'])&& !empty($_GET['pager_PageID']))
	{
		$pager_PageID = intval($_GET['pager_PageID']);
	}
	else
	{
		//如果是第一次访问，则设定当前页为第一页
		$pager_PageID = 1;
	} 
	//每页的起，始记录数
	//如果当前页是第一页，则记录是从第0条记录开始的
	if ($pager_PageID == 1 ) 
	{
		$pager_StartNum = 0;
		//如果当前页不是第一页，则记录是从当前的页数减去1乘以每页的显示记录数开始的 
	} 
	else 
	{
		$pager_StartNum = ($pager_PageID -1) * $pager_Size;
	} 
	//当前页的结束记录数是起始记录数加上每页的显示记录数
	$pager_EndNum = $pager_StartNum + $pager_Size;
	//echo "分页结尾记录  pager_EndNum:".$pager_EndNum."<br />";
	//如果当前页是第一页，且总页数大于1
	 if ($pager_PageID == 1 && $pager_Number>1) 
	 {
				//第一页
		 $pager_Links = "上一页 | <a href=".$pager_URL."&pager_PageID=".($pager_PageID+1).">下一页</a>";       
	  } 
	  elseif($pager_PageID == $pager_Number && $pager_Number>1) 
	  {
				//最后一页
		 $pager_Links = "<a href=".$pager_URL."&pager_PageID=".($pager_PageID-1).">上一页</a> | 下一页";
	  } 
	  elseif ($pager_PageID > 1 && $pager_PageID <= $pager_Number) 
	  {
				//中间
		 $pager_Links = "<a href=".$pager_URL."&pager_PageID=".($pager_PageID-1).">上一页</a> | <a href=".$pager_URL."&pager_PageID=".($pager_PageID+1).">下一页</a>";
	  } 
	  else 
	  {
		 $pager_Links = "上一页 | 下一页";
	  }	
	$smarty->assign('pager_Total',$pager_Total);   //总的记录数
	$smarty->assign('pager_Number',$pager_Number);  //分页数
	$smarty->assign('pager_PageID',$pager_PageID);   //当前分页数
	$smarty->assign('pager_StartNum',$pager_StartNum); //每页记录起始数
	$smarty->assign('pager_EndNum',$pager_EndNum);  //每页记录结束数
	$smarty->assign('pager_Links',$pager_Links);   //分页标签
	$smarty->display($temp.".tpl"); 
	
	
	}





	function genpage($sql,$num)
	   {
		  global $prepage,$nextpage,$pages,$sums,$page;  //out param
		  $page_size=$num;
		  $page = $_GET["page"];
		  $eachpage = $page_size;
		  $pagesql = strstr($sql," from ");
		  $pagesql = "select count(*) as ids ".$pagesql;
		  $conn = mysql_query($pagesql) or die(mysql_error());
		  if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
		  $pages = ceil(($sums-0.5)/$eachpage)-1;
		  $pages = $pages>=0?$pages:0;

		  $prepage = ($page>0)?$page-1:0;
		  $nextpage = ($page<$pages)?$page+1:$pages;  
		  $startpos = $page*$eachpage;
		  $sql .=" limit $startpos,$eachpage ";
		  return $sql;
		//echo $sql;
		 }



 
 
 
 
 function ck_login($username,$password,$chk_code){

if(!empty($username) && !empty($password)  && !empty($chk_code)){
     $checkcode=$_SESSION['checkcode'];
  if($checkcode!=$chk_code){	  
  		
	  echo("<script>alert('验证码错误！！');location.href='login.html'</script>");	
	  exit;
	  //break;
	  }	 
	  
	$sql="select * from admin where username='$username' and  password='$password'";
	$result=mysql_query($sql);
	$rs=mysql_fetch_array($result);
	if($rs){
 	    session_register('username');
		$_SESSION["username"]=$rs["username"];
		echo "<script>location.href='index.php'</script>";
			mysql_close();	
			exit;
	}else{
		
		 echo "<script>alert('登录失败，用户名和密码填写有误！！');location.href='login.html'</script>";
		 mysql_close();	
		
	}
	
		
}




}
 
 
 
	 //显示分页
	function showpage()
	{
		global $page,$pages,$prepage,$nextpage,$queryStrin,$shownum; //param from genpage function
		$shownum =10/2;
		$startpage = ($page>=$shownum)?$page-$shownum:0;
		//echo $startpage."<br />";
		$endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
		//echo $endpage."<br />";
		echo "共".($pages+1)."页:&nbsp;"; 
		if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
		if($startpage>0)
			echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>&laquo;</a></b>";
		for($i=$startpage;$i<=$endpage;$i++)
		{
			if($i==$page)    echo "&nbsp;<b>[".($i+1)."]</b>&nbsp;";
			else        echo "&nbsp;<a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a>&nbsp;";
		}
		if($endpage<$pages)
			echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>&raquo;</a></b> ... ";
		if($page<$pages)
			echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";
	
	 }





};

?>