CodeIgniter Forums
Variable not being passed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Variable not being passed (/showthread.php?tid=60003)



Variable not being passed - El Forum - 12-26-2013

[eluser]Booster77[/eluser]
Hello everyone,

I'm staring myself blind at my code at the moment. I have succesfully implemented this method before but somehow it won't work now. I'm trying to get a versionnumber out of my database and display it in the footer. But whatever I try it will not pass the variable to the view. I've literally copied the code from a working version I have on another page. I'm hoping I'm looking over something really simple. To make it easier to spot I've replaced the function for a static value, but that also doesn't work. Can you please look at my code and see what I'm doing wrong? Thank you so much in advance

My model (which in this example is not being used but is being called so I've included it for you):
Code:
<?PHP
class Versionxmodel extends CI_Model
{
   function __construct()
   {
      parent::__construct();
   }
  
    function getcurrentversion()
    {
  $kw = "SELECT parvalue FROM sys_par WHERE parname = 'versionnumber'";
        $query = $this->db->query($kw);
        return $query->result();
    }
  
}
?>

My controller:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Footer extends CI_Controller
{
function __construct()
{
  parent::__construct();

  $this->load->helper('url');
  $this->load->model('versionxmodel');
}

function index()
{
  
  //$versionresult = $this->versionxmodel->getcurrentversion();
  $testvalue = 'testvalue';
  $data = array('test' => $testvalue);
  $this->load->view('footer', $data);
  
  
}
}
?>

My view:
Code:
<FOOTER>
&lt;?PHP

print_r($test);

if ($this->ion_auth->logged_in())
  {
   $user = $this->ion_auth->user()->row();
  }

?&gt;
Logged in as:
&lt;?PHP
if ($this->ion_auth->logged_in()){
echo $user->username;  
}
else
{
echo "Nobody!";

}
if ($this->ion_auth->is_admin())
  {
   echo " (+) ";
   }
  
?&gt;

</FOOTER>

The error I keep getting (wether I try print_r($test); or echo $test; ) is Undefined variable: test


Variable not being passed - El Forum - 12-26-2013

[eluser]Tpojka[/eluser]
Check views again.

You should add the key to your $data variable because using as is, it is CI native engine (as you calling it after view 'footer').
Your key should be unique value that can be called in view as required.
Something like:
Code:
// controller
$testvalue = 'testvalue';
$data['myKey'] = array('test' => $testvalue);
$this->load->view('footer', $data);
Code:
// view
print_r('myKey');
Test it yourself.


Variable not being passed - El Forum - 12-26-2013

[eluser]Booster77[/eluser]
Edit: Not the solution afterall Sad


Variable not being passed - El Forum - 12-26-2013

[eluser]Tpojka[/eluser]
I made mistake above.

This is how it should be used.
In view you would call it as variable:
Code:
// view
print_r($myKey);



Variable not being passed - El Forum - 12-26-2013

[eluser]Booster77[/eluser]
Thanks again! Smile


Variable not being passed - El Forum - 12-26-2013

[eluser]Booster77[/eluser]
I'm sorry, I was too soon to celebrate. I literally copied your code, to make sure I did nothing wrong and I get the same error:

Message: Undefined variable: myKey


Variable not being passed - El Forum - 12-26-2013

[eluser]Booster77[/eluser]
Ok I think I found out what my problem is.

I load footer through another controller. So in another controller I say load header, main and footer. So although the data is being transported, it transport it to that controller and THEN it loads my footercontroller.

So I obviously need to build this part in the first controller and not the footer.

I found out by loading the footer view directly.

Thank you so much for helping!


Variable not being passed - El Forum - 12-26-2013

[eluser]InsiteFX[/eluser]
When you are having problems sending $data to your views you can always do this which will make them global to all of your views

Code:
$data = array();

$data['test'] = 'this_value';

$this->load->vars($data);      // This will load $data global to all of your views.
$this->load->view('footer');



Variable not being passed - El Forum - 12-27-2013

[eluser]Booster77[/eluser]
That is also very helpful information! Thank you so much.


Variable not being passed - El Forum - 12-27-2013

[eluser]Tpojka[/eluser]
+1