<?
 if($open_db==1){
   //error_reporting(E_ALL); # 顯示所有的錯誤訊息
   //include('adodb-errorhandler.inc.php');
   include("adodb.inc.php");
   $db = NewADOConnection('mysql');
   //$db->debug =true;
   $db->Connect("localhost","cola","cola2007","cola","META");
   $db->Execute('set names utf8');
  // $rs=$db->Execute('select * from productsz'); #不正確的資料表 productsz');
  // if ($rs) $rs2html($rs);
   //print $db->ErrorMsg();
   //print_r($db);
 }
 
Class TOOLS
{
  	// 用Java Script顯示訊息視窗，訊息為 $msg
	function msg_box($msg)
	{
		if ($msg != ""){
			echo '<script language="javascript">alert("'.$msg.'")</script>';
		}
		return "";
	}
	//上一頁跟下一頁
	function goto($go)
	{     if($go==1 || $go == -1)
			echo '<script language="javascript">history.go('.$go.')</script>';
		   else
		   echo '<script language="javascript">window.parent.location=("'.$go.'")</script>';		
	}
// 轉址Re-Direction URL address至 $url
	Function go_url($url){
		echo "<meta http-equiv='refresh' content='0; url=http:$url'>";
	}
	
	Function go_form($url,$adto,$id){
	    
		echo "<form name=\"form99\" method=\"post\" action=\"$url\">
		<input name=\"adto\" type=\"hidden\" value=\"$adto\">
		<input name=\"id\" type=\"hidden\" value=\"$id\"></form>";
		echo "<SCRIPT LANGUAGE=\"JavaScript\">document.form99.submit();</SCRIPT>";
		
	}


	function showchecked($val){
		if ($val==1) return "checked";
		else return "";
	}

	function closeWin(){
		echo '
		<SCRIPT LANGUAGE="JavaScript">
		<!--
		window.close();
		//-->
		</SCRIPT>';
	}

	function funTime($varTime,$addHowMuch){//return strTime
		$addHowMuch = intval($addHowMuch);
		$tmpH=substr($varTime,0,2);
		$tmpMM=intval(substr($varTime,2,2));
		//echo "addHowMuch=$addHowMuch<br>";
		//echo "tmpH=$tmpH<br>";
		//echo "tmpMM=$tmpMM<br>";
		return date("Hi",strtotime($tmpH.":".($tmpMM+$addHowMuch)));
	}

	

/*  Convert image size. true color*/ 
    //$src        來源檔案 
    //$dest        目的檔案 
    //$maxWidth    縮圖寬度 
    //$maxHeight    縮圖高度 
    //$quality    JPEG品質 

    function ImageCopyResizedTrue($src,$dest,$maxWidth,$maxHeight,$quality) { 
        //檢查檔案是否存在 
        if (file_exists($src)  && isset($dest)) { 
            $destInfo  = pathInfo($dest); 
            $srcSize   = getImageSize($src); //圖檔大小 
            $srcRatio  = $srcSize[0]/$srcSize[1]; // 計算寬/高 
           $destRatio = $maxWidth/$maxHeight;
			if($srcSize[1] <= $maxHeight && $srcSize[0] <= $maxWidth){
			    $destSize[0] = $srcSize[0];
			    $destSize[1] = $srcSize[1];
			}elseif($srcSize[1] > $maxHeight && $srcSize[0] <= $maxWidth){
			    $destSize[1] = $maxHeight;
			    $destSize[0] = $srcSize[0]/($srcSize[1]/$maxHeight);
			}elseif($srcSize[1] <= $maxHeight && $srcSize[0] > $maxWidth){
			     $destSize[0] = $maxWidth;
				 $destSize[1] = $srcSize[1]/($srcSize[0]/$maxWidth);
			}else{
			   if($destRatio > $srcRatio) { 
                 $destSize[1] = $maxHeight; 
                 $destSize[0] = $srcSize[0]/($srcSize[1]/$maxHeight); 
               }else{ 
                 $destSize[0] =  $maxWidth; 
                 $destSize[1] = $srcSize[1]/($srcSize[0]/$maxWidth); 
               }
			}
            
			//GIF 檔不支援輸出，因此將GIF轉成JPEG 
           //if ($destInfo['extension'] == "gif") $dest = substr_replace($dest, 'jpg', -3); 
           
		    //建立一個 True Color 的影像
	        $version = gd_info();
	        if(substr($version["GD Version"],0,10)=="bundled (2")
               $destImage = imageCreateTrueColor($destSize[0],$destSize[1]); 
			else 
			   $destImage = imageCreate($destSize[0],$destSize[1]);
            //根據副檔名讀取圖檔 
            switch ($srcSize[2]) { 
                case 1: $srcImage = imageCreateFromGif($src);break; 
                case 2: $srcImage = imageCreateFromJpeg($src);break; 
                case 3: $srcImage = imageCreateFromPng($src);break; 
                default: return false; break; 
            } 
            //取樣縮圖 
            ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]); 
          //輸出圖檔 
            switch ($srcSize[2]) { 
                case 1: imageGif($destImage,$dest); break; 
                case 2: imageJpeg($destImage,$dest,$quality); break; 
                case 3: imagePng($destImage,$dest); break; 
            } 
            return true; 
        } 
        else { 
            return false; 
        } 
		ImageDestroy($dest);
		ImageDestroy($src);
    } 

//呼叫縮圖函式

    /*ImageCopyResizedTrue($arr["pic"],"pict1s.jpg",200,150);
	<img src="pict1s.jpg">
  */

  function pages($db,$sql_cmd,$num_of_rows_per_page,$curr_page){
     $rs = $db->Execute($sql_cmd);
     $one_rs = $db->PageExecute($sql_cmd, $num_of_rows_per_page, $curr_page);
     $rows = $rs->RowCount();
     $pages = ceil($rows/$num_of_rows_per_page);
     for($k=1;$k<=$pages;$k++) {
        $select=($k==$curr_page)?"selected":"";
        $page[] .= "<option value=$k $select>$k/$pages</option>";
     }
     $allpage = array();
     $allpage[rows] = $rows;
     $allpage[rs] = $one_rs;
     $allpage[page] = $page;
     return $allpage;
  }
}
$tools = new TOOLS;
?>
