CodeIgniter Forums
Can i load view directly from database??? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can i load view directly from database??? (/showthread.php?tid=8652)

Pages: 1 2


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]mapo[/eluser]
Hi everybody.

From start i would like to say that Code Igniter is absolutely fantastic!!!

I have a questions regarding views:

1. How can i load view from database (memo field)? is it possible or i have to store it in to the file first? i would like to parse code igniter code written and stored in database.
2. Can i load views from different location or only views folder? or there is different command to load files?

Sorry, maybe this question for you are obvious but i would like to do it properly.

Thanks for your help
Mapo


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]oddman[/eluser]
Firstly can I please advise you to NOT store code in a database - no matter what you're doing this is not a good idea. Secondly, I'm not entirely sure what it is you're trying to do with #2. Could you elaborate a little? I might be able to help a bit more then as I don't quite understand your request, sorry Smile


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]mapo[/eluser]
Hi.
Thanks for your reply.

#1. Why? could you explain it deeper? If I want allow to change only for admin persons and there are templates. I gave them only do some things. So, is it possible??

#2. I would like to have views in different folder. eg. For tidy up folders. and have templates in one folder, totally different.

Thanks.


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]oddman[/eluser]
Ah okay, I see what you're doing now. If that's the case, have the views check to ensure that the logged in user is an admin - you can do this by setting for example, $data['admin'] = true; in your controller method. So for example, it might look like this:

Code:
class Blog extends Controller {
    function post()
    {
        if ('admin' == $this->session->userdata('user_level')) {
            $data['admin'] = true;
        }
        else {
            $data['admin'] = false;
        }
        
        $this->load->view('post', $data);
    }
}

then in the view, you'd do this:

Code:
<?php if (true == $admin) { ?>do this<?php } ?>

I understand what you mean about views - generally I try and keep my views to separate folders as well, defined by controller. Have you tried doing:

Code:
$this->load->view('folder/view')

? I've never actually tried, so don't know if it would work. Hope this helps! Smile


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]gtech[/eluser]
well if you want to store html in a database you have to ensure that no malicious scripts can be uploaded ... I know this forum for example will parse out the script tags so you can't execute javascript.

When you read the HTML out of the database you can store it in a variable and then echo the variable in the view. If you are storing php code then I would be very cautious as someone could insert code that could cause havoc on your server.

do you want dynamic content within your code stored in the database? If you do maybe you could use somthing like the parser class.. so rather than php tags being stored in your view you can use tags within curly braces, and then the parser will substitute the tags at runtime.

@oddman

yes you can load views like you suggest, and models and controllers. I think controllers only support one level of subdirectory.


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]got 2 doodle[/eluser]
I am using a combination of page specific views stored in a page specific directory and generic views (used by all pages)

In controller
Code:
function index()
    {
$page_name = 'media'; //$page_name is unique
    $this->load->model('pagedefault'); // i keep page specific data such as page title and keywords / description etc.
    $data = $this->pagedefault->get_default($page_name);  // get the page specific data
...snip
    $output .= $this->load->view('view_nav',"",true);  // this is a generic view
    $output .= $this->load->view("$page_name/ls_content","",true); // this is a page specific view
...snip
* Load the footer content */        
    $output .= $this->load->view('view_footer_open',"",true);
    $output .= $this->load->view('view_bottom_nav',"",true);
    $output .= $this->load->view('view_footer',"",true);
/* Done loading views */        
    $this->output->set_output($output); // this line outputs the view

In Model
Code:
function get_default($page_name) {

switch ($page_name) {
/******************************************************/
/*                     about us                 */
/******************************************************/
case 'about_us':
    $data = array('page_name'=>"$page_name",
    'title'=>'Tatamagouche Centre | About Us',
    'desc'=>'Tatamagouche Centre offers its hospitality to everyone, works for the justice of all people, and joyfully affirms and celebrates human diversity in sexual orientation, ethnicity, gender, ability, race, religion and age, regardless of economic situation.',
    'keywords'=>'Retreat, recharge',
    'base_url'=>$this->config->item('base_url')
    );
return($data);
break;
/******************************************************/
/*                     contact                */
/******************************************************/

Example generic view snippet loads a page specific banner graphic
Code:
<div id="BannerGraphic"><img name="BannerGraphic_img" src="&lt;?php echo"$base_url" ?&gt;assets/images/&lt;?php echo"$page_name" ?&gt;/BannerGraphic.gif" width="825" height="220" border="0">
</div>

Does any of this help you out. By working in a modular way like this I can quickly assemble page using generic and non generic pieces.

All of my graphics are stored in /site root/assets/images/page_name. All of the graphics have the same name but are stored in different folders.


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]got 2 doodle[/eluser]
I forgot to mention, but it might be obvious anyway you must pass the $data array to the view
Code:
/* Load the header and banner */
    $output = $this->load->view('view_header',$data,true);  // passing the $data array
    $output .= $this->load->view('view_banner',"",true);  // .= (appending view fragments) no need to pass data array for subsequent views
    /* open the main container */



Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]Jamie Rumbelow[/eluser]
I think that's a bit confusing, doodle. Let me strip it back.

What doodle's doing is using the $this->load->view() function to load the view into a string - by setting the third parameter to TRUE - and then setting the final output with $this->output->set_output().

So, to do something like you want to, this sort of code can be used:

Code:
//Load the header
$output .= $this->load->view("layout/header");

//load the sidebar
$output .= $this->load->view("layout/sidebar");

//Load the page content from the db
$output .= $this->db->getwhere("pages","page_id","1");

//Load the footer
$output .= this->load->view("layout/footer");

//Send it to the browser!
$this->output->set_output($output);

Hope this makes it easier!

Thanks,


Jamie


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]got 2 doodle[/eluser]
confusion is my middle name ;-)


Can i load view directly from database??? - El Forum - 05-26-2008

[eluser]Jamie Rumbelow[/eluser]
haha!