Michał Lipa
Michał Lipa

Reputation: 978

Undefined variable in my Codeigniter View

Having trouble with displaying my data from controller in View.

Searched for this, tried few things and still not working for me.

My controller:

public function microBitcoin()
{
    $arr = array('hashtag' => 'myhashtag', 'tweet_id' => '673899616799191040');
    $data = array();
    $data['liczba_tweetow'] = $this->load->library('Twetterclass', $arr);

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

My View:

 <?php print_r($data); ?>

 <?php echo $data['$liczba_tweetow']; ?>
 //tried with $data->liczba_tweetow;

I still get the same error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/micro_btc.php

How I can display this variable in my View?

Upvotes: 0

Views: 725

Answers (4)

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

please follow this,in controller

public function microBitcoin()
{
$data['first'] = 'this is first';
$data['second'] = 'this is second';
$this->load->view('micro_btc', $data);
}

In view ,you can catch as like

echo $first;
//it will print this is first
echo $second;
//it will print this is second

Upvotes: 0

Pawan Kumar Sharma
Pawan Kumar Sharma

Reputation: 221

your view should be look like this

<?php
 print_r($liczba_tweetow); 
?>

instead

<?php print_r($data); ?>

 <?php echo $data['$liczba_tweetow']; ?>
 //tried with $data->liczba_tweetow;**strong text**

when we load a view with the data the it would we converted in variable in respective key so you would get "$liczba_tweetow" instead $data['$liczba_tweetow']

Upvotes: 0

AkshayP
AkshayP

Reputation: 2169

 <?php echo $liczba_tweetow; 
       print_r($liczba_tweetow);
  ?>

This is proper way to get data in view.

Reference

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

You have to use like this,

print_r($liczba_tweetow);

see this for more information

Upvotes: 2

Related Questions