Welcome Guest, Not a member yet? Register   Sign In
can not pass varibles to view
#1

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> 
Reply
#2

Should be for CI 2+ and up:

PHP Code:
class Hello extends CI_Controller 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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();"?
Reply
#4

(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.
Reply
#5

(This post was last modified: 02-22-2015, 02:26 PM by Muzikant.)

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.
Reply
#6

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!
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB