Welcome Guest, Not a member yet? Register   Sign In
Another noob question on passing $data
#1

[eluser]nubianxp[/eluser]
hey guys, sorry for starting a series of noob posts but I was hoping you could extend some help and patience...

I'm using the world.sql sample and I have the following code inside my controller (just for testing):

Code:
function index() {
  $sql = "SELECT * FROM country";
  $result = $this->db->query($sql);
  $data['country'] = $result->result_array();
  $this->load->view('view_main',$data);
}

in my view_main.php
Code:
<ol>
  &lt;?php foreach ($country as $country) {?&gt;
  <li>&lt;?=$country['Name']?&gt;
    <ul>
      <li>&lt;?=$country['Region']?&gt;</li>
      <li>&lt;?=$country['GovernmentForm']?&gt;</li>
    </ul>
  </li>
&lt;?php };?&gt;
</ol>

it's fine right now but how about if I want to pass the $data as
Code:
&lt;?=$Region?&gt;
instead, how do I format the logic in the controller?

I apologize, but I've been browsing a lot of resources but I still don't get some of it for the life of me but I'm getting the $data variable concept a bit now. LOL

Thanks for any help.
#2

[eluser]umefarooq[/eluser]
hi
if you are getting a single record from database than you can do some thing like in you controller

Code:
$sql = "SELECT Region FROM country";
  $result = $this->db->query($sql);
  $data['Region'] = $result->row();
than you can print only Region in your view.
#3

[eluser]TheFuzzy0ne[/eluser]
I'd suggest that you export things like that to a model, and then the model can pass your data back in whatever format you need.
#4

[eluser]nubianxp[/eluser]
@umefarooq:
thanks, how about if I need to do that on several items?

@TheFuzzyOne:
thanks, I'll be moving it to the Model on the actual application, right now I just use the controller for testing.

So, any tips on how to do it on several items? e.g.
Code:
&lt;?=$Name?&gt;
&lt;?=$Region?&gt;
&lt;?=$GovernmentForm?&gt;

Thanks folks!
#5

[eluser]TheFuzzy0ne[/eluser]
Code:
# Model method
function get_country()
{
    $sql = "SELECT * FROM country";
    $result = $this->db->query($sql);
    return $result->result_array(); # Return an array.
}

Assign values individually.
Code:
# Controller - Method 1

$country = $this->my_model->get_country();

$data = array();
$data['Name'] = $country['Name'];
$data['Region'] = $country['Region'];
$data['GovernmentForm'] = $country['GovernmentForm'];

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

Use list().
Code:
# Controller - Method 2

$country = $this->my_model->get_country();

$data = array();
list($data['Name'], $data['Region'], $data['GovernmentForm']) = $country;

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

Merge the results.
Code:
# Controller - Method 3

$country = $this->my_model->get_country();

$data = array();
$data = array_merge($data, $country);

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

None of this code has been tested, it serves simply to illustrate my suggestions. It's advisable to add some more logic to check for results where necessary, or to set the defaults.
#6

[eluser]nubianxp[/eluser]
ah thanks, I'll try them out, guess I'll need to expand my PHP.




Theme © iAndrew 2016 - Forum software by © MyBB