-2

次のDBと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 の出力/スクリーンショットを投稿します

これまでのところ私の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 に答える