:::
所有書籍
「[1032]PHP入門」目錄
MarkDown
6-4 index.php
1. PHP開發環境與表單
1-1 post.php
1-2 index.php
2. 邏輯判斷及樣板引擎
2-1 index.php
2-2 post.php
2-3 index_tpl.html
3. MySQL的資料存取
3-1 index.php
3-2 post.php
3-3 post_tpl.html
3-4 eznews.sql
4. PHP函數與引入
4-1 post.php
4-2 index.php
4-3 list_tpl.html
4-4 function.php
5. 編輯與刪除資料
5-1 index.php
5-2 list_tpl.html
5-3 post.php
5-4 config.php
5-5 function.php
5-6 post_tpl.html
6. BootStrap各式應用
6-1 post.php
6-2 list_tpl.html
6-3 post_tpl.html
6-4 index.php
7. 置頂、計數器與 join
7-1 post.php
7-2 post_tpl.html
7-3 index.php
7-4 list_tpl.html
7-5 function.php
7-6 eznews.sql
8. 上傳與分頁
8-1 post.php
8-2 index.php
8-3 config.php
8-4 post_tpl.html
8-5 eznews.sql
9. 身份認證機制
9-1 eznews.sql
9-2 post_tpl.html
9-3 list_tpl.html
9-4 config.php
9-5 index.php
9-6 post.php
7-1 post.php
\[1032\]PHP入門 ============= [](http://www.tad0616.net/uploads/tad_book3/file/1032/7.pdf) ### 一、 整理post.php 1. 顏我們將發布表單的HTML語法放在post\_tpl.html樣板檔中,如此,會造成post.php不好維護的問題,因此,我們將發布表單做成函數,並傳回完整表單語法。 2. 可利用Heredoc定界符<<<的用法(左邊不可有任何空白)來做: ``` <pre class="brush:php;"> $form = <<<form //表單HTML語法(這裡面要秀出$或"或'都不用加\) form; ``` 3. 樣板中,原本的表單位置請用\[var.main\]及\[var.error\_msg\]樣板標籤代替之。 4. 預設流程中,請去呼叫發布表單的函數。該函數需可傳入一個流水號參數,以便讓程式判斷是新增還是修改。若是修改(有流水號),則讀出該筆資料預設值。 ### 二、 置頂功能、醒目功能 1. 所謂置頂就是會永遠秀在最上方的新聞。簡單想,就是優先讀出的新聞。 2. 如何讓系統知道此新聞要優先讀出?很簡單,多設一個欄位(如:status)來註記即可。 3. 請編輯eznews.sql檔,加入一組欄位(同時記得真的去資料表新增一個欄位): ``` <pre class="brush:sql;"> `status` varchar(255) NOT NULL, ``` 4. 表單部份,請多一個「置頂」的勾選鈕(可用checkbox)。 5. 依序修改儲存、更新的SQL語法。 6. 讀出時,方法有二,比較簡單的想法是先讀出「status='置頂'」的文章,再讀出「status!='置頂'」即可。但缺點是要分兩次處理,另一個比較好的方法是利用order 欄位='指定值' desc 的方式來處理: ``` <pre class="brush:sql;"> select * from eznews order by status='置頂' desc , a.post_time desc"; ``` 7. 同樣的方法,亦可製作所謂的醒目功能(亮色底色)。 ### 三、 計數器 1. 請編輯eznews.sql檔,加入一組欄位(同時記得真的去資料表新增一個欄位): ``` <pre class="brush:sql;"> `counter` smallint(5) unsigned NOT NULL, ``` 2. 當使用者進入某篇文章時,計數器就+1,可以寫個函數來做此事。 3. 主要就是去更新資料表的counter欄位: ``` <pre class="brush:php;"> update eznews set counter=counter+1 where sn='$sn' ``` 4. 特別注意!更新文章時,切勿更新counter欄位。 5. 讀出時,可用Bootstrap的徽章樣式來套用到計數器上: ``` <pre class="brush:xml;"> <span class='badge badge-info'>{$news['counter']}</span> ``` ### 四、 製作圖形計數器 (補充) 1. 請建立一個新檔案,例如:counter.php,其內容如下: ``` <pre class="brush:php;"> <?php $counter=isset($_REQUEST['counter'])?intval($_REQUEST['counter']):0; header("Content-type: image/png"); $im = @imagecreatetruecolor(28, 18) or die("無法建立圖片!"); $text_color = imagecolorallocate($im, 255, 255, 255); imagestring($im, 2, 5, 2, $counter, $text_color); imagepng($im); imagedestroy($im); ?> ``` 2. imagecreatetruecolor() 用來建立一個全彩的圖片格式,裡面分別是寬和高。 3. imagecolorallocate() 用來設定顏色,分別為 R(紅)G(綠)B(藍),值從 0~255 4. imagestring() 用來加上文字,第二個參數是字形型,有1~6種,第三、四個參數是x,y的位置,接著填入欲呈現數字(或英文),最後填入顏色。 5. imagepng()是做成png圖檔 6. imagedestroy()用來釋放圖檔佔用的記憶體。 7. 最後,請在要呈現的地方,利用HTML圖片語法即可秀出該圖: ``` <pre class="brush:xml;"> <img src=”counter.php?counter=12345”> ``` ### 五、 一次讀兩個以上的資料表 1. 關聯資料表方便的地方就是:一次可以讀取兩個以上的資料表,簡易語法如下: ``` <pre class="brush:sql;"> select a.* , b.* , c.* from `資料表1` as a join `資料表2` as b on a.`索引欄位`= b.`索引欄位` join `資料表3` as c on b.`索引欄位`= c.`索引欄位` where a.欄位='值' and …. ``` 2. 「left join」代表以左邊為主,順便到右邊撈撈看有無指定的資料。 3. 「right join」代表以右邊為主,順便到左邊撈撈看有無指定的資料。 4. 「join」代表兩邊都要同時有資料,否則該筆資料不會出現。 5. 以本例而言,讀出新聞順便讀出新聞分類的寫法: ``` <pre class="brush:sql;"> select a.*,b.* from eznews as a left join eznews_cate as b on a.cate_sn=b.cate_sn order by a.post_time desc ``` 6. 此處用left join,這樣才不會因為沒有設定新聞分類而導致新聞出不來。 ### 六、 其他常用的join的方式 1. inner join: ``` <pre class="brush:sql;"> select a.* , b.* from `資料表1` as a , `資料表2` as b where a.`索引欄位`= b.`索引欄位` ``` 2. natural join:自動搜尋兩資料表相同欄位,自動對應 ``` <pre class="brush:sql;"> select a.* , b.* from `資料表1` as a natural join `資料表2` as b ```
:::
搜尋
search
進階搜尋
QR Code 區塊
快速登入
所有討論區
「PHP全端開發」線上課程討論區
XOOPS使用討論區
一般研習學員
社大學員專用
路過哈啦區
XOOPS佈景設計
XOOPS模組開發
Tad書籍區
即時留言簿
書籍目錄
總目錄
1.PHP開發環境與表單
1-1post.php
1-2index.php
2.邏輯判斷及樣板引擎
2-1index.php
2-2post.php
2-3index_tpl.html
3.MySQL的資料存取
3-1index.php
3-2post.php
3-3post_tpl.html
3-4eznews.sql
4.PHP函數與引入
4-1post.php
4-2index.php
4-3list_tpl.html
4-4function.php
5.編輯與刪除資料
5-1index.php
5-2list_tpl.html
5-3post.php
5-4config.php
5-5function.php
5-6post_tpl.html
6.BootStrap各式應用
6-1post.php
6-2list_tpl.html
6-3post_tpl.html
6-4index.php
7.置頂、計數器與 join
7-1post.php
7-2post_tpl.html
7-3index.php
7-4list_tpl.html
7-5function.php
7-6eznews.sql
8.上傳與分頁
8-1post.php
8-2index.php
8-3config.php
8-4post_tpl.html
8-5eznews.sql
9.身份認證機制
9-1eznews.sql
9-2post_tpl.html
9-3list_tpl.html
9-4config.php
9-5index.php
9-6post.php
展開
|
闔起
線上使用者
31
人線上 (
7
人在瀏覽
線上書籍
)
會員: 0
訪客: 31
更多…
:::
主選單
NTPC OpenID
活動報名
模組控制台
進階區塊管理
站長工具箱(急救版)
網站地圖
Tad Tools 工具包
站長工具箱
行事曆
討論留言
嵌入區塊模組
快速登入
網站計數器
好站連結
最新消息
檔案下載
線上書籍
電子相簿
影音播放
常見問題
萬用表單
友站消息
社大學員
新聞
下載
教材
影音
討論
其他選單
好站連結
行事曆
電子相簿
常見問題
萬用表單
即時留言簿
友站消息
社大學員
登入
登入
帳號
密碼
登入