views:

96

answers:

5

I made a PHP-function which returns an array and then I move its value to a variable $a. How can I output it to a web page on CodeIgniter?

A: 

It depends on how you want your output.

Considering you have this array :

$a = array(
    'glop', 'test', 'hello', 
);

The simplest way to display the items it to join them, using implode :

echo implode(', ', $a) . '<br />';

Which will give you :

glop, test, hello


Another solution would be to loop over the items, using a foreach loop :

echo '<ul>';
foreach ($a as $item) {
    echo '<li>' . htmlspecialchars($item) . '</li>' . "\n";
}
echo '</ul>';

And you'd get this HTML source :

<ul><li>glop</li>
<li>test</li>
<li>hello</li>
</ul>

i.e. an un-numbered list.

With this, the possibilities are almost un-limited, and you can get pretty much any kind of output you'd want.


If you just want to output the array to see what it contains, for debugging, var_dump and print_r are generally useful :

echo '<pre>';
print_r($a);
echo '</pre>';

will give you :

Array
(
    [0] => glop
    [1] => test
    [2] => hello
)

And if you install the Xdebug extension, var_dump is even better -- colors and all ;-)

Pascal MARTIN
A: 

Possibly by:

print_r($a);

If you want pretty formatting:

echo "<pre>";
print_r($a);
echo "</pre>";

You could stick stuff between them

echo join(",", $a);

Or you could give them pretty formatting

echo "<ul>";
foreach($a as $k => $v)
{
    echo "<li>" . $k . ":" . $v . "</li>";
}
echo "</ul>";
Chacha102
A: 

How can I output it to a web page on CodeIgniter?

I suspect you mean to send the variable data to the View. You can do like this:

class Page extends Controller {

   function index()
   {
      $data['my_var'] = $your_var;
      $this->load->view('name_here');
   }

}

Now you can access the $data['my_var'] on your View normally.

Sarfraz
why down vote............. ?
Sarfraz
You haven't passed the $data variable to the view. (Though it wasn't me who down-voted)
John McCollum
how i have not passed data var to view? i have said at the last line of my answer: "Now you can access the $data['my_var'] on your View normally".
Sarfraz
A: 

This should do the trick. Don't forget to include the variable that you want to be loaded together with the view, like in the following example:

class Page extends Controller {

    function index()
    {
        $data['my_var'] = $your_var;
        $this->load->view('name_here', $data);
    }

}
Industrial
A: 

In the Controller:

<?php
    class Page extends Controller {

        function index()
        {
            $data['array_var'] = $this->get_some_array(); // this function returned an array
            $this->load->view('name_here', $data);
        }

    }

In the View:

<?php

foreach ($array_var as $arr):

    echo $arr;

endforeach;

?>

I hope this answers your question.


Note: the $data variable won't reflect in the view. CI extracts that variable and returns the array keys as variables like in the example.

Thorpe Obazee