Codeigniter reusable sections

codeigniter web application
codeigniter project

I have a table of data from a database that I want to display on various pages of my website. Ideally, i would like to just have an include or something that will go off and get the data, and return the html table. The html and data will be identical everytime I need to use this.

I wanted to know the best way of doing this

Thanks

EDIT

If this helps, something similar to a Django "inclusion" custom tag...for any django developers reading

 you need to pass the variable $data to the view method.

This is your code:

function load_my_view(){  
    $this->load->model('my_table');
    $data['my_results'] = $this->my_table->my_data();
    $this->load->view('my_view');
}

Please change it to this in order to load the $data into the view:

function load_my_view(){  
    $this->load->model('my_table');
    $data['my_results'] = $this->my_table->my_data();
    $this->load->view('my_view',$data);
}

reusable code / sections / blocks, reusable code / sections / blocks. El Forum Guest. #1. 01-23-2011, 02:29 PM. [​eluser]Future Webs[/eluser] Ive been reading the forums allot again lately and  CodeIgniter 3 has a 2MB download, including the user guide. CodeIgniter 4 is a 1.2MB download, plus 6MB for the user guide. Much of the CodeIgniter configuration is done by convention, for instance putting models in a "models" folder. There are still a number of configuration options available

You should use a function in a model to fetch the data you need. Your controller calls the model function and sends the returned information to a view. You don't need to use traditional php includes with Codeigniter. I recommend a review of the user guide. It's very good and will tell you all the basic stuff you need to know to develop with CI. But to get you started, you watn to use Models, Views, and Controllers. Your url will tell CI what controller and function inside that controller to run. If your url is

http://www.example.com/my_controller/load_my_view

Then CI will do what is inside the load_my_view function in the my_controller controller. function load_my_view in turn instantiates a model "my_table" and runs a database query, returns information that the controller sends to the view. A basic example follows:

Your model

class my_table extends CI_Model{
    function my_data(){
        $this->db->select('column_1,column_2,column_3');
        $this->db->from('my_table');
        $query = $this->db->get();
        if($query->num_rows()>0){
          $result = $query->result();
        }
        else{
          $result = false;
        }
        return $result;
    }


}

Your controller

class my_controller extends CI_Controller{
    function load_my_view(){  
        $this->load->model('my_table');
        $data['my_results'] = $this->my_table->my_data();
        $this->load->view('my_view');
    }

}

Your View

<ul id = "my_db_results">

    <?php foreach($my_results as $result):?>
         <li><?php echo $result->column_1." : ".$result->column_2." ( ".$result->column_3." )";?></li>
    <?php endforeach;?>
</ul>

Best way to project, plan and structure an app with CI, I then determine the default screens and how I can make useful reusable view parts (i.e. header and footer views). B. I then determine the  CodeIgniter supports a form of code modularization to help you create reusable code. Modules are typically centered around a specific subject, and can be thought of as mini-applications within your larger application.

It looks like a good oportunity to use cache: http://codeigniter.com/user_guide/libraries/caching.html

Query Builder Class, While not “true” caching, Query Builder enables you to save (or “cache”) certain parts of your queries for reuse at a later point in your script's execution. Normally​  The CodeIgniter installation assumes that you have set up an appropriate database, as outlined in the requirements. In this tutorial, we provide SQL code for a MySQL database, and we also assume that you have a suitable client for issuing database commands (mysql, MySQL Workbench, or phpMyAdmin).

Ok, so here is one thing that worked for me, but its definitely not perfect.

I created a view in which made a call to the model getting the data then put the data into a table. This way, I only have to include this view to put the table anywhere.

I understand this completely ruins the point of having a MVC framework, but hopefully it demonstrates what I want to do...and it works

Session Library, storage drivers, that you can see in the last section of the table of contents: so if you intend to reuse that same key in the same request, you'd want to use  Tutorial¶. This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in step-by-step fashion.

Making our site more scalable and our code more reusable , Long and short PHP syntax and other CodeIgniter style guidelines · Nesting views This way we can reuse code from one site on another very easily. Libraries can be very We can reuse code easily from one project for another, creating our own CI libraries, helpers, and so on. Free trial Previous Section Next Section  Build Your First Application¶. Build Your First Application. Static pages; News section; Overview & General Topics¶

Creating a Robust Web Application with PHP and CodeIgniter, They can be application-specific or generic, which you can reuse from application Remember that any CodeIgniter page has at least two components to it: the  Module A then calls Modules B to M for loading various (reusable) sections of the page (the form) Modules B to M may initiate connections to databases if needed (queries are not overly complex, though have a few cross-database joins)

zia/skeleton: This is a basic codeigniter reusable app for my , This is a basic codeigniter reusable app for my projects. - zia/skeleton. Installation. Please see the installation section of the CodeIgniter User Guide. Optionally, you can have CodeIgniter load environment-specific configuration files. This may be useful for managing things like differing API keys across multiple environments. This is described in more detail in the Handling Different Environments section of the Working with Configuration Files documentation.

Comments
  • You could create a view containing that html which you could include in other views.(if you are certain that the data will be the same every time, you can create the html before putting the site live and just include the view)
  • The data itself may not be the same, but the format for the data will be, no matter which page it is put on
  • thanks for th reply. I do understand the mvc, my point was that I have a block of data (in my case, just an ordered table) that I am wanting to use in several places. So instead of having to do all that stuff and load it into the view, if there was something I could do to just say "get that table and put it here"...if that makes sense
  • I guess caching might be your answer then, as bensiu suggests, unless you are up to creating an image file on the fly and inserting that wherever you need the table after the initial get request.
  • Would caching update the table every time though? Sure there is something you can do to make a reusable section of code/data...
  • You could make the html table you describe into a string and store it in a special db table as that string. If your table is as static as you say, this would only mean one db call per page load.
  • You could have rendered the view as a child and passed the parameters which included you data to that child. this way no direct call to DB will be made via view.