Welcome Guest, Not a member yet? Register   Sign In
Noob! Accessing a variable from an array
#1

[eluser]trickymh[/eluser]
Hello,

I apologise if this has been covered before. I have searched, but only found one similar question, and the fix didn't work for me.

All I want to do is retrieve the value of a field, and use it in a view.

My Controller:

Code:
function index()
    {
        $this->load->model('data_model');
        $data['resources'] = $this->data_model->get_resource(); // get info from table
        $data['no_props'] = $this->db->count_all('resources'); // count no. of rows in table
        $this->load->view('home_view', $data);
    }

My model code:

Code:
function get_resource()
    {
        $results = $this->db->get('resources')->result();
        return $results;
    }

And my view code (well, the important bit)

Code:
<div>&lt;?php echo $results->name;?&gt;</div>

where 'name' is the name of the field. For testing's sake there is only one row in the 'resources' table.

I get the following errors:
undefined variable: results
trying to get property of non-object

I have tried so many different syntaxes and ways of doing this. I'm going mental hehe.

Thanks for any help
#2

[eluser]Adam K[/eluser]
You have to notice, that you're returning 'results' into your $data array under index 'resources'.

So in view, rather than
Code:
echo $results->name;
, you have to call
Code:
echo $resources->name;
#3

[eluser]trickymh[/eluser]
[quote author="Adam K" date="1270918560"]You have to notice, that you're returning 'results' into your $data array under index 'resources'.

So in view, rather than
Code:
echo $results->name;
, you have to call
Code:
echo $resources->name;
[/quote]


Thanks Adam, when I do this I get a 'Trying to get property of non-object' error on the view code.
#4

[eluser]John_Betong[/eluser]
Try this:
Code:
// model
function get_resource()
{
  $query = $this->db->get('resources', 1);


  if ($query->num_rows() > 0)
  {
    $row = $query->row();
    
   // echo $row->name;
  }
    
  return $query->row();
}

// controller
function index()
{
  $this->load->model('data_model');
  $data['resources'] = $this->data_model->get_resource(); // get info from table

  // $data['no_props'] = $this->db->count_all('resources'); // count no. of rows in table

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

// view
<div>&lt;?php echo $resources->name;?&gt;</div>
&nbsp;
&nbsp;
edit: changed view $result-> to $resources->name
&nbsp;
&nbsp;
&nbsp;
&nbsp;
#5

[eluser]Adam K[/eluser]
@trickymh - that probably means, that what you get as return from your model isn't what you think it is - this usually happens, when you 'think' you're returning db array, but somehow, something else is returned (i.e. -1 for not found in database).

Try following in your view:

Code:
&lt;?php
  var_dump($resources);
?&gt;
And you'll see what is inf $data['resources'] when you post it to your template.
#6

[eluser]John_Betong[/eluser]
If you are using Windows try this - the output will be formatted and easier to read:
Code:
echo '<pre>';
    var_dump($data['results']);
  echo '</pre>';
  die;
&nbsp;
#7

[eluser]trickymh[/eluser]
Hi Adam,

I tried your code and got the original errors.

I have tried the var_dump thing before, and it shows the data I'm after, which is why it's so frustrating i can't access it. This is the output:

object(stdClass(2) { ["id"]=>string(1)"1"["name"]=>string(9)"Villa One" }

Thanks for your time!
#8

[eluser]trickymh[/eluser]
[quote author="John_Betong" date="1270919884"]If you are using Windows try this - the output will be formatted and easier to read:
Code:
echo '<pre>';
    var_dump($data['results']);
  echo '</pre>';
  die;
&nbsp;[/quote]

Thanks John, on a mac
#9

[eluser]Adam K[/eluser]
I may be obvious, but it works like this:

everything you assign to $data as (i.e.)
Code:
$data['myIndex']
should be then avalaible as
Code:
$myIndex
in your view, when you load your view via
Code:
$this->load->view($view,$data);

so, it comes down to fact, that you're referencing to different values.
#10

[eluser]trickymh[/eluser]
[quote author="John_Betong" date="1270919030"]Try this:
Code:
// model
function get_resource()
{
  $query = $this->db->get('resources', 1);


  if ($query->num_rows() > 0)
  {
    $row = $query->row();
    
   // echo $row->name;
  }
    
  return $query->row();
}

// controller
function index()
{
  $this->load->model('data_model');
  $data['resources'] = $this->data_model->get_resource(); // get info from table

  // $data['no_props'] = $this->db->count_all('resources'); // count no. of rows in table

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

// view
<div>&lt;?php echo $resources->name;?&gt;</div>
&nbsp;
&nbsp;
edit: changed view $result-> to $resources->name
&nbsp;
&nbsp;
&nbsp;
&nbsp;[/quote]


John, I love you, it worked! Sorry, i didnt see you'd posted the orignal code suggestion, and i didnt see the edit you made till a minute ago.

I'm just going to figure out why it worked now...

Many thanks,

Matt




Theme © iAndrew 2016 - Forum software by © MyBB