2014-10-27 156 views
0

我想在MVC體系結構中創建一個簡單的論壇。
這是我數據庫設置(相關部分):

表:forum_categories
在PHP MVC準備語句

`forum_categories` (
`cat_id` INT(8) NOT NULL AUTO_INCREMENT, 
`cat_title` VARCHAR(255) NOT NULL, 
`cat_desc` TEXT NOT NULL, 
PRIMARY KEY (`cat_id`), 
UNIQUE KEY (`cat_title`) 

表:forum_topics

`forum_topics` (
`topic_id` INT(8) NOT NULL AUTO_INCREMENT, 
`cat_id` INT(8) NOT NULL COMMENT 'foreign key with forum_categories table', 
`user_id` INT(11) NOT NULL COMMENT 'foreign key with users table', 
`topic_title` VARCHAR(255) NOT NULL, 
`topic_desc` TEXT NOT NULL, 
`topic_date` DATETIME DEFAULT NULL, 
PRIMARY KEY (`topic_id`), 
FOREIGN KEY (`cat_id`) REFERENCES forum_categories (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE 
的功能

,我想實現:
類別1有cat_id = 1
第2類具有CAT_ID = 2

主題1具有CAT_ID = 1
主題2具有CAT_ID = 2

現在選擇1類時,我只想話題1所示。
如果選擇了category2,我只想讓topic 2顯示。

該準備的SQL語句實現了:

PREPARE stmnt FROM 
    'SELECT * 
    FROM forum_categories fc 
    JOIN forum_topics ft ON fc.cat_id = ft.cat_id 
    WHERE fc.cat_id = ? 
    ORDER BY ft.topic_date DESC'; 

SET @a = 1; 
EXECUTE stmnt USING @a; 


問題:我想這個功能移動到我的PHP MVC結構。
這是我的嘗試,它不起作用(它顯示所有類別中的所有主題)。

控制器

/** 
* Show all the topics in the chosen category 
*/ 
public function showForumTopics() 
{ 
    $topic_model = $this->loadModel('Forum'); 
    $this->view->forum_topics = $topic_model->getForumTopics(); 
    $this->view->render('forum/viewTopics'); 
} 

型號

/** 
* Gets an array that contains all the forum topics in the database. 
* Each array element is an object, containing a specific topic's data. 
* @return array All the forum topics 
*/ 
public function getForumTopics($cat_id) 
{ 
    $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC'; 
    $query = $this->db->prepare($sql); 
    $query->execute(array(':cat_id' => $cat_id)); 

    return $query->fetchAll(); 
} 

查看

if ($this->forum_topics) { 
      foreach($this->forum_topics as $key => $value) { 
       echo '<p><strong>Title:</strong>' . $value->topic_title . '</p>'; 
       echo '<p><strong>Description:</strong> ' . $value->topic_desc . '</p>'; 
       echo '<p><strong>Author:</strong> ' . $value->topic_author . '</p>'; 
       echo '<p><strong>Date:</strong> ' . $value->topic_date . '</p>'; 
      } 
     } else { 
      echo 'No forum topics.'; 
     } 

幫助將不勝感激!謝謝!!

+0

是否$ CAT_ID都值,你希望它? – 2014-10-27 12:42:48

+0

不!這是我的問題。我希望$ cat_id的值由我選擇的類別來設置。 – Schwesi 2014-10-27 12:44:22

+0

你如何修改'$ cat_id'? – Elias 2014-10-27 12:47:38

回答

0

例如,你的頁面http://example.com/?cat_id=2 您的代碼應該是這樣的

控制器

public function showForumTopics() 
{ 
    $default_category = 1; 
    $topic_model = $this->loadModel('Forum'); 
    $cat_id = isset($_GET['cat_id']) ? $_GET['cat_id'] : $default_category; 
    $this->view->forum_topics = $topic_model->getForumTopics($cat_id); 
    $this->view->render('forum/viewTopics'); 
} 

型號

public function getForumTopics($cat_id) { 
    $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC'; 
    $query = $this->db->prepare($sql); 
    $query->execute(array(':cat_id' => $cat_id)); 

    return $query->fetchAll(); } 

查看

if ($this->forum_topics) { 
      foreach($this->forum_topics as $key => $value) { 
       echo '<p><strong>Title:</strong>' . $value->topic_title . '</p>'; 
       echo '<p><strong>Description:</strong> ' . $value->topic_desc . '</p>'; 
       echo '<p><strong>Author:</strong> ' . $value->topic_author . '</p>'; 
       echo '<p><strong>Date:</strong> ' . $value->topic_date . '</p>'; 
      } 
     } else { 
      echo 'No forum topics.'; 
     } 
+0

太棒了!謝謝!不幸的是,我不能使用URL(http://example.com/?cat_id=2),因爲我在.htaccess文件中改變它,由於MVC結構(它將是http://example.com/forum/showForumTopics /不管id)。但這比其他任何東西都更接近答案......謝謝你的努力! – Schwesi 2014-10-27 14:12:01

0

你的問題是你的後端需要和ID來拉特定類別(並通過連接,正確的主題)。在您的數據庫查詢中,您在此處查找:WHERE fc.cat_id = ?

您的getForumTopics($cat_id)函數還需要將該ID傳遞到準備好的語句中。問題是你不能傳遞任何ID到該功能時,你怎麼稱呼它:

$this->view->forum_topics = $topic_model->getForumTopics();

因此,沒有任何透進來,你的功能現在打破,應該拋出一個錯誤。你在這一點上有兩種選擇:

  1. 從該頁面提供的ID,通過控制器的功能(提示,你必須把它添加到URL像@kringeltorte提出這樣的頁面知道什麼加載!)
  2. 進行備份,列出未選擇特定類別時的所有主題。你會做,在你的函數定義是這樣的:

    // Specifying the backup option null here, for when there is no ID passed 
    public function getForumTopics($cat_id = null) { 
    
        // Simple check for an ID 
        if ($id) { 
    
         // Run the code you had before 
         $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC'; 
         $query = $this->db->prepare($sql); 
         $query->execute(array(':cat_id' => $cat_id)); 
    
         return $query->fetchAll(); } 
    
        } else { 
    
         // Otherwise, do the same thing as above but without a WHERE clause 
        } 
    
    }