變身多人使用月曆-notes部份(使用WordPress $wpdb)
一、資料表格的處理 – 增加user_login欄位 (參考wp_myCalendar_theme資料表格)
資料表格user_login欄位加上使用者帳號資料,用來分別記事資料是屬於那些使用者:
二、我們先做修改讀取(Read)記事資料的功能
函式getNoteData一開頭的地方,我們加入使用者的取得,並在讀取記事資料時加上where子句:
//呼叫wp_get_current_user方法得WordPress的使用者資料,並放入$current_user這個全域變數中 global $current_user; wp_get_current_user(); global $wpdb; //宣告WordPress的全域資料庫物件 $table = $wpdb->prefix.'myCalendar_notes'; $data = $wpdb->get_results("SELECT * FROM $table where user_login = '$current_user->user_login'");
原本的程式:(請上下對照一下,我們加了什麼,有多很多程式嗎?一點都沒有…)
global $wpdb; //宣告WordPress的全域資料庫物件 $table = $wpdb->prefix.'myCalendar_notes'; $data = $wpdb->get_results("SELECT * FROM $table");
完整的getNoteData函式列表
<!-- 記事資料讀取 --> <?php function getNoteData(){ //呼叫wp_get_current_user方法得WordPress的使用者資料,並放入$current_user這個全域變數中 global $current_user; wp_get_current_user(); global $wpdb; //宣告WordPress的全域資料庫物件 $table = $wpdb->prefix.'myCalendar_notes'; $data = $wpdb->get_results("SELECT * FROM $table where user_login = '$current_user->user_login'"); $id = 0; $color = 1; $text = ""; foreach ($data as $row) { $id = $row->note_id; $color = $row->note_color; $text = $row->note_text; // while($row = mysqli_fetch_assoc($result)){ // $id = $row['note_id']; // $color = $row['note_color']; // $text = $row['note_text']; //以上為php碼 ?> <script type="text/javascript"> postIt = { id: <?php echo json_encode($id); ?>, note_num: <?php echo json_encode($color); ?>, note: <?php echo json_encode($text); ?> } postIts.push(postIt); </script> <?php //再接著php碼,這種寫法在混合式的php、html、JavaScript很常見的寫法,要習慣。 } } ?>
結果畫面 (只顯示屬於目前登入的使用者的記事資料)
三、修改記事資料的新增(Insert)
db_insertNote函式完整列表
function db_insertNote($uid, $color, $text){ //新增記事資料函式 //呼叫wp_get_current_user方法得WordPress的使用者資料,並放入$current_user這個全域變數中 global $current_user; wp_get_current_user(); global $wpdb; //宣告WordPress的全域資料庫物件 $text = $wpdb->_real_escape($text); $wpdb->insert( $wpdb->prefix."myCalendar_notes", array( "note_id" => $uid, "note_color" => $color, "note_text" => $text, "user_login" => $current_user->user_login ), array( "%s", "%s", "%s", "%s" ) ); // $query = "INSERT INTO notes(note_id, note_color, note_text, user_login) VALUES('$uid', '$color', '$text', $current_user->user_login)"; // $result = mysqli_query($connection, $query); }
修改的部份:
- 取得目前登入的使用者帳號
- 在$wpdb->insert中加入user_login的欄位名稱與值,還有array加一個%s
That is it. (就這樣而已… )
注意:這裏有一個地方,我沒有處理,如果使用者沒有登入的話,應該不能插入記事資料,你們想想在那邊加比較好?基本上,在前端與後端都可以處理這個問題,我把這個問題留給同學自己去想了。自我挑戰一下。我建議是參考我們在index.php後面JavaScript程式碼一個判斷使用者是否有登入的if:
if (typeof currentUserName !== 'undefined') {//若currentUserName不是未定義的話,換句話說,就是使用者有登入的…
把這個if加在前端關於記事資料的新增上… (更新、刪除可以不用做,因為當前的使用者沒有記事資料,就不會有記事圖示,沒有記事圖示就不能叫出記事資料對話方塊…)
四、修改記事資料的更新(Update)
function db_updateNote($uid, $text){//更新記事資料函式 //呼叫wp_get_current_user方法得WordPress的使用者資料,並放入$current_user這個全域變數中 global $current_user; wp_get_current_user(); global $wpdb; //宣告WordPress的全域資料庫物件 $text = $wpdb->_real_escape($text); $wpdb->update( $wpdb->prefix."myCalendar_notes", array( "note_text" => $text ), array( "note_id" => $uid, "user_login" => $current_user->user_login ), array("%s"), array( "%s", "%s" ) ); // die( $wpdb->prefix."myCalendar_notes" ); // $query = "UPDATE notes SET note_text = '$text' WHERE note_id = '$uid' LIMIT 1"; }
五、修改記事資料的刪除(Delete)
function db_deleteNote($uid){ //刪除記事資料函式 //呼叫wp_get_current_user方法得WordPress的使用者資料,並放入$current_user這個全域變數中 global $current_user; wp_get_current_user(); global $wpdb; //宣告WordPress的全域資料庫物件 $wpdb->delete( $wpdb->prefix."myCalendar_notes", array( "note_id" => $uid, "user_login" => $current_user->user_login ), array( "%s", "%s" ) ); // $query = "DELETE FROM notes WHERE note_id = '$uid'"; }