:::

5. 新增、列出功能

一、 XOOPS的資料庫物件

  1. $xoopsDB是內建的資料庫物件
  2. 基本的連線XOOPS已經處理好了
  3. 在函數中記得global $xoopsDB;
  4. 加入資料表前置字串:$xoopsDB->prefix("資料表")
  5. 執行SQL語法:$result=$xoopsDB->query("SQL語法")
  6. 抓取資料陣列(名稱索引):$xoopsDB->fetchArray($result)
  7. 抓取資料陣列(數字索引):$xoopsDB->fetchRow($result)
  8. 最新流水號:$xoopsDB->getInsertId()

二、 XOOPS使用者物件:$xoopsUser

  1. 取得使用者編號的寫法:$uid=$xoopsUser->uid();
  2. 避免沒登入產生錯誤:$uid=($xoopsUser)?$xoopsUser->uid():0;
  3. $xoopsUser物件方法:
    <?php
    $xoopsUser->getVar('uid'):1 //使用者編號
    $xoopsUser->getVar('name'):吳大頭 //真實姓名
    $xoopsUser->getVar('uname'):tad //登入帳號
    $xoopsUser->getVar('email'):[email protected] //Email
    $xoopsUser->getVar('url'):http://localhost/x25/ //個人網站
    $xoopsUser->getVar('user_avatar'):avatars/cavt50877193c9788.png //頭像圖片
    $xoopsUser->getVar('user_regdate'):1294384702 //註冊日
    $xoopsUser->getVar('user_icq'):
    $xoopsUser->getVar('user_from'):
    $xoopsUser->getVar('user_sig'):
    $xoopsUser->getVar('user_viewemail'):1
    $xoopsUser->getVar('actkey'):
    $xoopsUser->getVar('user_aim'):
    $xoopsUser->getVar('user_yim'):
    $xoopsUser->getVar('user_msnm'):
    $xoopsUser->getVar('pass'):56ab24c15b72a457069c5ea42fcfc640
    $xoopsUser->getVar('posts'):12
    $xoopsUser->getVar('attachsig'):0
    $xoopsUser->getVar('rank'):7
    $xoopsUser->getVar('level'):5
    $xoopsUser->getVar('theme'):school2013
    $xoopsUser->getVar('timezone_offset'):8.0
    $xoopsUser->getVar('last_login'):1358862705
    $xoopsUser->getVar('umode'):thread
    $xoopsUser->getVar('uorder'):0
    $xoopsUser->getVar('notify_method'):1
    $xoopsUser->getVar('notify_mode'):0
    $xoopsUser->getVar('user_occ'):龍崎國小
    $xoopsUser->getVar('bio'):
    $xoopsUser->getVar('user_intrest'):114620
    $xoopsUser->getVar('user_mailok'):0
    $xoopsUser->getGroups():Array
    ?>
  4. 以uid取得使用者名稱
    $uid_name=XoopsUser::getUnameFromId($uid,1);
    if(empty($uid_name))$uid_name=XoopsUser::getUnameFromId($uid,0);

     

二、 寫入資料庫

  1. PHP的寫法:
    $sql="insert into `xx_tad_honor` (`honor_year` , `honor_date` , `honor_students` , `honor_descript` , `honor_teachers` , `honor_note`) values('$honor_year' , '$honor_date' , '$honor_students' , '$honor_descript' , '$honor_teachers' , '$honor_note' , '$uid')";
    mysql_query($sql) or die(mysql_error());
  2. XOOPS的寫法:
    $sql="insert into ".$xoopsDB->prefix("tad_honor")." (`honor_year` , `honor_date` , `honor_students` , `honor_descript` , `honor_teachers` , `honor_note` , `uid`) values('$honor_year' , '$honor_date' , '$honor_students' , '$honor_descript' , '$honor_teachers' , '$honor_note' , '$uid')";
    $xoopsDB->queryF($sql) or redirect_header('index.php', 3, mysql_error());

 

三、 列出全部

 

  1. 列出所有資料的SQL語法(幾乎所有撈資料的情形皆可套用,只要修改資料表名稱以及排序欄位即可):
    $sql="select * from ".$xoopsDB->prefix("tad_honor")." order by `honor_date` desc";
    $result=$xoopsDB->queryF($sql) or redirect_header('index.php', 3, mysql_error());
    $all_data="";
    $i=0;
    while($all=$xoopsDB->fetchArray($result)){
      $all_data[$i]=$all;
      $i++;
    }
    $xoopsTpl->assign("all_data" , $all_data);
    
  2. 其中 $all 是一個陣列,每抓出一筆資料,就會得到下列這樣的陣列:
    $all['honor_sn']='流水號的值';
    $all['honor_year']='學年度的值';
    $all['honor_date']='日期的值';
    $all['honor_students']='得獎者的值';
    $all['honor_descript']='得獎事項的值';
    $all['honor_teachers']='指導老師的值';
    $all['honor_note']='備註的值';
    $all['uid']='發布者的值';
  3. 當我們使用 $all_data[$i]=$all; 後,會產生如下的值(假如抓到三筆資料的話),換言之,當我們多加上一維(現在是二維陣列了)之後,一個 $all_data 就足以容納所有的資料內容:
    $all_data[0]['honor_sn']='第1筆資料流水號的值';
    $all_data[0]['honor_year']='第1筆資料學年度的值';
    $all_data[0]['honor_date']='第1筆資料日期的值';
    $all_data[0]['honor_students']='第1筆資料得獎者的值';
    $all_data[0]['honor_descript']='第1筆資料得獎事項的值';
    $all_data[0]['honor_teachers']='第1筆資料指導老師的值';
    $all_data[0]['honor_note']='第1筆資料備註的值';
    $all_data[0]['uid']='第1筆資料發布者的值';
    
    $all_data[1]['honor_sn']='第2筆資料流水號的值';
    $all_data[1]['honor_year']='第2筆資料學年度的值';
    $all_data[1]['honor_date']='第2筆資料日期的值';
    $all_data[1]['honor_students']='第2筆資料得獎者的值';
    $all_data[1]['honor_descript']='第2筆資料得獎事項的值';
    $all_data[1]['honor_teachers']='第2筆資料指導老師的值';
    $all_data[1]['honor_note']='第2筆資料備註的值';
    $all_data[1]['uid']='第2筆資料發布者的值';
    
    $all_data[2]['honor_sn']='第3筆資料流水號的值';
    $all_data[2]['honor_year']='第3筆資料學年度的值';
    $all_data[2]['honor_date']='第3筆資料日期的值';
    $all_data[2]['honor_students']='第3筆資料得獎者的值';
    $all_data[2]['honor_descript']='第3筆資料得獎事項的值';
    $all_data[2]['honor_teachers']='第3筆資料指導老師的值';
    $all_data[2]['honor_note']='第3筆資料備註的值';
    $all_data[2]['uid']='第3筆資料發布者的值';

四、產生XOOPS風格的表格

  1. 套用XOOPS的表格風格:<table cellspacing='1' class='outer'>,標題部份:<th class='txtcenter'>分類標題</th>,表格內容部份:<tr class='odd'>或<tr class='even'>
    <table cellspacing='1' class='outer'>
      <tr>
        <th class='txtcenter'>學年度</th>
        <th class='txtcenter'>得獎日期</th>
        <th class='txtcenter'>得獎者</th>
        <th class='txtcenter'>得獎事項</th>
        <th class='txtcenter'>指導老師</th>
      </tr>
     
      <tr class='odd'>
        <td>第1行學年度的內容</td>
        <td>第1行得獎日期的內容</td>
        <td>第1行得獎者的內容</td>
        <td>第1行得獎事項的內容</td>
        <td>第1行指導老師的內容</td>
      </tr>
    
      <tr class='even'>
        <td>第2行學年度的內容</td>
        <td>第2行得獎日期的內容</td>
        <td>第2行得獎者的內容</td>
        <td>第2行得獎事項的內容</td>
        <td>第2行指導老師的內容</td>
      </tr>
    
    </table>

五、利用Smarty迴圈產生列表內容

  1. smarty迴圈的寫法:
    <{foreach from=$all_data item=data}>
      <tr>
        <td><{$data.honor_year}></td>
        <td><{$data.honor_date}></td>
        <td><{$data.honor_students}></td>
        <td><{$data.honor_descript}></td>
        <td><{$data.honor_teachers}></td>
      </tr>
    <{/foreach}>
  2. 套用上BootStrap後的表格:
    <table class="table table-striped table-bordered table-hover">
      <tr>
        <th class='txtcenter'>學年度</th>
        <th class='txtcenter'>得獎日期</th>
        <th class='txtcenter'>得獎者</th>
        <th class='txtcenter'>得獎事項</th>
        <th class='txtcenter'>指導老師</th>
      </tr>
     
      <{foreach from=$all_data item=data}>
        <tr>
          <td><{$data.honor_year}></td>
          <td><{$data.honor_date}></td>
          <td><{$data.honor_students}></td>
          <td><{$data.honor_descript}></td>
          <td><{$data.honor_teachers}></td>
        </tr>
      <{/foreach}>
    
    </table>
    
    

六、 在樣板中使用判斷式

  1. 如果要在同一個樣板,可以選擇性的出現「表單」或者「資料列表」,那麼可以用 <{if}><{elseif}><{else}><{/if}>來做判斷。
    <{if $now_op=="honor_form"}>
      <!--顯示新增表單-->
    <{elseif $now_op=="honor_modify_form"}>
      <!--顯示修改表單-->
    <{else}>
      <!--顯示資料列表-->
    <{/if}>
  2. 當然,我們需要傳一個樣板變數讓樣板做判斷,例如:$now_op

七、加入後台管理頁面標題

  1. 請在頁尾之前加入:
    include_once XOOPS_ROOT_PATH."/modules/" . $xoopsModule->getVar("dirname") . "/class/admin.php" ;
    $index_admin = new ModuleAdmin() ;
    $admin_title=$index_admin->addNavigation('檔名.php') ;
    $xoopsTpl->assign("admin_title" , $admin_title);
  2. 接著在樣板檔套上 <{$admin_title}> 樣板標籤即可。

:::

搜尋

QR Code 區塊

https%3A%2F%2Fwww.tad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D25%26tbdsn%3D696

書籍目錄

展開 | 闔起

線上使用者

48人線上 (9人在瀏覽線上書籍)

會員: 0

訪客: 48

更多…