8

I'm trying to print my key/value pairs of data in my CodeIgniter view. However, I'm getting the following error. What I'm I doing wrong?

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/search_page2.php

Line Number: 8

application/controller/search.php

// ...
        $this->load->library('/twitter/TwitterAPIExchange', $settings);
        $url = 'https://api.twitter.com/1.1/followers/ids.json';
        $getfield = '?username=johndoe';
        $requestMethod = 'GET';     
        $twitter = new TwitterAPIExchange($settings);

        $data['url'] = $url;
        $data['getfield'] = $getfield;
        $data['requestMethod'] = $requestMethod;        

        $this->load->view('search_page2', $data);
// ...

application/views/search_page2.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Twitter Test</title> 
</head>
<body>
<?php print_r($data); ?>

<?php foreach ($data as $key => $value): ?>
    <h2><?php echo $key . ' ' . $value . '<br />'; ?></h2>
<?php endforeach ?>
</body>
</html>

2 Answers 2

7

to get the data array accessible in the view do like

    $data['url'] = $url;
    $data['getfield'] = $getfield;
    $data['requestMethod'] = $requestMethod;        
    
    $data['data'] = $data;
    $this->load->view('search_page2', $data);

else only the variables with names as its keys will be available in the view not the data variable we pass.

update:

this is in response to your comment to juan's answer Actually if you are trying make it working in the other way proposed.

controller code will be having no change from the code you posted.

    $data['url'] = $url;
    $data['getfield'] = $getfield;
    $data['requestMethod'] = $requestMethod;
    $this->load->view('search_page2', $data);

but in the view code you will need to just do.

 <h2>url <?PHP echo $url; ?><br /></h2>
 <h2>getfield <?PHP echo $getfield; ?><br /></h2>
 <h2>requestMethod <?PHP echo $requestMethod; ?><br /></h2>

instead of the foreach loop as your keys in $data are already available as respective named variables inside the view.

Sign up to request clarification or add additional context in comments.

4 Comments

Why would you do this? I'd much prefer to keep the container and the template data itself separate
+1 Thank you for your reply. This works. I'm new to CodeIgniter and may not know the best practices but is there something wrong with this solution Juan?
@JuanMendes : it was just a quick fix i proposed brother. the other way around is pretty much simpler.
@Anthony it's not a real problem, it just feel wrong to use the container as template data.
5

The variables to use in your template are

 $url, $getfield, $requestMethod

$data is the container for the variables that are passed to the view and not accessible directly

If you do need $data accessible to the view, use a different wrapper object

 $container  = array();
 $container  ['url'] = $url;
 $container  ['getfield'] = $getfield;
 $container  ['requestMethod'] = $requestMethod;
 $container  ['data'] = $data;
 $this->load->view('search_page2', $container);

3 Comments

How else can I pass multiple variables to the view? The following code does not work: $this->load->view('search_page2', $url, $getfield, $requestMethod);
@Anthony You pass multiple variables just as you did with $data, all the keys you add into $data will be passed to the template as variables
@Anthony : i have put a sample code of this also in my answer if you need to look into.

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.