Undermine2k
Undermine2k

Reputation: 1491

retrieving data in view from controller

So im running a query and trying to return a result set back into my view however when I do <?php foreach ($form_records as $form_record) : ?><?php echo $form_record['fname']; ?> <?php endforeach; ?> I get Undefined variable: form_records. What am i doing wrong when I run the query (Select fname, lname from form_manager where username = "tester") I get a row returned successfully so the query is working.

Model

 function retrieve_form_record($username) {
    $this->db->select('fname,lname');
    $this->db->from('form_manager');
    $this->db->where('username', $username); 

    $query = $this->db->get();

        if ($query->num_rows() > 0)
        {
            $form_records= $query->result_array();
              return $form_records;
        }

            else {

            return false;
            }

      }
    }

Controller

function load_saved_forms() {
      $username = $this->tank_auth->get_username();
      $form_records = $this->Form_model->retrieve_form_record($username);
      $this->load->view('welcome',$form_records);
                            }   

Upvotes: 0

Views: 54

Answers (2)

egig
egig

Reputation: 4430

The problem is how you pass parameter to view, change this line:

   $this->load->view('welcome',$form_records);

to :

   $data['form_records'] = $form_records; //<<--pass data to view right way.
   $this->load->view('welcome',$data);

and the then you can perform this inside view:

<?php foreach ($form_records as $form_record) : ?>
   <?php echo $form_record['fname']; ?>
<?php endforeach; ?>

Note that $form_records in view is an index of array $datayou passed at $this->load->view('welcome',$data);

Upvotes: 2

gar
gar

Reputation: 168

The $form_records is probably an array and looking at the codeigniter's doc it will turn those into array elements when it is passed to the view.

See this link: http://ellislab.com/codeigniter/user-guide/general/views.html

But it is saying if you did this in your controller:

$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$this->load->view('blogview', $data);

You can use that "todo_list" like this in your view:

<?php foreach ($todo_list as $item):?>

<li><?php echo $item;?></li>

<?php endforeach;?>

Upvotes: 1

Related Questions