我有以下数据库和 PHP。我正在尝试在 CodeIgniter 中将其转换为 MVC,以下是我到目前为止所得到的,但它不起作用。如果有人能指出我做错了什么,我将不胜感激。
我的 SQL 和 PHP
数据库
CREATE TABLE seats
(
rowId varchar(1) not null,
columnId int not null,
status int,
updatedby varchar(10),
PRIMARY KEY (rowId,columnId)
);
CREATE TABLE userauth (
rowID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
commonname VARCHAR(35) NOT NULL,
username VARCHAR(8) NOT NULL,
pswd VARCHAR(32) NOT NULL,
PRIMARY KEY(rowID)
);
insert into seats values (‘A’, 1, 0, ‘’);
insert into seats values (‘A’, 2, 0, ‘’);
insert into seats values (‘B’, 1, 0, ‘’);
insert into seats values (‘B’, 2, 0, ‘’);
insert into seats values (‘C’, 1, 0, ‘’);
insert into seats values (‘C’, 2, 0, ‘’);
insert into seats values (‘D’, 1, 0, ‘’);
insert into seats values (‘D’, 2, 0, ‘’);
menu.php(原始 PHP 和工作)
<?php
$linkID = @ mysql_connect(“localhost”, “tickets”, “tickets”) or die(“Could not connect to MySQL server”);
@ mysql_select_db(“tickets”) or die(“Could not select database”);
$query = “SELECT * from seats order by rowId, columnId desc”;
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo “<table width=‘100%’ border=‘0’ cellpadding=‘3’ cellspacing=‘3’>”;
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
if ($rowId != ‘A’) {
echo “</tr></table></td>”;
echo “\n</tr>”;
}
$prevRowId = $rowId;
echo “\n<tr><td align=‘center’><table border=‘1’ cellpadding=‘8’ cellspacing=‘8’> <tr>”;
} else {
$tableRow = false;
}
if ($status == 0) {
$seatColor = “lightgreen”;
} else if ($status == 1 && $updatedby == ‘user1’) {
$seatColor = “FFCC99”;
} else if ($status == 1 && $updatedby == ‘user2’) {
$seatColor = “FFCCFF”;
} else if ($status == 2 && $updatedby == ‘user1’) {
$seatColor = “FF9999”;
} else if ($status == 2 && $updatedby == ‘user2’) {
$seatColor = “CC66FF”;
} else {
$seatColor = “red”;
}
echo “\n<td bgcolor=’$seatColor’ align=‘center’>”;
echo “$rowId$columnId”;
if ($status == 0 || ($status == 1 && $updatedby == $_SERVER[‘PHP_AUTH_USER’])) {
echo “<input type=‘checkbox’ name=‘seats[]’ value=’$rowId$columnId’></checkbox>”;
}
echo “</td>”;
if (($rowId == ‘A’ && $columnId == 7)
|| ($rowId == ‘B’ && $columnId == 9)
|| ($rowId == ‘C’ && $columnId == 9)
|| ($rowId == ‘D’ && $columnId == 10)
) {
// This fragment is for adding a blank cell which represent the “center aisle”
echo “<td> </td>”;
}
}
echo “</tr></table></td>”;
echo “</tr>”;
echo “</table>”;
mysql_close();
?>
由于我的声誉很低,我无法发布图片,所以我发布了这个原始 PHP 的输出/屏幕截图:http: //www.flickr.com/photos/95621694@N02/8743026263/in/photostream
到目前为止,我的 MVC 无法正常工作
CodeIgniter 模型
<?php
class Bus_model extends CI_Model {
public function __construct()
{
//$this->load->database();
}
function get() {
$this->load->database();
$query=$this->db->query(‘SELECT * from seats order by rowId, columnId desc’);
return $query->result();
}
}
>
控制器
<?php
class Bus_type extends Controller
{
function Bus_agent()
{
parent::Controller();
}
function Bus_fun()
{
$this->load->model(‘busmodel’);
$data[‘query’] = $this->busmodel->get();
$this -> load ->view(‘view’);
}
}
看法
<?PHP
$prevRowId = null;
$seatColor = null;
$tableRow = false;
echo “<table width=‘100%’ border=‘0’ cellpadding=‘3’ cellspacing=‘3’>”;
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
if ($rowId != ‘A’) {
echo “</tr></table></td>”;
echo “\n</tr>”;
}
$prevRowId = $rowId;
echo “\n<tr><td align=‘center’><table border=‘1’ cellpadding=‘8’ cellspacing=‘8’><tr>”;
} else {
$tableRow = false;
}
if ($status == 0) {
$seatColor = “lightgreen”;
} else if ($status == 1 && $updatedby == ‘user1’) {
$seatColor = “FFCC99”;
} else if ($status == 1 && $updatedby == ‘user2’) {
$seatColor = “FFCCFF”;
} else if ($status == 2 && $updatedby == ‘user1’) {
$seatColor = “FF9999”;
} else if ($status == 2 && $updatedby == ‘user2’) {
$seatColor = “CC66FF”;
} else {
$seatColor = “red”;
}
echo “\n<td bgcolor=’$seatColor’ align=‘center’>”;
echo “$rowId$columnId”;
if ($status == 0 || ($status == 1)) {
echo “<input type=‘checkbox’ name=‘seats[]’ value=’$rowId$columnId’></checkbox>”;
}
echo “</td>”;
if (($rowId == ‘A’ && $columnId == 7)
|| ($rowId == ‘B’ && $columnId == 9)
|| ($rowId == ‘C’ && $columnId == 9)
) {
}
}
echo “</tr></table></td>”;
echo “</tr>”;
echo “</table>”;
>
错误
Error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: views/view.php
Line Number: 6
—
A PHP Error was encountered
Severity: Warning
Message: mysql_fetch_row() expects parameter 1 to be resource, null given
Filename: views/view.php
Line Number: 6