-2

我有以下数据库和 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 “&lt;table width=‘100%’ border=‘0’ cellpadding=‘3’ cellspacing=‘3’&gt;”;
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
  if ($rowId != ‘A’) {
    echo “&lt;/tr></table></td>”;
    echo “\n</tr>”;
  }
  $prevRowId = $rowId;
  echo “\n<tr><td align=‘center’&gt;<table border=‘1’ cellpadding=‘8’ cellspacing=‘8’&gt;   <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’&gt;”;
echo “$rowId$columnId”;
if ($status == 0 || ($status == 1 && $updatedby == $_SERVER[‘PHP_AUTH_USER’])) {
  echo “&lt;input type=‘checkbox’ name=‘seats[]’ value=’$rowId$columnId’&gt;</checkbox>”;
}
echo “&lt;/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 “&lt;td> </td>”;
  }
}
echo “&lt;/tr></table></td>”;
echo “&lt;/tr>”;
echo “&lt;/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 “&lt;table width=‘100%’ border=‘0’ cellpadding=‘3’ cellspacing=‘3’&gt;”;
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
  if ($prevRowId != $rowId) {
      if ($rowId != ‘A’) {
        echo “&lt;/tr></table></td>”;
        echo “\n</tr>”;
      }
      $prevRowId = $rowId;
      echo “\n<tr><td align=‘center’&gt;<table border=‘1’ cellpadding=‘8’ cellspacing=‘8’&gt;<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’&gt;”;
  echo “$rowId$columnId”;
  if ($status == 0 || ($status == 1)) {
      echo “&lt;input type=‘checkbox’ name=‘seats[]’ value=’$rowId$columnId’&gt;</checkbox>”;
  }
  echo “&lt;/td>”;
      if (($rowId == ‘A’ && $columnId == 7)
        || ($rowId == ‘B’ && $columnId == 9)
        || ($rowId == ‘C’ && $columnId == 9)
) {


      }
}
echo “&lt;/tr></table></td>”;
echo “&lt;/tr>”;
echo “&lt;/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
4

2 回答 2

2

模型:

<?php  
class Bus_model extends CI_Model {
  public function __construct()
  {
     $this->load->database();
  }

  function get() {
     return $this->db->query("SELECT * from seats order by rowId, columnId desc")
            ->result_array();
  }
}


控制器:

<?php
 class Bus_type extends Controller
 {
   public function __construct() {
        parent::__construct();
   }
   function bus_fun()
   {
       $this->load->model('busmodel');
       $data['query'] = $this->busmodel->get();
       $this->load->view('view',$data); //You should pass value to view...
   }
 }


看法:

   我有点害怕碰它,所以我只会告诉你要修改什么。
代替:

while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{

foreach($query as $key=>$result)
{
  $rowId = $result['rowId'];
  $status = $result['status'];
  $columnId = $result['columnId'];
  $updatedby = $result['updatedby'];

于 2013-05-16T10:50:31.047 回答
0

替换mysql_fetch_row($result)each($query)

于 2013-05-16T10:12:30.790 回答