用PHP-MVC编写语句
我试图在MVC架构中创建一个简单的论坛。用PHP-MVC编写语句,php,select,prepared-statement,Php,Select,Prepared Statement,我试图在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` ( `topic_i
这是我的数据库设置(相关部分):
表:论坛类别
`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` (
`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的类别id=2
主题1的类别id=1
主题2的类别id=2
现在,当选择类别1时,我只想显示主题1。
如果选择了category2,我只想显示主题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');
}
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');
}
模型
看法
if($this->forum\u主题){
foreach($key=>value形式的本->论坛主题){
回声“标题:”.$value->topic_Title.”;
回声“说明:”.$value->topic_desc.”;
回音“作者:”.$value->topic_Author.”;
回显“日期:”.$value->topic_Date.”;
}
}否则{
回应“没有论坛主题”;
}
非常感谢您的帮助!谢谢 例如,您的页面
您的代码应该是这样的
控制器
/**
* 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');
}
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');
}
模型
看法
if($this->forum\u主题){
foreach($key=>value形式的本->论坛主题){
回声“标题:”.$value->topic_Title.”;
回声“说明:”.$value->topic_desc.”;
回音“作者:”.$value->topic_Author.”;
回显“日期:”.$value->topic_Date.”;
}
}否则{
回应“没有论坛主题”;
}
您的问题是后端需要和ID来提取特定类别(以及通过加入,正确的主题)。在DB查询中,您在此处查找它:WHERE fc.cat\u id=?
您的getForumTopics($cat_id)
函数还需要将id传递到准备好的语句中。问题是在调用该函数时,没有将任何ID传递给该函数:
$this->view->forum_topics=$topic_model->getForumTopics()代码>
因此,如果没有任何结果,您的函数现在已被破坏,应该抛出一个错误。此时,您有两个选项:
从页面通过控制器向函数提供一个ID(提示,您必须将其添加到URL中,如@kringeltore建议的那样,以便页面知道要加载什么!)
在未选择特定类别时,进行备份,列出所有主题。您可以在函数定义中使用如下内容:
// 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
}
}
$cat_id是否具有您期望的值?否!这是我的问题。我希望$cat\u id的值由我选择的类别设置。您如何修改$cat\u id
?哦,是的,我明白了,您没有将它传递到getForumTopics()方法中,它最初来自哪里?GET/POST?例如,当我将一个主题保存在类别1(cat_id=1)中时,该主题将具有外键cat_id=1。之后我根本不修改它。太好了!非常感谢。不幸的是,我不能使用URL(),因为我在.htaccess文件中更改了它,这是由于MVC结构(无论id如何)。但这比其他任何东西都更接近答案。。。谢谢你的努力!!
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.';
}
// 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
}
}