posts.php
特定の投稿とそのコメントを表示するページを作成し、ユーザーがこの投稿に新しいコメントを動的に追加できるようにしました。
*"submit_comment"* を Ajax で実装したいのですが、MVC での実装方法がよくわかりません。
これは私の Posts.php です:
<script type="application/javascript" src="Ajax/addComment.js"> </script>
src="../views/Ajax/addComment.js"> </script> <title> Posts
(View)</title> </head>
<body> <div id="main"> <div class="container"> <?=$data['header'];?>
<div id="content">
<!-- Main Post -->
<div class="content-background">
<h2> <?=$data['title'];?></h2>
<h4> <?=$data['date'];?> </h4>
<p> <?=$data['content'];?></p>
</div>
<div id="form-content">
<form name="commentForm" method="post">
Enter your name: <input type="text" name="username" id="username"> <br />
Enter your comment: </br> <textarea name="comment" id="comment" cols="10" rows="10"> </textarea> <br />
<input value='Submit' type='button' onclick='JavaScript:commentRequest2("<?=$data['id']?>")'
name='submit'>
</form>
</div>
<div id="commentArea">
<?=include_once("comments_area.php");?>
</div><!-- end commentArea -->
</div> </div> </div>
</body> </html>
これは私の Posts_Controller.php です:
<?php
/**
* This file handles the retrieval and serving of posts posts
*/
class Posts_Controller
{
/**
* This template variable will hold the 'view' portion of our MVC for this
* controller
*/
public $template = 'posts';
/**
* This is the default function that will be called by router.php
*
* @param array $getVars the GET variables posted to index.php
*/
public function main(array $getVars)
{
$postsModel = new Posts_Model;
}
else{
//get the post
$post = $postsModel->get_main_post("`id`='", $getVars['id'], "LIMIT", "1");
//get the comments
$comments = $postsModel->get_comments("`postID`='", $getVars['id']);
//create a new view and pass it our template
$header = new View_Model('header_template');
$view = new View_Model($this->template);
//assign post data to view
$view->assign('header', $header->render(FALSE));
$view->assign('title' , $post['title']);
$view->assign('content' , $post['content']);
$view->assign('date' , $post['date']);
$view->assign('by' , $post['added_by']);
$view->assign('id' , $post['id']);
$view->assign('commentsArr' , $comments);
$view->render();
}
}
}
これが私の Posts_Model.php です。
<?php
/**
* The Posts Model does the back-end heavy lifting for the Posts Controller
*/
class Posts_Model
{
/**
* Holds instance of database connection
*/
private $db;
public function __construct()
{
$this->db = new MysqlImproved_Driver;
}
/**
* Fetches article based on supplied name
*
* @param string $author
*
* @return array $article
*/
public function get_main_post($cond1, $var1, $cond2 ="", $var2 = "")
{
//connect to database
$this->db->connect();
//sanitize data
$var1 = $this->db->escape($var1);
$var2 = $this->db->escape($var2);
$cond = $cond1.$var1."'";
$cond.= " ".$cond2." ".$var2;
//prepare query
$this->db->prepare
(
"
SELECT * FROM `posts`
WHERE $cond
;
"
);
//execute query
$this->db->query();
$article = $this->db->fetch('array');
return $article;
}
public function get_comments($cond1, $var1, $cond2 ="", $var2 = "")
{
//connect to database
$this->db->connect();
//sanitize data
$var1 = $this->db->escape($var1);
$var2 = $this->db->escape($var2);
$cond = $cond1.$var1."'";
$cond.= " ".$cond2." ".$var2;
//prepare query
$this->db->prepare
(
"
SELECT * FROM `comments`
WHERE $cond
;
"
);
//execute query
$this->db->query();
$resultArr[0] = 0;
$i = 1;
while( $result = $this->db->fetch('array')){
$resultArr[$i] = $result;
$i++;
}
$resultArr[0] = $i;
return $resultArr;
}
}
?>
addComment.js はどこに追加すればよいですか? (V、M、または C?) そして、addComment は何をすべきでしょうか? どの URL/関数を呼び出すか?
助言がありますか?例?