【发布时间】:2012-05-22 11:04:59
【问题描述】:
我正在拼命地朝着 OOP 方向发展,但我不知道什么时候使用它。我得到了机制,但何时使用它们只是不点击。我很好奇我目前的情况是否适合 OOP 方法。
我有 3 页。 Details.php 显示两个并排的 div。一个用户可以添加注释,另一个可以查看存储在 MySQL 中的以前的注释。他们可以通过 Details.php 中的 AJAX 功能添加注释和提取注释。 javascript 函数调用 add_notes.php 向数据库添加注释,并调用 load_notes.php 通过 Jquery .load() 在页面上加载注释以及提交新注释以刷新 div。
我是新手,但我从骨子里觉得有更好的方法来组织这段代码。我会研究一个框架,但我已经深入到这个项目中,所以正在寻找关于如何更好地分解它或验证我正在以尽可能简化的方式进行它的 OOP 想法。所有 cmets 都有帮助!
DETAILS.PHP
<script type="text/javascript">
$(document).ready(function(){
//When loading page load notes/messages tables and then reload when ajax is done
$('#note_holder').load('load_notes.php?subcat=<? echo $subcat;?>');
//onclick handler send message btn
$("#notes_submit").click(function(){
$(this).closest('form').submit(function(){
return false;
});
var frm = $(this).closest('form');
var data = $(frm).serialize();
if($(frm).valid()){
$.post(
"../php/add_notes_ajax.php",
data,
function(data){
$('#note_holder').load('load_notes.php?subcat=<? echo $subcat;?>');
}
);
}
});
});
</script>
<div style="float:left; margin-left:15px;">
<form name="messages1" class="form" id="myforma" method="post" action="#" enctype="multipart/form-data">
<fieldset style="width:500px; height:400px; overflow:auto; font-size:11px;">
<legend>Click to View Previous Notes / Messages</legend>
<div style="height:350px; overflow:auto;" class="note_holder" id="note_holder">
<!--This div is being called from the ajax script to load add_notes_ajax.php-->
</div>
</fieldset>
<div style="margin-top:20px;"></div>
</form>
</div>
<div style=" float:right;">
<form name="notes" class="notes" id="notes" method="post" action="#" enctype="multipart/form-data">
<fieldset style="width:300px; height:400px;">
<legend>Enter a Note</legend>
<div style="margin-top:00px;"></div>
<div>
<textarea rows="20" cols="20" style="height:300px; width:290px;" name="notes"></textarea>
<input type="submit" name="notes_submit" id="notes_submit" value="Submit Note" class="button" />
<input type="hidden" name="subcat" value= "<?php echo $subcat; ?>" />
</div>
</fieldset>
<div style="margin-top:20px;"></div>
</form>
</div>
添加注释 AJAX.PHP
<?php
include_once('../bootstrap.php');
include_once('../site_globals/common_functions.php');
include_once('../site_globals/common_queries.php');
include_once('../php/gump.class.php');
page_protect();
error_reporting(0);
$firstname = filter($_SESSION['user_name']);
$myid = filter($_SESSION['user_id']);
// All the variables from the submission form
$notes = filter($_POST['notes']);
$subcat = filter($_POST['subcat']);
//Insert Notes into the database
$stmt = $dbh->prepare('
INSERT INTO `notes`
(date , sub_cat_id , notes)
VALUES
(:date , :subcat , :notes )
');
$stmt->bindValue('subcat', $subcat);
$stmt->bindValue('date', date('Y-m-d H:i:s'));
$stmt->bindValue('notes', $notes);
$stmt->execute();
echo "This note was added successfully";
exit;
?>
。 加载 NOTES.PHP
<table width="100%">
<thead style="text-align:left; ">
<tr style="font-size:14px; font-weight:bold;">
<!-- <th><input class="check-all" type="checkbox" /></th>-->
<th>Date</th>
<th >Contents</th>
<th>Preview / Print</th>
</tr>
</thead>
<?php while ($messages_row = mysql_fetch_object($messages_res)):?>
<tr>
<td><a target="_blank" href="../site_hospital_files/thread.php?question_id=<?php echo $messages_row->question_id;?>"><?php echo substr($messages_row->reply, 0, 20) . '...';?></a></td>
<td><?php echo date('Y-m-d', strtotime($messages_row->date_added));?></td>
<td><a href="../site_hospital_files/pdf_messages_notes.php?msg_id=<?php echo $messages_row->question_id;?>&var1=<?php echo $subcat;?>">Create PDF</a></td>
</tr>
<?php endwhile;?>
<?php while($notes_row = $notes_res->fetch(PDO::FETCH_ASSOC)):?>
<tr>
<td><?php echo $notes_row[date]; ?></td>
<td><?php echo substr($notes_row[notes], 0, 50).'...';?></td>
<td><a href="pdf_messages_notes.php?note_id=<?php echo $notes_row->sub_cat_id; ?>&var1=<?php echo $subcat;?>">View</a></td>
</tr>
<?php endwhile;?>
</table>
【问题讨论】:
-
此时我不会太担心它。总有改进的方法,但你所做的还不错。很高兴看到您正在使用 PDO!
-
...但是为什么是随机的
mysql_fetch_object? -
而这个问题更适合codereview.stackexchange.com,这里就不多说了。
-
谢谢保罗。在走得太远之前,我想检查一下我是否走在正确的轨道上。 Deceze 我有人帮我用过所有东西。我正在将此代码移至 PDO 并移至普通数组。经过一些研究,我不相信出于任何特定原因我需要使用 fetch_object。似乎没有害处,只是没有必要。希望我没有错。
-
从来不知道 codereview 的存在。我会检查一下。感谢您的提醒。