如何将此PHP文件翻译为模型文件?我是CodeIgniter的新手。
这是我的代码:
<?php
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$mysqli=mysqli_connect('192.168.128.30','root','root','root') or die("Database Error");
$sql="SELECT * FROM items WHERE itemcode LIKE '%$my_data%' ORDER BY itemcode";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error());
if($result)
{
while($row=mysqli_fetch_array($result))
{
echo $row['itemcode']."\n";
}
}
?>
答案 0 :(得分:1)
如果你的表名是项目,那么你可以创建一个名为mitems.php的php文件。 有一个模型文件的例子......
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MItems extends CI_Model
{
public $table;
public function __construct()
{
parent::__construct();
$this->table = "items";
$this->load->database();
}
function select($conditions=NULL, $tablename="", $limit=500, $offset=0)
{
if($tablename=="")
{
$tablename = $this->table;
}
if($conditions != NULL)
$this->db->where($conditions);
$query = $this->db->get($tablename, $limit, $offset=0);
return $query->result();
}
}
您可以点击此链接http://thephpcode.com/blog/codeigniter/a-smart-codeigniter-model
答案 1 :(得分:0)
仅 CI模式?这是你的:
public function select_data($my_data){
$this->db->like('itemcode', $my_data);
$this->db->order_by('itemcode');
return $this->db->get('items');
}
答案 2 :(得分:0)
一些基本想法:
Search
;