Welcome Guest, Not a member yet? Register   Sign In
trim the views php
#1

[eluser]trevor2[/eluser]
Need some assistance with trimming my php in the view and throwing it into the function.
I like how wordpress calls their php in the html by using functions. What I'm not sure about is the proper coding to accomplish all of this. Array or object? I understand it's not completely wrong to have this in the view, but it would seem cleaner. A search mentioned using helper functions, which I guess should be read up on.

This is in my controller:
Code:
function edit_users()
        {
            $this->load->database();
            $data['query']     = $this->db->query('SELECT * FROM ci_users');
            $this->load->view('/mysite/admin/edit-users', $data);
        }

This is in my view:
Code:
<?php
    foreach ($query->result() as $row)
    {
        echo '<tr>';
        echo '<td class="tc">&lt;input type="checkbox"&gt;&lt;/td>';
        
        if($row->group_id == 0)
        {
            echo '<td class="tc">No</td>';
        }else{
            echo '<td class="tc">Yes</td>';
        }
        
        if($row->upload_id == 0)
        {
            echo '<td class="tc">No</td>';
        }else{
            echo '<td class="tc">Yes</td>';
        }

        echo '<td class="tc">' . $row->username . '</td>';
        echo '<td class="tc">' . $row->join_date . '</td>';
        echo '<td class="tc">' . $row->last_visit . '</td>';
        echo '<td class="tc">' . $row->ip_address . '</td>';
        
        if($row->activated == 0)
        {
            echo '<td class="tc">No</td>';
        }else{
            echo '<td class="tc">Yes</td>';
        }

        echo '</tr>';
    }
  ?&gt;
#2

[eluser]trevor2[/eluser]
Here some code I tried to change. It does seem to output some the html and do query logic, but my knowledge of array's and objects is not that great as you can see. I have the following error:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/main.php

Line Number: 59


Code:
function edit_users()
        {
            $this->load->database();
            $data['query']     = $this->db->query('SELECT * FROM ci_users');
            
            foreach ($data['query'] as $row)
            {
                echo '<tr>';
                echo '<td class="tc">&lt;input type="checkbox"&gt;&lt;/td>';
                
                if($row->group_id == 0) // error Line 59
                {
                    echo '<td class="tc">No</td>';
                }else{
                    echo '<td class="tc">Yes</td>';
                }
                
                if($row->upload_id == 0) // error
                {
                    echo '<td class="tc">No</td>';
                }else{
                    echo '<td class="tc">Yes</td>';
                }

                echo '<td class="tc">' . $row->username . '</td>'; // error
                echo '<td class="tc">' . $row->join_date . '</td>'; // error
                echo '<td class="tc">' . $row->last_visit . '</td>'; // error
                echo '<td class="tc">' . $row->ip_address . '</td>'; // error
                
                if($row->activated == 0) // error
                {
                    echo '<td class="tc">No</td>';
                }else{
                    echo '<td class="tc">Yes</td>';
                }

                echo '</tr>';
            }
            return;
            $this->load->view('/mysite/admin/edit-users', $data);
        }
#3

[eluser]jedd[/eluser]
Hi trevor2,

First, you may want to prune your view files by changing things like this:
Code:
// original
        if($row->group_id == 0)
        {
            echo '<td class="tc">No</td>';
        }else{
            echo '<td class="tc">Yes</td>';
        }

... into this ...

Code:
// suggested
        echo '<td class="tc">';
        echo ($row->group_id) ? "Yes" : "No";
        echo '</td>';


Now, I'm not quite sure what you mean about moving view stuff 'into the function' here.

Your controller would, generally, not be making database calls directly - it'd be consulting your relevant model, and the model does the databaase calls, and your controller gets an object or an array sent back.

You're allowed to work with arrays rather than objects, too, of course - the functions exist to go either way, as you've doubtless read in the [url="http://ellislab.com/codeigniter/user-guide/database/queries.html"]Database Queries[/url] page of the user guide. I tend to work with arrays in preference to objects, as they're what I grew up with.

In your second message, it looks like you've moved all your HTML - the domain of your view - into your controller ...? This misses the point of MVC, if it's the case, but certainly having a database query and HTML generation in the one file is way off.




Theme © iAndrew 2016 - Forum software by © MyBB