Welcome Guest, Not a member yet? Register   Sign In
Not able to pass data to a view
#1

[eluser]Giuseppe Caruso[/eluser]
Hi all,
a first alert: it's a newbie question, sorry for that, I am just starting learning CI.

I am trying to pass some data from a controller to a view, but I get nothing.

In the controller "start.php" I have the "showfolders" function I wrote like this:
Code:
function showfolders() {
        $this->load->helper('directory');
        $map = directory_map('.');
        $this->load->view('showfolders', $map);
    }

While the "showfolders.php" view is:
Code:
<html>
    <head>
    <title>showfolders</title>
    </head>
    <body>
    <dl>
    &lt;? foreach ($map as $k => $v) {
        echo "<dt>$k</dt>
              <dd>$v</dd>"
    } ?&gt;
    </dl>
    <pre>
    &lt;? print_r($map) ?&gt;
    </pre>
    &lt;/body&gt;
&lt;/html&gt;

Yep, *VERY* basic, I am just at the first chapters of books I got… 
But, visiting the URI "/start/showfolders/" I am not able to show any data.
Thanks you all for you patience. Smile
#2

[eluser]techgnome[/eluser]
take a look at how it's shown in this thread: http://ellislab.com/forums/viewthread/167509/

you need to load your data into an associative array and pass the array to the view...

Using your code, it would look like this:
Code:
$dat['map'] = directory_map('.');
$this->load->view('showfolders', $data);

when CI loads a view, it converts the array into variables for you... so in your view, $map will exist so you can use it just like you are.

-tg
#3

[eluser]n0xie[/eluser]
You misspelled $data :-)
[quote author="techgnome" date="1284655585"]
Code:
$data['map'] = directory_map('.');
$this->load->view('showfolders', $data);
[/quote]
#4

[eluser]techgnome[/eluser]
maybe I did that intentionally... to prevent blind copy pasting....

or maybe there's a reason I'm a programmer and not an English teacher.

or maybe I just haven't had enough coffee yet...
Code:
if (my.cup.Coffee == coffee.State.Empty){
  while (my.cup.Coffee != Cups.State.Full){
    breakRoom.Appliance.CoffeeMachine.Fill(my.cup.Coffee, water.Amount.Oz8, coffee.Type.Kona);
    my.cup.Coffee.Add(new Creamer(Hazelnut) + new sweetner(Equal));
    }
  }

-tg
#5

[eluser]Giuseppe Caruso[/eluser]
Hi guys, thank you both.
Sadly it doesn't work… :/

I am sure I wrote them correctly, I also checked paths, but it doesn't show anything.
The strange behavior is that is doesn't even shot the title which is hard coded into the HTML code so it makes me think about a view issue, could it be?

Btw, any good CI book to suggest? I am currently reading "Codeigniter 1.7" Packt publishing but I find it really confusing. I would not suggest it to anyone.

Thank you again.
#6

[eluser]techgnome[/eluser]
just for squirts and giggles... what happens when you take the loop out of your view? Does it load then?

And just to double check, the controller is in the controllers folder and the view is in the views folder right? (I ask because I did see another thread where some one had the files in the wrong folders.)

-tg
#7

[eluser]Giuseppe Caruso[/eluser]
[quote author="techgnome" date="1284659328"]what happens when you take the loop out of your view? Does it load then?[/quote]

Do you mean by doing the following?

Code:
function showfolders() {
        $this->load->view('showfolders');
    }

It doesn't load the view… :long:

Quote:And just to double check, the controller is in the controllers folder and the view is in the views folder right?
(I ask because I did see another thread where some one had the files in the wrong folders.)

Yep, double checked, but could be some eyes more could see what I don't see:

Foder structure screenshot

The question is absolutely pertinent, too much times I made these simple errors. Wink
#8

[eluser]Phil Sturgeon[/eluser]
If in doubt, var_dump(). They have shown you how to pass data to your views, now you have to test your own data.
#9

[eluser]techgnome[/eluser]
I meant the loop INSIDE your view file... not in your controller... (in fact there isn't a loop in the controller, but I'm being pedantic).

this is what I meant:
Code:
&lt;? foreach ($map as $k => $v) {
        echo "<dt>$k</dt>
              <dd>$v</dd>"
    } ?&gt;

What happens when you take that out? Does the view load then?

-tg
#10

[eluser]Giuseppe Caruso[/eluser]
[quote author="techgnome" date="1284670330"]I meant the loop INSIDE your view file... not in your controller... (in fact there isn't a loop in the controller, but I'm being pedantic).[/quote]
You are not pedantic, I told you it was a newbie question. Smile

Quote:What happens when you take that out? Does the view load then?
It… works! Smile
What's wrong with that code?
Now with the view:
Code:
&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;showfolders&lt;/title&gt;
[removed]
    alert "ciccio";
[removed]
    &lt;/head&gt;
    &lt;body&gt;
    <pre>
        &lt;?= print_r($map) ?&gt;
    </pre>
    &lt;/body&gt;
&lt;/html&gt;

I get the following (shortened) page:

Code:
Array
(
    [css] => Array
        (
            [0] => styles.css
        )

    [0] => index.php
    [1] => license.txt
    [system] => Array
        (
            [application] => Array
                (
                    [config] => Array
                        (
                            [0] => autoload.php
                            [1] => config.php
                            [2] => constants.php
                            [3] => database.php
                            [4] => doctypes.php
                            [5] => hooks.php
                            [6] => index.html
                            [7] => mimes.php
                            [8] => routes.php
                            [9] => smileys.php
                            [10] => user_agents.php
                        )

                    [controllers] => Array
                        (
                            [0] => email.php
                            [1] => ftp.php
                            [2] => index.html
                            [3] => start.php
                            [4] => welcome.php
                        )

and so on…

What was wrong with that loop?
Anyway, thanks you all, I learned a lot so far.




Theme © iAndrew 2016 - Forum software by © MyBB