1

I am using CodeIgniter framework. I am sending an array to my view, but I could not get the array in my view.

This is my controller code:

public function edit($id)
{
    $record = $this->Start_model->get_entry($id);//receiving array from model 
    $this->load->view('edit',$record);//send array to my view
} 

This is my array on controller that I send:

Array
(
    [0] => Array
        ( [id] => 1 [name] => Hamza [age] => 20 [address] => audit and account [phone] => 03000000000  )
)

But when I call this array view I get this error:

Undefined variable: record

This is how I am getting my array in view:

<?php 
echo '<pre>';
print_r($record); 
echo '</pre>';
?>

Now I know I am sending an array to my view but I want to know If there is array in my view or not. I can get record through another method but I think it is not a good practice. So anyone can help me how I can detect if there is an array in my view?

4
  • My question is about detect array in my view. I want to know how array is coming to my view. as I can also debugging It. Commented Sep 15, 2014 at 13:56
  • 1
    Please see my latest edit Commented Sep 15, 2014 at 13:58
  • 2
    @TheRealHamza wait, are you referring to something like the global parent array that holds the data inside the view when its imported from controller? i dont think you can check since i think extract() has been used Commented Sep 15, 2014 at 14:05
  • 1
    @TheRealHamza check this entry $this->load->vars() part Commented Sep 15, 2014 at 14:06

2 Answers 2

3

In your controller, send a parent array instead:

public function edit($id)
{
    $data = array();
    $data['record'] = $this->Start_model->get_entry($id); // provided this is not empty
    $this->load->view('edit', $data);
}

Then in your view:

foreach($record[0] as $key => $value) {
    echo $value['id'];
    // the rest blah blah
}
Sign up to request clarification or add additional context in comments.

Comments

2

Codeigniter extracts the array passed to a view, creating variables based on the keys of the array. To work as you want, pass an array with a key or record and a value of your array:

public function edit($id)
{
    $data = array('record' => $this->Start_model->get_entry($id));
    $this->load->view('edit',$data);//send array to my view
} 

Then this will work in your view:

<?php 
echo '<pre>';
print_r($record); 
echo '</pre>';
?>

The way you are currently sending the data, it will be extracted into individual variables for each element in the array, however as your array is numerically indexed, and php variable name rules prevent numeric variable names, you cannot access the data.

7 Comments

Thanks @Steve but can you tell me how I can see the existing array in my view?
@TheRealHamza However, if you make this small change, you can access the array as you expected to - via the variable $record
Your answer is nearly right but I want to confirm I can't detect how many array exist in php?
@TheRealHamza Yes, i can confirm that with your current code, as shown in your question, you cannot access the data in $record inside your view, so you cannot count the number or records or do anything else with it.
@TheRealHamza Oops, real simply typo, please see edit (replaced , with =>)
|

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.