2008年2月23日 星期六

php分頁

程式端相關程式:
相關提要,此分頁範例為新聞,會有相關的類別,在切換類別時又會有分頁的問題。
比如說 政治這個類別 又15筆資料,當$p為10就會產生分頁,會有 [1][2]需要切換。
到時就需要借助cat 這個變數。那cat這個變數會有兩種方式丟出(POST及GET)
$p=10;  //每頁顯示10筆
$px=5;   //每頁顯示跳頁用的5筆
$col_id=intval($_GET['cat']); //get
$post_kind_id=$_POST['kind_id'];//post

if($post_kind_id!=''){
 $col_id=$post_kind_id;
 $page=""; #避免之前選擇的頁數影響之後post出去的page頁面
 $sql_where_kind=" where kind_id='$col_id'";
 $sql="SELECT * FROM `".DBPREFIX."_news`  ".$sql_where_kind;
 $data=lazy_get_data($sql);
}else{
 $data=lazy_get_data($sql);
}
if($col_id!=""){
 $sql_where_kind=" where kind_id='$col_id'";
 $sql="SELECT * FROM `".DBPREFIX."_news`".$sql_where_kind;   
 $data=lazy_get_data($sql);
}
 
$snStr="issue_news.php?cat={$col_id}";

$total =$num;
$show=ceil($total/$p); //每頁顯示$P筆
if(isset($page)){
 $page=$page;
}else if(isset($_GET['page']))
{
 $page=$_GET['page'];
}else{
 $page=0;
}
   
$sqlproducts=$sql. " ORDER BY  top_sn desc ,news_sn desc "." LIMIT ".($page*$p).",".$p;
    
   
$data=lazy_get_data($sqlproducts);


//放在table最下面
array_page($total,$page,$p,$px,$snStr);


以下為副程程式:
一、只要給這支function資料庫的總筆數(用count去算就好了)
二、原本的select查詢,最後面加上limit ".($page*$p).",".$p;
三、在表格最後面,把function這個名字貼上
   array_page($totals_rows,$page,$p,$px,$new_Link);
四、$new_Link就是要該網頁,原本有些 $_GET 要回傳,就加在這裡...
   例如:http://localhost/test.php?mode=old   ---> 查歷史資料 $new_Link 就
                                                  給他 'mode=old'
山人覺得優點就是...
database用 limit 去限制每次查的大小,可以節省資料庫抓資料數量的負擔
用count去算database的內容也快

原本的SQL語法如...
$sql="select id,name,sex from humandata";

要改成二支
一支如第二點所說的 :
   $sql="select id,name,sex from humandata limit ".($page*$p).",".$p;
另一支就是要算筆數
   $sql="select count(*) from humandata";
或擔心二支SQL算出不用筆數,就偷懶這樣寫
   $sql="select count(*) from (原sql語法) as a ";

   接著...$totals_rows=mysql_result(mysql_query($sql),0,0);

一點點小小的心得供大家參考...也希望大家能多多指教

 //自動產生分頁排序說明
 //版本1.1
 //開發者:羽山秋人
 //時間:2007414
 //第二版修正於:2007416
 //使用方法
 /* array_page(
               $totals_rows  $資料庫算出的總筆數,
               $page         $目前的頁碼常用
               $p            $每頁顯示的筆數
               $px           $每頁要顯示多少個【第 xx 頁】
               $new_Link     $跳頁用的網頁帶入值  ---> ?以後原本傳的值

               P.S:需自行在 SQL 語法最後加上 limit ".($page*$p).",".$p;
               P.S:$p、$px、$page 請加注在 檔案開頭 以上

         limit ".($page*$p).",".$p; //加在原本沒分頁的SQL語法最後(mysql only)
//要加開網頁開頭的部分-------------------start
         $p=10;  //每頁顯示5筆
         $px=5;   //每頁顯示跳頁用的5筆
               if(isset($page))
               {
                 $page=$page;
               }
               else if(isset($_GET['page']))
               {
                 $page=$_GET['page'];
               }
               else
               {
                 $page=0;
               }
//要加在網頁開頭的部分-------------------end
 */
 function array_page($totals_rows,$page,$p,$px,$new_Link)
 {
       //傳說中的分頁
       //$p=5; // 每頁顯示5筆
       //$px=5; //每頁限制最多5頁,超過就用「下5頁」上5頁
       $page_range_start=floor($page/$px)*$px;
       $page_range_end=$page_range_start+$px;
       //自動算幾頁
       $totals_page=ceil($totals_rows/$p);
       if($page_range_end>$totals_page)
       {
         $page_range_end=$totals_page;
       }
       //echo $page_range_start;
       //echo "
";
       //echo $page_range_end;
       //echo "
";
       if($page-($page%$px)>=$px)
       {
           echo "【上".$px."頁】             ";
       }
       if(($page-$page%$px)<$totals_page-$px)
       {
         if(($page+$px)>=$totals_page) //修正加上page的頁碼超過最終頁碼 2007/4/16
         {
           $temp=$totals_page-1;
         }
         else
         {
           $temp=$page+$px;
         }
           echo "【下".$px."頁】";
       }
       echo "
";
       for($i=$page_range_start;$i<$page_range_end;$i++)
       {
         if($page==$i)
           echo "【第 ".($i+1)." 頁】";
         else
           echo "【第 ".($i+1)." 頁】";
       }
       echo "
第【".($page+1)."】頁
"; echo "合計共【".$totals_rows."】筆/共【".$totals_page."】頁"; //分頁結束 } ?>

2008年1月16日 星期三

物件


//seal類別
class seal {
//屬性
public $name;
public $shape;
public $diameter;
public $length;
//建構子
function __construct($name,$shape,$diameter,$length) {
$this->name=$name;
$this->shape=$shape;
$this->diameter=$diameter;
$this->length=$length;
}
public function stamp()
{
echo "(印)".$this->name."\n";
}
}
header("Contet-type:text/plain;charset=big5");
//建立實體
$obj=new seal("高島","圓柱",10,60);
//屬性
echo"私的印章的形狀是「".$obj->shape."」\n";
//方法
$obj->stamp();

列出已被定義函數、變數



header("Content-Type:text/plain; charset=big5");

if(function_exists("mb_send_mail")){
echo "mb_send_mail函式已定義";
}else{
echo "mb_seind_mail函式沒定義";
}

print_r(get_defined_functions());/*列出已被定義的函數 */
print_r(get_defined_vars());/*列出已定義的變數 */

2008年1月6日 星期日

array_map(PHP函式)

說明
array array_map (mix callback , array $arr1 [, array $... ] )
傳入參數:參數1是使用者自定的函式(callback function)的名稱,參數2到n是陣列型態的參數。
基本說明:依照使用者自定的函式,對陣列的元素組做處理。


function cube($n){
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);

結果:

Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)


Example#2 array_map() - using more arrays

function show_Spanish($n, $m)
{
return("The number $n is called $m in Spanish");
}
function map_Spanish($n, $m)
{
return(array($n => $m));
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);


print_r($c);



$d = array_map("map_Spanish", $a , $b);
print_r($d);

結果

// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
// printout of $d
Array
(
[0] => Array
(
[1] => uno
)
[1] => Array
(
[2] => dos
)
[2] => Array
(
[3] => tres
)
[3] => Array
(
[4] => cuatro
)
[4] => Array
(
[5] => cinco
)
)


簡易資料驗證(javascript)


<HTML>
<HEAD>
<TITLE>簡易資料驗證</TITLE>
<script language="javascript">
function myFunction(){
try{
if(document.myForm.ID.value !="test"){
throw "idError"
}else if(document.myForm.psw.value !="test"){
throw"pswError"
}else{
alert("身份驗證通過")
}
}catch(e){
if(e=="idError")alert("帳號資料不符")
if(e=="pswError")alert("密碼資料不符")
}
}
</script>
</HEAD>
<BODY >
<center>
進入會員專區前,請先登入:<BR>
<form name="myForm">
帳號:<input type="text" name="ID" size="10"><br>
密碼:<input type="password" name="psw" size="10"><br>
<input type="button" value="身份驗證" onClick="myFunction()">

</form>
</BODY>
</HTML>