我有一个包含多个表的体育赛事的数据库。一些表保存结果的记录,有一个用户配置文件表,以及一些杂项表。
我的模型如下:
class Results extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function displayrecords($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
public function getdisplayname($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
public function updatetable() {
}
public function deleterecords($table, $id) {
$this->db->query("delete * from " . $table . " where id='" . $id . "'");
}
}
Run Code Online (Sandbox Code Playgroud)
以及索引视图的控制器:
public function index() {
// Load the models
$this->load->model('Results');
$this->load->model('Info');
$this->load->model('TableNames');
// Create the array to store the data for the index page
$data['results'] = array();
// Get a list of all the tables in the event database
$tables = $this->db->list_tables();
$data['tables'] = array();
// Loop through the list of table names and extract the results tables
foreach($tables as $table) {
if (strpos($table, "rs_") === 0) {
array_push($data['tables'], $table);
$temp = $this->Results->displayrecords($table);
array_push($data['results'], $temp);
$data['headnames'] = $this->db->list_fields($table);
}
}
// Get the event info data and the list of Human Readable Table Names
$data['tablenames'] = $this->TableNames->getnames();
$data['eventinfo'] = $this->Info->getinfo();
// Render the templates
$this->parser->parse('templates/index_header', $data);
$this->parser->parse('pages/index', $data);
$this->parser->parse('templates/footer', $data);
}
Run Code Online (Sandbox Code Playgroud)
我想弄清楚的是,是否建议使用单个 CodeIgniter 模型以及所有结果表。结果表的示例:21KM 赛事的前十名男性 30 至 39 岁,以及 21KM 赛事的 40 至 49 岁男性前十名。我目前拥有使用相同模型方法的所有结果表,例如:
public function displayrecords($table) {
$query=$this->db->query("select * from " . $table);
return $query->result();
}
Run Code Online (Sandbox Code Playgroud)
它以结果表为参数,并显示结果。我会为每个结果表创建模型,但我事先不知道该表将具有什么名称,它将具有什么列等。每个结果表都是由管理员通过网页创建的,并根据需要添加到数据库中. 每个表都会对它们执行相同的操作,删除行,更新行。这就是为什么我认为最好有一个可以处理所有表的模型。
我不确定我的处理方式是否正确,因为我似乎已经遇到了一些问题。例如,当尝试从表中删除记录时,我可以获得需要删除的行的 id,但我不确定如何获取数据库表名,因为 deleterecords 方法需要 2 个参数,$id , 和 $tablename 以正确删除记录。模型方法是这样的:
public function deleterecords($tablename, $id) {
$this->db->query("delete from " . $tablename . " where id='" . $id . "'");
}
Run Code Online (Sandbox Code Playgroud)
然后在视图中我显示每个结果表:
foreach ($table as $row) {
echo "<tr id='result_" . $row->id . "'>";
echo "<td>" . $row->Place . "</td>";
echo "<td class='bibcell'>" . $row->BibNumber . "<a class='editbut' href=''><img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'></a><a class='delbut' href='deletedata?id=" . $row->id . "'><img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'></a></td>";
echo "<td>" . $row->Name . "</td>";
echo "<td>" . $row->Age . "</td>";
echo "<td>" . $row->Club . "</td>";
echo "<td>" . $row->Time . "</td>";
echo "<td><select class='user-options'><option value='pending' style='background: red;'>Pending</option><option value='checked' style='background: green;'>Checked</option></select></td>";
echo "</tr>";
}
Run Code Online (Sandbox Code Playgroud)
使用隐藏输入是获取表名的方式吗? CodeIgniter 中可能有哪些功能可以使用单个模型处理多个表?
编辑:
我设法弄清楚了这一点:
echo "<td class='bibcell'>" . $row->BibNumber . "<a class='editbut' href=''><img class='bibnum' src='" . base_url() . "assets/images/editicon.png' width='80' style='padding-left: 20px;'></a><a class='delbut' href='deletedata?id=" . $row->id . "&tablename=" . $tables[$i] . "'><img src='" . base_url() . "assets/images/deleteicon.png' width='75' style='padding-left: 5px;'></a></td>";
Run Code Online (Sandbox Code Playgroud)
将 id 和 tablename 传递给要删除的 Model 方法,但我得到了未定义的变量 tablename。我可以看到 tablename 变量在 url 中。
http://localhost/results/pages/deletedata?id=1&tablename=rs_toptenfemale
Run Code Online (Sandbox Code Playgroud)
小智 0
我不确定你的情况,但你可以在模型中使用多个函数,并且可以在单个模型中处理所有表(但我认为这是不可取的,也是一个好的做法)
您可以使用这样的动态函数来避免模型中函数过多
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common_model extends CI_Model {
public function insertContent($data,$table){
$this->db->insert($table,$data);
if($this->db->affected_rows() > 0)
{
return TRUE;
}
else{
return FALSE;
}
}
public function updateContent($data,$table,$whereField,$value){
$this->db->where($whereField,$value);
$update=$this->db->update($table, $data);
if ($update > 0) {
return TRUE;
} else {
return FALSE;
}
}
public function login_check($data,$table)
{
$this->db->select('*');
$this->db->where($data);
$query=$this->db->get($table);
if($query->num_rows()>0)
{
return TRUE;
} else {
return FALSE;
}
}
public function retriveTableDetails($table)
{
$this->db->select('*');
$this->db->from($table);
$query=$this->db->get();
if($query->num_rows()>0)
{
return $query->result();
} else {
return FALSE;
}
}
public function retriveTableDetailsById($table,$id)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where('id',$id);
$query=$this->db->get();
if($query->num_rows()>0)
{
return $query->result();
} else {
return FALSE;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1815 次 |
| 最近记录: |