Welcome Guest, Not a member yet? Register   Sign In
Simple Help Please
#1

[eluser]regal2157[/eluser]
Sorry if this is not in the right board, the layout of this forum is a bit confusing.

Anyways, I have a simple and basic question trying to display data from a database.

In my view, a $data variable is passed through, and I am trying to utilize it (and it's contents).

The controller sets the $data['featured_user'] = single row from database.

In the view, I tried to utilize it by doing
Code:
<?php if(isset($featured_user)): echo $row->username; endif; ?>
This presents me with this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/options_view.php

Line Number: 162

Then I tried doing it as an array:
Code:
<?php if(isset($featured_user)): echo $featured_user['username']; endif; ?>

This presents me with this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: views/options_view.php

Line Number: 162

I did a print_r on the $featured_user variable, and the username index is set..

Can anyone help me with what I may be doing wrongly?
#2

[eluser]fesweb[/eluser]
Seeing your controller code would give us more to work with...

How you set the $data has everything with how you access that data in the view.

Quote:The controller sets the $data[‘featured_user’] = single row from database.

Working semi-blind, but one possibility:
Code:
// controller
$data['featured_user'] = $my_query->row();
$this->load->view('my_view', $data);
// view
echo $featured_user->username;

Another possibility:
Code:
// controller
$data = $my_query->row();
$this->load->view('my_view', $data);
// view
echo $username;
#3

[eluser]regal2157[/eluser]
This is my current controller:

Code:
function index(){
        $data = array();
        $this->load->model('Site_model');
        if($q = $this->Site_model->get_last_announcments()){
            $data['announcements'] = $q;
        }
        if($q = $this->Site_model->get_featured_user()){
            $data['featured_user'] = $q;
        }
        
        $this->load->view('options_view', $data);
    }

The view shows both, later on in the page:

Code:
<?php if(isset($announcements)): foreach($announcements as $row): ?>
            <?php $date = explode('/',$row->date); ?>
                <div class="month">&lt;?php echo $date[0]; ?&gt;</div>
                <div class="date">&lt;?php echo $date[1]; ?&gt;</div>

            <div class="content">
                <h2>&lt;?php echo $row->title; ?&gt;</h2>
                <p>&lt;?php echo $row->entry; ?&gt;</p>
            </div>
            &lt;?php endforeach; ?&gt;
            &lt;?php else: ?&gt;
            <h2>No records were returned.</h2>
            &lt;?php endif; ?&gt;

The announcement part works fine.
#4

[eluser]InsiteFX[/eluser]
If featured_user is a single row then you need to display featured_user after your endforeach

If it is multiple rows then you will need to use another forecah loop.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB