大家好,我已经苦苦挣扎了一两个星期,试图了解整个 PDO、单身或非 MVC 等等。我现在已经编码了 3 个月,我想知道我是否最终做到了这一点。
我正在为班级成绩等构建一个简单的学校应用程序......并制作了一个名为 section 的课程,我现在已经将 PDO 纳入其中。到目前为止,它的工作和功能与 id 一样,但在我以同样的方式制作更多课程之前,我想知道是否有任何错误以及如何改进。尽管我写了其他所有内容,但我自己并没有正确的 DB 类。
class pdoDB {
/*** Declare instance ***/
private static $db = NULL;
/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
/*** maybe set the db name here later ***/
}
/**
*
* Return DB instance or create intitial connection
*
* @return object (PDO)
*
* @access public
*
*/
public static function getInstance() {
if (!self::$db)
{
self::$db = new PDO('mysql:host=localhost;dbname=attendance_ksu','root','root');;
self::$db-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$db;
}
/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}
} /*** end of class ***/
现在我的部分类。
class Section {
// if protected can't loop through on html page into table
// I don't want to make getter method for each attribute.
// all data will be coming from the db
public $id;
public $section_number;
public $subject;
public $classroom;
public $level_id;
public $shift;
public $course;
public $campus;
public $times;
public $academic_year;
public $start_date;
public $end_date;
public $max_st_number;
public $status;
// retrieve all sections in db
public static function get_all_sections() {
try {
// maybe a waste of time doing this bit?? not sure!! the " if(!isset($db)) "
if(!isset($db)) {
$db = pdoDB::getInstance();
/*** echo a message saying we have connected for testing reasons only ***/
echo 'Connected to database <br />';
}
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM sections";
$stmt = $db->query($sql);
$objs = $stmt->fetchALL(PDO::FETCH_CLASS, 'Section');
// 1. return the complete array of data so I can loop through it later
return $objs;
// 2. Or echo out a loop immediately where this method is called
/*
foreach ($objs as $section) {
echo "Section:" . $section->section_number . " | " .
"Subject: {$section->subject}" . "<br/>";
}
*/
$db = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
// Get the section details by id from db
// will be passed via the URL as $_GET
public static function get_section_by_id($id) {
try {
if(!isset($db)) {
$db = pdoDB::getInstance();
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
}
/*** prepare the SQL statement ***/
$stmt = $db->prepare("SELECT * FROM sections WHERE id = :id");
/*** bind the paramaters ***/
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
/*** execute the prepared statement ***/
$stmt->execute();
/*** fetch the results and give it a class ***/
$section = $stmt->fetchObject('section');
return $section;
$db = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
我打算只在页面上调用 get_all_sections() 方法并将结果循环到表中。其他页面也将使用此方法完成。如果不将我的学习曲线扩展到 MVC 等......这是一种可以接受的方法吗?我是否朝着正确的方向前进?
例如:
<?php
// some code
$sections = Section::get_all_sections();
// any other necessary code
?>
<html>
<body>
<table class="table table-striped table-bordered table-highlight" id="example">
<thead>
<tr>
<th>#</th>
<th>Section Number</th>
<th>Subject</th>
<th>Course</th>
<th>Students</th>
<th>Level</th>
<th>Campus</th>
<th>Classroom</th>
<th>Shift</th>
<th>Start date</th>
<th>End date</th>
<th>Teacher</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php
$key = 1;
foreach($sections as $section) {
?>
<tr>
<td><? echo $key++ ?></td>
<td><a href=""><?php echo $section->section_number ?></a> </td>
<td><?php echo $section->subject ?> </td>
<td><?php echo $section->course ?> </td>
<td><?php echo $section->no_students ?> sts</td>
<td><?php echo $section->level_id ?> </td>
<td><?php echo $section->campus ?> </td>
<td> <?php echo $section->classroom ?></td>
<td><?php echo $section->shift ?> </td>
<td><?php echo date("d-M-Y",strtotime($section->start_date ))?></td>
<td><?php echo date("d-M-Y",strtotime($section->end_date ))?></td>
<td>
<?php
if($section->username =="")
{
echo "<a href=\"add_section_teacher.php?id=".urlencode($section->id )."§ion_number=".urlencode($section->section_number )."\">Not assigned</a>";
} else {
echo "<a href=\"edit_section_teacher.php?ts_id=".urlencode($section->ts_id )."\">";
echo $section->username ;
echo "</a>";
}
?>
</td>
<td><a href="delete.php?id=<?php echo $section->id ; ?>"
onclick="return confirm('Are you sure you want to delete section <?php echo $section->section_number ; ?> ?');">Delete</a></td>
</tr>
<? }?></tbody>
</table>
提前感谢您的任何反馈和建议。我想正确地学习这一点,因此任何详细的反馈:)都会受到赞赏,因为这里的目标是学习。