CodeIgniter Forums
can not pass varibles to view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: can not pass varibles to view (/thread-1245.html)



can not pass varibles to view - munna - 02-22-2015

the following s my controller code.I new to codeigniter..have no idea...but trying to learn...so my code is below.. can any one please help me find the bug?
PHP Code:
<?php
class Hello extends Controller
{
    var 
$name;
    var 
$color;
    
        
        function 
Hello()
        {
        
            
parent::Controller();
            
$this->name='test';
            
$this->color='red';
        }
     
 
        
function view()
        {
            
$data['name']=$this->name;
            
$data['color']=$this->color;
            
$this->load->view('view',$data);
        }
        
}

?>


my view is

PHP Code:
Hai  <font color="<?= $color ?>" <? $name ?> </font> 



RE: can not pass varibles to view - InsiteFX - 02-22-2015

Should be for CI 2+ and up:

PHP Code:
class Hello extends CI_Controller 



RE: can not pass varibles to view - Avenirer - 02-22-2015

What is "var"? This is not javascript. This is php. You should at least know how to define variables in php. And what is this: "parent::Controller();"?


RE: can not pass varibles to view - Muzikant - 02-22-2015

(02-22-2015, 01:45 PM)Avenirer Wrote: What is "var"? This is not javascript. This is php. You should at least know how to define variables in php. And what is this: "parent::Controller();"?

I think, he is using PHP 4 syntax.


RE: can not pass varibles to view - Muzikant - 02-22-2015

Munna, try this:

Controller:
PHP Code:
<?php
class Hello extends CI_Controller
{
 public 
$name;
 public 
$color;
 
 public function 
__construct()
 {
 
parent::__construct();
 
$this->name='test';
 
$this->color='red';
 }

 public function 
view()
 {
 
$data['name']=$this->name;
 
$data['color']=$this->color;
 
$this->load->view('view',$data);
 }



View:
PHP Code:
Hai <span style="color: <?=$color?>;"><?=$name?></span>. 

I think, this is what you want. But I recommend you to learn actual HTML/CSS standards and OOP in PHP 5.4+ at first. Good luck.


RE: can not pass varibles to view - dbui - 02-22-2015

Hi there

Change your controller as

class Hello extends CI_Controller{
protected $name=''; //or can be public / private depending on your scope
protected $color='';
public __construct(){
parent::__construct();
$this->name='test';
$this->color='red';
}
function view()
{
$data['name']=$this->name;
$data['color']=$this->color;
$this->load->view('view',$data);
}
}//end hello class
//then in your view
change it to:
Hai <font color="<?= $color ?>"> <?= $name ?> </font><!--Note the equal sign in front of name is needed -->

OR
Hai <font color="<?php echo $color ?>"><?php echo $name ?></font>

This should work!


RE: can not pass varibles to view - dbui - 02-22-2015

Sorry about my post, I didnt read that Muzikant already had the fix!