Welcome Guest, Not a member yet? Register   Sign In
Outputting results from Array?
#1

[eluser]invision[/eluser]
Hi,

I'm a little stuck. I would love some help with this.

I want to output the results of a SQL Query into a View.

How do I do this?


model
Code:
function get_entries($country = FALSE, $city = FALSE, $category = FALSE) {

    $this->db->select('entry.*, city.title as city_title, country.iso3 as country_title, category.title as category_title');
    $this->db->from('entry');
    $this->db->join('category', 'entry.category_id = category.id');
    $this->db->join('city', 'entry.city = city.id');
    $this->db->join('country', 'city.country = country.iso3');
    if($country !== FALSE) {
      $this->db->where('country.iso3', $country);
    }
    if($city!== FALSE) {
      $this->db->where('city.title', $city);
    }
    if($category!== FALSE) {
      $this->db->where('entry.aslug', $category);
    }
    $result = $this->db->get();
    if($result->num_rows() > 0) {
      return $result->result_array();
    } else {
      return FALSE;
    }
  }

controller
Code:
if (!empty($param_3)) {
      $data['title'] = $param_3;
      $data['body'] = 'Info about Accommodation';  
        $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
        $data['entries2'] = $this->Mains->get_all_cities();
        $data['main'] = 'public_entry';  
     }

view
Code:
<h2>&lt;?php echo $entries['atitle']; ?&gt;</h2>

I know for certain that my SQL Query is working.

Can anyone explain what I am doing wrong?



Many thanks for your time and help.
#2

[eluser]Marcelo Reborn[/eluser]
Why don't you debug $data?

Code:
if (!empty($param_3)) {
      $data['title'] = $param_3;
      $data['body'] = 'Info about Accommodation';  
        $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
        $data['entries2'] = $this->Mains->get_all_cities();
        $data['main'] = 'public_entry';  

        // I know it's uncool ouput data on Controller, but is for a good reason :)
        echo '<pre>';
        print_r($data);
        echo '</pre>';

     }
#3

[eluser]invision[/eluser]
Cool.

What am I looking for in the output?

Here's what I've got:

Code:
Array
(
    [entities] => Array
        (
        )

    [title] => buchanan-view
    [main] => public_entry
    [body] => Info about Accommodation
    [entries] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [atitle] => Buchanan View
                    [metakeys] =>
                    [metadescr] =>
                    [aslug] => buchanan-view
                    [status] => published
                    [body] => This is the body of text here. This is the body of text here. This is the body of text here. This is the body of text here.
                    [address] => 35 Calgary Street, G4 0XG
                    [cost] => From: £94.00 p/w
                    [addressdescr] => Buchanan view is located...
                    [features] => Buchanan view offers a range...
                    [roomclass1] => Premium
Buchanan view offers both
                    [roomclass2] => Classic
Buchanan view offers a range of
                    [roomclass3] => Basic
There are no Basic rooms
                    [atype] => 1
                    [country] => 0
                    [city] => 2
                    [category] => 1
                    [category_id] => 6
                    [pubdate] => 2010-12-23
                    [img] => buchanan-view-1-small.jpg
                    [user_id] => 1
                    [city_title] => Turin
                    [country_title] => ITA
                    [category_title] => Student Accommodation
                )

        )

    [param_1] => ita
    [param_2] => turin
    [page_data] => Sorry, we have no Accommodation in this area at present.
    [entries2] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [slug] => glasgow
                    [title] => Glasgow
                    [country] => gbr
                )

            [1] => Array
                (
                    [id] => 2
                    [slug] => turin
                    [title] => Turin
                    [country] => ita
                )

            [2] => Array
                (
                    [id] => 3
                    [slug] => edinburgh
                    [title] => Edinburgh
                    [country] => gbr
                )

            [3] => Array
                (
                    [id] => 4
                    [slug] => manchester
                    [title] => Manchester
                    [country] => gbr
                )

            [4] => Array
                (
                    [id] => 5
                    [slug] => london
                    [title] => London
                    [country] => gbr
                )

            [5] => Array
                (
                    [id] => 6
                    [slug] => paris
                    [title] => Paris
                    [country] => fra
                )

            [6] => Array
                (
                    [id] => 7
                    [slug] => barcelona
                    [title] => Barcelona
                    [country] => esp
                )

            [7] => Array
                (
                    [id] => 8
                    [slug] => madrid
                    [title] => Madrid
                    [country] => esp
                )

            [8] => Array
                (
                    [id] => 9
                    [slug] => birmingham
                    [title] => Birmingham
                    [country] => gbr
                )

        )

)
#4

[eluser]Madmartigan1[/eluser]
Try this bud:

$this->output->enable_profiler(true);

Put that anywhere in your controller. All your queries will be displayed at the bottom of the page.
#5

[eluser]invision[/eluser]
Thank you Madmartigan. It shows 6 queries and they all successfully receive data.

Can you explain how I show this data in my View?

For instance, if I wanted to show the following data in my View:

Code:
[cost] => From: £94.00 p/w
[addressdescr] => Buchanan view is located...
[features] => Buchanan view offers a range...
[roomclass1] => Premium

How do I do this?


Many thanks for your help
#6

[eluser]Madmartigan1[/eluser]
Are you simply asking how to send data from a controller to a view?

Haven't you done this before?

http://ellislab.com/codeigniter/user-gui...views.html
#7

[eluser]invision[/eluser]
Thanks for the quick reply and your patience.

In my Controller, I'm currently using:
Code:
$data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
which as you can see stores all the data in an 'entries' array.

If I want to display the value of (say) roomclass1 in my View, how do I do this?

I have done this before, but for some reason using $roomclass1 doesn't display anything.

Am I passing it to the View incorrectly?


Thanks again for your help with this.
#8

[eluser]Madmartigan1[/eluser]
Code:
$data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);

When you send this to your view, it will be stored in an array named $entries. It does not alter the data in any other way. You would work with the array or object as you normally would in plain non-codeigniter PHP.
#9

[eluser]invision[/eluser]
As you can tell I'm pretty new to this.

I always presumed you'd do either $data['entries']['roomclass1'] or $entries['roomclass1'] but sadly both don't work.

Can you give me any pointers? I'm sure once I get this started, I'll be just fine Smile


Many Thanks
#10

[eluser]Madmartigan1[/eluser]
$data[‘entries’][‘roomclass1’] will work in the controller, in the view you want to use $entries[‘roomclass1’], assuming that your array keys are correct.

Does this make sense?




Theme © iAndrew 2016 - Forum software by © MyBB