2014-10-27 158 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 
        } 
    
    }