Welcome Guest, Not a member yet? Register   Sign In
Links in VIEW, transferred from CONTROLLER...
#1

[eluser]Harvard725[/eluser]
Hello CodeIgniters....

I want to create an array in a CONTROLLER, send it to my VIEW and then use a function to iterate through the array, displaying each element as a link.


Here's the CONTROLLER code...
=======================================
class Footer extends Controller {

function index()
{

$this->load->helper('url');

$data['linkA'] = anchor('welcome','Main Page',array('class' => 'lnkA'));
$data['linkB'] = anchor('news','News',array('class' => 'lnkA'));
$data['linkC'] = anchor('shop','Store',array('class' => 'lnkA'));
$data['linkD'] = anchor('about','About Us',array('class' => 'lnkA'));
$data['linkE'] = anchor('contact','Contact Us',array('class' => 'lnkA'));

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

}


My goal is to allow as little PHP code in the VIEW as possible... Every time I pass the $data array to the VIEW and attempt to iterate I get an "UNDEFINED VARIABLE data" error messsage... I can echo an associative array without a problem but I'd rather call a function that returns a value so that I can put <?php =$txtLinks ?> in the VIEW and have them appear. Another question... Do I put the function in a MODEL?

Thanks for any help or suggestions...
#2

[eluser]Armchair Samurai[/eluser]
You'll need to pass an array to your view if you want to iterate through them:
Code:
$data['links'] = array(
    'foo' => anchor('url', 'url'),
    'bar' => anchor('url2', 'title2')
);
$this->load->view('vfooter', $data);

// then in the view

<?php foreach ($links as $val) echo $val;?>
#3

[eluser]Harvard725[/eluser]
Thank you for the response...

I think I've tried this... I still receive the following error messages... Maybe my CI configuration is wrong?




A PHP Error was encountered
Severity: Notice

Message: Undefined variable: data

Filename: views/vfooter.php

Line Number: 4

A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/vfooter.php

Line Number: 4
#4

[eluser]Fr3aked0ut[/eluser]
Can you show us some more code?
#5

[eluser]Harvard725[/eluser]
Actually those are the error messages I receive by using the code you provided in your original response. Am I doing something wrong? Does that code work properly for you?
#6

[eluser]Michael Wales[/eluser]
It appears as if you are using $data in your view file somewhere. CodeIgniter will automatically bump the passed array up one-level within the view (getting rid of the initial level).

For example, this is our controller:
Code:
function index() {
  $data['links'] = array('foo' => 'foo/index', 'bar' => 'bar/index');
  $this->load->view('links', $data);
}

You would access these values via $links within your view (ignore the $data array):
Code:
<ul>
&lt;?php foreach ($links as $text => $path): ?&gt;
  <li>&lt;?php echo anchor($path, $text); ?&gt;</li>
&lt;?php endforeach; ?&gt;
#7

[eluser]Harvard725[/eluser]
Thank you. That now works for me. Another question... What I really want to do is only have one line of PHP code in my view file. Is there a reasonble way to do this?

I'm thinking that if I put the code you've got above into a function, store it in another page (call it functions.php) , include the fuctions.php at the top of the view page and ask for a return value... The result being that all I've got in my VIEW is &lt;?php txtLinks ?&gt;... There. Now if the designer want to move those text links anywhere on the page, she can without the fear of messing up my code.

Will this work? Is it too much for the server if this is done repeatedly?

Thank you for your help thus far... I'm really new at this CI thing and this forum has helped me a lot.




Theme © iAndrew 2016 - Forum software by © MyBB