gpt4 book ai didi

mysql - codeigniter 中的链接表

转载 作者:行者123 更新时间:2023-11-30 00:07:40 33 4
gpt4 key购买 nike

我是使用codeigniter的新手,我正在学习为我的学校项目开发基于mysql数据库的应用程序,我有10个表(table1,table2,....table10),我正在创建我的模型就像这样

class Show_model extends CI_Model{    function __construct()    {        parent ::__construct();    }    function table1()    {        $query1 = $this->db->get('table1');        return $query1->result();            }    function table2()    {        $query2 = $this->db->get('table2');        return $query2->result();            }    bla....bla......................    function table10()    {        $query10 = $this->db->get('table10');        return $query10->result();            }}

and my controller just like this

class Show extends CI_Controller{    function __construct(){        $this->load->model('show_model');    }    function show_table1()    {        $data['show_table1']   = $this->show_model->table1();                $this->load->view('v_page_1',$data);    }    function show_table2()    {        $data['show_table2']   = $this->show_model->table2();                $this->load->view('v_page_2',$data);    }    bla....bla....}

and my goal is :

  1. how to simply my code

  2. how to show / view on one page and create a link for each table to show

this is i want to show:

clik to show : table1|tabel2|table3|......|

-----------------------
|itemA | itemB |itemC |
-----------------------
| | | |
| | | |
-----------------------

有人可以帮助我,或者解释一下我该怎么做,或者也许有人分享教程的链接

最佳答案

您可以传递要在 Controller 参数中显示的表格。然后创建一个函数来创建菜单。如果您只使用 $this->db->get('');,则不需要模型。

class Show extends CI_Controller
{
private $tables = array('table1', 'table2', 'table3' ....);

public function show_table($table)
{
if (! in_array($table, $this->tables))
{
show_404();
}

$data['table'] = $this->db->get($table)->result();
$menu = $this->get_menu();

$this->load->view('show_table', array(
'table' => $this->load->view('v_table_'. $table, $data, TRUE),
'menu' => $menu,
}

private function get_menu()
{
$menu = '';
foreach ($this->tables as $table)
{
$menu .= '<a href="'. base_url('show/show_table/'. $table) .'">'. $table .'</a>';
}
}
}

关于mysql - codeigniter 中的链接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24381131/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com