0
votes

i'm a newbie for using codeigniter ,i'm learn for develop an application base on mysql database for my school project, i have 10table ( table1,table2,....table10), i'm create my model just like this

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 |
-----------------------
|      |       |      |
|      |       |      |
-----------------------

cant somebody help me, or explained how can i do that, or maybe some share a link for tutorial

2

2 Answers

0
votes

You can pass the table you want to show in your controller parameter. Then create a function to create your menu. You don't need a model if you just use $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>';
        } 
    }
}
0
votes

I am not 100% sure I am understanding the question right, but here goes;

I would use Routes for this, and it can all be done with a couple of functions. Open your routes.php file (./application/config/routes.php), and add this;

$route['show/(:any)'] = 'show/table/$1';

This will re-route any user that visits "/show/table1" to your "show/table/table1" function.

In your controller, you can remove all your "show_table#" functions, and replace them with this;

function table($table)
{
    if ( ! $table )
    {
        show_404();
    }

    $data['table'] = $this->show_model->get_table($table);
    $this->load->view('table_page', $data);
}

This is the function your route file is showing the user, it's pretty simple. First we're checking for a $table, if not found we show the 404. If we find one, we're going to the model.

Now, in your model, you can also remove all your table# functions, with this;

public function get_table($table)
{
    $query = $this->db->get($table);
    return ($query->num_rows() > 0) ? $query->result() : false;                    
}

That's it. You can now show links in your view to each table.

<ul>
    <li><a href="<?php echo site_url('show/table1'); ?>">Table 1</a></li>
    <li><a href="<?php echo site_url('show/table2'); ?>">Table 2</a></li>
    <li><a href="<?php echo site_url('show/table3'); ?>">Table 3</a></li>
    <li><a href="<?php echo site_url('show/table4'); ?>">Table 4</a></li>
    <li><a href="<?php echo site_url('show/table5'); ?>">Table 5</a></li>
    <li><a href="<?php echo site_url('show/table6'); ?>">Table 6</a></li>
    <li><a href="<?php echo site_url('show/table7'); ?>">Table 7</a></li>
    <li><a href="<?php echo site_url('show/table8'); ?>">Table 8</a></li>
    <li><a href="<?php echo site_url('show/table9'); ?>">Table 9</a></li>
    <li><a href="<?php echo site_url('show/table10'); ?>">Table 10</a></li>
</ul>