Welcome Guest, Not a member yet? Register   Sign In
New to CI (be kind)
#1

[eluser]k2zs[/eluser]
I have a few questions...

I see reference in the forum where you can use
Code:
<?php echo base_url();?>
in your views but when ever I try using it I get:

Fatal error: Call to undefined function base_url() in .....

I see it defined in my config file, what am I doing wrong?

Also I am used to developing dynamic sites where all pages are drawn from a database. I have searched the forum and found a few posts regarding this but it still eludes me. How would I create a common navigational object and deliver pages based in links to specific entries in the database with one record being shown as the default page?
#2

[eluser]ShoKatoo[/eluser]
Simple...
Open ./system/application/config/autoload.php and scroll down to
Code:
$autoload['helper'] = array();
Now set this to
Code:
$autoload['helper'] = array('url');
This is quite self explanatory: will load the url helper that will give you access to the base_url() function.
As for the following question... I don't quite get what are you trying to say... please give some more info...

Have phun! Smile
#3

[eluser]k2zs[/eluser]
Perfect! Problem one solved

I have only heen at this CI stuff since yesterday morning (and I had to attend my first sons wedding yesterday :grrr: ) but I have been able to build quite a bit so far.

I've already re-arranged the site structure to get all of the CI stuff out of my web root, and built static header and footer files.
I connected to an exiating database and am drawing all of my pages from the "pages" table, but unfortunately they are all being displayed at once. I've been trying to understand models so that I can write my own sql statements but am lost. I am new to MVC and have always used flat file structure...

Now onto my other issue... How would I make a common navigational item that draws its items from a database?
#4

[eluser]boldyellow[/eluser]
[quote author="k2zs" date="1287963491"]
Now onto my other issue... How would I make a common navigational item that draws its items from a database?[/quote]

On a school site I did, the nav model goes to the table of the courses offered and grabs the title of each active course to display in the local navigation.

Code:
// Controller
// Asks the nav model to find what courses are active

function courses()
    {
        $data['title'] = "Courses";
        $data['nav_courses'] = $this->nav_model->get_nav_courses();        
        $this->load->view('course_view', $data);
    }

// Model
// DB query to get all active course titles to view in nav

    function get_nav_courses()
    {
        $this->db->order_by('course_name', 'asc');
        $this->db->where('active', 1);
        $query = $this->db->get('course_info');
        return $query->result();
    }
#5

[eluser]k2zs[/eluser]
I'm just not getting it. Can you explain the whole thing with actual file names? I have yet to be able to use a model. Where is the controller code calling the model? what is the actual model name? Here is what I did:

I have one controller called "main.php" and in it I have:

Code:
<?php
class Main
    extends Controller {

    function Main() {

        // load controller parent

        parent::Controller();

        // load helpers here

    }

    // load views sequentially

    function index() {
        
        // get the default page from the database
        $query = $this->db->query("SELECT * FROM pages WHERE (parentID =0 AND pgNavOrder =1 AND pgActive =1)");

        if ($query->num_rows() > 0)

        {
            $row = $query->row();

        // load 'header' view

        $this->load->view('header_view', array ( 'header' => $row->pgTitle ));
        
        
        //testing call to model
        $data['subNav_pages'] = $this->subNav_model->get_subNav_main();        
        $this->load->view('subnav_view', $data);

        // load 'main' view and pass in database content

        $this->load->view('main_view', array ( 'pageContent' => $row->pText ));

        } else {
            
        // load 'header' view

        $this->load->view('header_view', array ( 'header' => 'Page not found' ));

        // load 'content' view and pass in database content

        $this->load->view('main_view', array ( 'pageContent' => 'oops! the page you were looking for can not be found' ));
        
        }

        // load 'footer' view

        $this->load->view('footer_view', array ( 'footer' => 'created and maintained by <a href="http://www.tgcdesign.com" target="_blank">TGCDesign.com</a> | powered by <a href="http://www.codeigniter.com" target="_blank">CodeIgniter</a> ' ));
    }
    
    function page_lookup () {
        // get the default page from the database
        $this->db->where('pgName', $this->uri->segment(1));
        $query = $this->db->get('pages');
        
        if ($query->num_rows() > 0)
        {
        $row = $query->row();

        // load 'header' view

        $this->load->view('header_view', array ( 'header' => $row->pgTitle ));

        // load 'content' view and pass in database content

        $this->load->view('main_view', array ( 'pageContent' => $row->pText ));
        
        } else {

        // load 'header' view

        $this->load->view('header_view', array ( 'header' => 'Page not found' ));

        // load 'content' view and pass in database content

        $this->load->view('main_view', array ( 'pageContent' => 'oops! the page you were looking for can not be found' ));
        }

        // load 'footer' view

        $this->load->view('footer_view', array ( 'footer' => 'created by <a href="http://www.tgcdesign.com" target="_blank">TGCDesign.com</a> | powered by <a href="http://www.codeigniter.com" target="_blank">CodeIgniter</a> ' ));
    }
}

Then I have the model called "subNav_model.php" and in it I have:
Code:
&lt;?php
// Model
// DB query to get all active page titles to view in nav

function get_subNav_main()
    {
        $this->db->order_by('ParentID', 'asc');
        $this->db->order_by('pgNavOrder', 'asc');
        $this->db->where('pgActive', 1);
        $query = $this->db->get('pages');
        echo 'boo';
        return $query->result();
    }

I created the view to load the subNav and called it "subNav_view.php" and in it I have:
Code:
<div>
        &lt;?php foreach($subNav_pages->result() as $row):?&gt;
        &lt;?php echo $row->pgName;?&gt;
        &lt;?php endforeach;?&gt;
    </div>

When I try running it I get

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: Main::$subNav_model

Filename: controllers/main.php

Line Number: 13
#6

[eluser]boldyellow[/eluser]
Hmmm, yeah, alot going on there.

I just started with CI a few months ago. I'm not an expert and I'm not a programmer, just a designer. So knowing that, I will proceed to dispense free advice:

The MVC concept is all about making your code modular. And it is literally at the core of CI.

MVC means you'll likely want to avoid db queries in your Controller. Likewise, not sure you'd want an echo statement in your Model, that's more of a View thing.

The CI documentation is great, but can lack specific examples for a raw beginner (like me). But it's a good jumping off point... from there, Google is my best friend.

Start with simple code. Just get the basic concepts using a Controller, a Model and a single View. No nav or such, just simple dumb stuff. I started just using a database about alien sightings (inspired by a PHP book I read). Then build on that and add more functionality in your Controller and Views.

'bout the best I can do at this hour... hope this helps you keep going with CI.
#7

[eluser]Bart Mebane[/eluser]
@k2zs: You need to load your model before calling it.
Code:
$this->load->model('subNav_model');
You can do this either in your controller constructor (if you're using the model in all or most of the controller functions), or inside a specific controller function. I don't know if the model code you posted was the entire file, but a model, unlike a view or a helper, needs to be defined as a class.
#8

[eluser]k2zs[/eluser]
Wow!!!

I think I'm getting it...

My new controller:

Code:
&lt;?php
class Home
    extends Controller {

    function Home() {
        // load controller parent
        parent::Controller();
        
        // load helpers here
    }

    // load views sequentially

    function index() {
        
        //Model test
        $this->load->model('main_model');
        $data['mainNav'] = $this->main_model->getMainNav();
        $data['subNav'] = $this->main_model->getSubNav();
        $data['page'] = $this->main_model->getPage();
        $this->load->view('template_view', $data);
        
    }
}

My new model:

Code:
&lt;?php
// Model
// DB query to get all active page titles to view in nav

class Main_model extends Model {
    

    function getMainNav() {
        
        $this->db->order_by('ParentID', 'asc');
        $this->db->order_by('pgNavOrder', 'asc');
        $this->db->where('pgActive', 1);
        $this->db->where('ParentID', 0);
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
    
    function getSubNav()
    {
        $this->db->order_by('ParentID', 'asc');
        $this->db->order_by('pgNavOrder', 'asc');
        $this->db->where('pgActive', 1);
        $this->db->where('ParentID !=', 0);
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
    
    function getPage() {
        if ($this->uri->total_segments() <> 0) {
            $this->db->where('pgName', $this->uri->segment(1));        
        } else {
            $this->db->where('ParentID', 0);
            $this->db->where('pgNavOrder', 1);
        }
        $q = $this->db->get('pages');
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        return $data;
        }
    }
}

The if in the "getPage" method seems a bit messy, I'm sure there are probably better ways of doing it but it works for now. Now I just need to figure out how to pass the pages parentID from "getPage" to "getSubNav" in the model...


This stuff is GREAT!!




Theme © iAndrew 2016 - Forum software by © MyBB