【发布时间】:2018-09-23 00:18:53
【问题描述】:
我已经在线学习编程 3 年了。这段时间我开发了一个大项目,但是我有一个问题。我不知道 MVC 模式,并且是如何说“从头开始编程”。现在我的代码一团糟,没有人能理解,只有我能理解..
我发现了这种 MVC 模式,这是一件很棒的事情,但现在我不明白在哪里以及如何制作一些东西。我怎么理解没有 php 代码可以查看?并且没有 html/css 进入模型。
例如,我必须在哪个结构中实现我的 javascript 和 ajax 代码? (是视图吗?) 在哪里以及如何管理显示 if's?喜欢:
if($user_id == $me){
//display post with delete option
}else{
//display post
}
我有一个包含数百行和 if 的函数。例如我的功能之一。我想了解如何以 MVC 模式重现它。
public function selectUserPosts(){
try{
require_once('Class.Users.php');
$user = new USER();
$id = $_GET['id'];
$stmt = $this->conn->prepare("SELECT * FROM fun_posts WHERE addedby=('$id') ORDER BY date DESC");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $post){
?>
<div class="col-sm-4">
<div class="animated flipInY" id="panel_<?php echo $post['id'];?>">
<div class="thumbnail" style="height:300px;">
<a href="/Web/Pages/Fun/Fun_Post.php?action=select&image_id=<?php echo $post['id'];?>" target="_blank">
<img class="img" style="width: 100%; height:150px;" src="<?php echo $post['image']; ?>" alt="" />
</a>
<i class="fa fa-clock-o" aria-hidden="true"></i><?php echo $user->time_elapsed($post['date']); ?>
<div id="upvote_<?php echo $post['id'];?>" class="panel">
<i class="fa fa-arrow-circle-up" style="font-size:22px; margin-top:10px;"></i> <b id="upvote_panel_<?php echo $post['id'];?>"><?php echo $post['upvotes']; ?></b>
<button style="float:right; margin-top:5px; width:90px;" class="btn btn-sm btn-success" type="submit"><i class="fa fa-arrow-circle-up"></i> Upvote</button>
</div>
<div id="downvote_<?php echo $post['id'];?>" class="panel">
<i class="fa fa-arrow-circle-down" style="font-size:22px; margin-top:-5px;"></i> <b id="downvote_panel_<?php echo $post['id'];?>"><?php echo $post['downvotes']; ?></b>
<button style="float:right; margin-top:-10px; width:90px;" class="btn btn-sm btn-danger" type="submit"><i class="fa fa-arrow-circle-down"></i> Downvote</button>
</div>
<div id="comment_<?php echo $post['id'];?>" class="panel">
<i class="fa fa-comment" style="font-size:22px; margin-top:-10px;"></i> <b id="comment_panel_<?php echo $post['id'];?>"><?php echo $post['comments']; ?></b>
<a href="/Web/Pages/Fun/Fun_Post.php?action=select&image_id=<?php echo $post['id'];?>" target="_blank">
<button style="float:right; margin-top:-13px; width:90px;" class="btn btn-sm btn-primary" type="submit"><i class="fa fa-comment"></i> Comment</button>
</a>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$("#upvote_<?php echo $post['id'];?>").click(function(){
$.ajax(
{ url: "Home.php?upvote-btn=true?action=select&image_id=<?php echo $post['id'];?>",
type: "get",
success: function(result){
$('#upvote_panel_<?php echo $post['id'];?>').load(document.URL + ' #upvote_panel_<?php echo $post['id'];?>');
$('#downvote_panel_<?php echo $post['id'];?>').load(document.URL + ' #downvote_panel_<?php echo $post['id'];?>');
$('#comment_panel_<?php echo $post['id'];?>').load(document.URL + ' #comment_panel_<?php echo $post['id'];?>');
document.getElementById('result-box').innerHTML += result;
}
});
});
$("#downvote_<?php echo $post['id'];?>").click(function(){
$.ajax(
{ url: "Home.php?downvote-btn=true?action=select&image_id=<?php echo $post['id'];?>",
type: "get",
success: function(result){
$('#upvote_panel_<?php echo $post['id'];?>').load(document.URL + ' #upvote_panel_<?php echo $post['id'];?>');
$('#downvote_panel_<?php echo $post['id'];?>').load(document.URL + ' #downvote_panel_<?php echo $post['id'];?>');
$('#comment_panel_<?php echo $post['id'];?>').load(document.URL + ' #comment_panel_<?php echo $post['id'];?>');
document.getElementById('result-box').innerHTML += result;
}
});
});
});
</script>
<?php
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
【问题讨论】:
-
这不是准备好的语句的使用方式。参数化查询。
-
你在寻找这样的东西吗:stackoverflow.com/a/5864000/727208
-
@tereško 谢谢,我会调查的。
标签: javascript php model-view-controller