1

I am a beginner to codeIgniter and have looked for an answer to this on the CI forums and google, and even here...

OK I have searched for this and every time I find answers to this pertaining to use a foreach() to get the data out of the array then do something with it in the foreach loop; this is not what i wish to do.

I am building a site whereby I would like to store site information, such as site title, description, abstract, keywords etc in a database table with just one row (rather than having to go to the html and do it there..)

So far I have something like this in my model:

    function getAll(){
    $q = $this->db->get('system');

    if($q->num_rows() > 0){
            return $q->row();
    }
}

in my controller I have:

function index(){
    $this->load->model("system_model");
    $data[] = $this->system_model->getAll();    
    $this->load->view('home', $data);
}

and in my view I wish to have very simply (html tags are descriptive only):

   <title><?php echo $this->title; ?></title>
<description><?php echo $this->description; ?></description>

As you can see a foreach loop would not work in this instance and I don't believe that the best way to do this is loop through the array in the controller and then pass each individual array part into the view as a separate variable..

Is this at all possible?

EDIT

I have given the tick to the first answer as that put me on the right track to find the solution (although it might not be 100% correct its working) in order to get this to work I followed answer number 1, but then in the view I did the following:

<title><?php echo$system[0]->title; ?></title>
2
  • Sounds like an unnecessary db call. I hope you're at least planning on caching this. Commented Jun 23, 2011 at 17:07
  • i will be caching it when i've learnt how to do it, for now im just learning Commented Jun 23, 2011 at 18:05

2 Answers 2

2

Give the $data[] array a key name

function index(){
    $this->load->model("system_model");
    $data['mydata'] = $this->system_model->getAll();    
    $this->load->view('home', $data);
}

and in the view file you can echo or loop or whatever based on that key

echo $mydata

or

foreach($mydata as $md){
echo $md
}

The best way to query the database in the model is to do the following:

function get_user_by_id($id){
    $this->db->where('id', $id);
    $query = $this->db->get('users');
    $result = $query->result_array();
    return $result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

if you want to get al the data, remove the following: $this->db->where('id,$id);
Depends if he wants to use the object-oriented method to retrieve data or not. He seems to prefer this notation as shown in his view example. If you do OPer, use result(); as this will return an object which can also be accessed using array notation.
thanks for the reply, if i use this method and then do code<?php echo $system; ?>code i just get array and if i try $system->title; i get an error..
I don't understand where you're getting $system from or have you renamed $data to that? If you're using @tylerpennys code then you're returning the result as an array which means you need to access data like $system['title']
yes sorry, i renamed it to $system. thanks for the comment, i will test this later
0

Here you go:

In your controller use:

function index(){
    $this->load->model("system_model");
    $data = $this->system_model->getAll(); 

    $this->load->view('home', $data);
}

Your view will then look like this:

   <title><?php echo $title; ?></title>
<description><?php echo $description; ?></decription>

Also, with your database query (in general) use result() as it sounds like you want more than one row:

   function getAll(){
    $q = $this->db->get('system');

    if($q->num_rows() > 0){
            return $q->result();
    }

3 Comments

hi thanks for the reply, i still get an error using the above code.. undefined variable title..
further to this if i var_dump the $data variable i get the following:array 0 => object(stdClass)[18] public 'id' => string '1' (length=1) public 'title' => string 'EVECTK' (length=6) public 'decription' => string 'EVECTK is the Corporation Tool for EVE Online Corporations. It provides unparalleled functionality; and best of all - its free! ' (length=129) public 'keywords' => string 'some keywords' (length=54) ' (length=128)
In your original code you are using "$data[] =" you need to just use "$data =" in your controller as you are adding an extra level to the array for no reason. Use this along with my other edits and it should work fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.