Welcome Guest, Not a member yet? Register   Sign In
Mysql Results Conditional
#1

[eluser]bunoire14[/eluser]
Hey Guys,

I've just picked up the framework for a project im working on. The idea is for a Flight Simulator Training Website that lists training courses being offered by various members, other members can then apply to take part..... basically...although a little more complex.

To begin with i have been watching some of the beginners tutorials from the wiki page, and since my previous PHP expereince has all been procedural, im trying to get my head round this MVC theory.

For the most part i have sussed it out and thanks to some great tutorial vids, managed to get the basics of my forward facing site in place.

I am however struggling with one area.... I have a page that simply querys MYSQL and gets all the information for the instructors from the DB. It then tables that info into a roster. The DB Table holds personal info, name, etc, but also the areas of training the staff member is allowed to teach. This info is stored as a BOOLEAN value on the DB, so when its pulled in the puery and displayed on the table I simply get a few rows of 1's or 0's... which is fine. What I would like to do is put a conidtional into the system somewhere that basically says if the result is 1 then display tick.gif, if the result is 0 display cross.gif... in essence to display images on the table not the raw 1's and 0's.

Now using the MVC methodology I'd expect that this would be done in the Controller? since its not exactly a DB query as much as it doing something with a result...however Its also dealing with how the data is formatted and displayed so could it also be done in the view?

Any help on where i should do this would be great, also suggestions on how to achieve it would be handy as I'm still trying to get my head round MVC and the different syntax used here.


Heres my Controller Method

Code:
//Site Staff Page Method
        function staff()
        {
            $this->load->model('staff_model');
            $data['staff_records'] = $this->staff_model->getStaff();
            $this->load->view('site_head');
            $this->load->view('staff', $data);
            $this->load->view('site_foot');
        }

Heres my Model

Code:
//Staff Model

class Staff_model extends Model{

    function getStaff()
    {
        $query = $this->db->query('SELECT * FROM staff ORDER BY callsign ASC');
        if($query->num_rows() >0){
        foreach($query->result() as $row)
        {
            $data[] = $row;
        }
        return $data;
        }
    }
}
#2

[eluser]johnpeace[/eluser]
Quote:Now using the MVC methodology I’d expect that this would be done in the Controller? since its not exactly a DB query as much as it doing something with a result…however Its also dealing with how the data is formatted and displayed so could it also be done in the view?

Any help on where i should do this would be great, also suggestions on how to achieve it would be handy as I’m still trying to get my head round MVC and the different syntax used here.

Hi! I'm a professional pilot and Codeigniter developer and so took interest in your post when I saw it in my RSS feed...

The MVC architecture is just that...an architecture. Don't forget that you are still the architect and builder and have the freedom to build whatever you like and whatever you would prefer to maintain moving forward.

The way I would handle this would probably be to put it in a function in a helper file. If you're having this problem, you'll probably have other issues where PHP could be used to do some UI stuff in your app. So, a helper like 'user_interface_helper'....

The function would look something like this:

Code:
public function boolean_icon($in) {
    $in = (int)$in; // cast as int just to be sure
    $out = array(0 => 'tick.gif', 1 => 'cross.gif');
    return $out[$in];
}

That will return the filename you want to use...you could modify it to return an <img> tag if you like...

The advantage of putting it in a function in a helper is that you have access to it in ALL of your controllers and views, which gives you a little more flexibility than just coding it as an if/else statement in the view you're working on right now.

As your app grows you'll come up with other ways you want to use the same cross/tick designation of boolean data and have it easy if you're only modifying the way you do it in one place.
#3

[eluser]bunoire14[/eluser]
Hi,

Thanks for the assits with this ill give it a crack, and you are right i will be using this sort of UI to give students a visual representation of progress made through courses so Ill have a crack at creating this helper, and include it in other places, is it worht auto loading this or just calling where needed?


good to see some aviation professionals floating about on here ;-)
#4

[eluser]metaltapimenye[/eluser]
when light weight is the issue.. "calling where needed" is the answer sir.

if practical/simplicity is the main menu, this might be an options..
Code:
#controller
#..
$data['header']=$this->load->view('header');
$data['content']=$this->load->view('staff_specific_content');
$data['footer']=$this->load->view('footer');

$this->load->view('general_page_template',$data);
#..
# view/staff_specific_content.php
#..
$this->load->model('staff_model');
$data_array=$this->staff_model->get_staff();
//..doing data processing
glad to know not just all programmer professionals here ^^
#5

[eluser]tonanbarbarian[/eluser]
because what you are trying to do is change the "presentation" of the data it should be done in the view

remeber MVC is about the 3 layers

business logic, i.e how to perform actions = Controller
data logic, i.e. how to interact with the data such as SELECT, INSERT, UPDATE, DELETE = Model
presentation login, i.e how to present the data taken from the model = View

You should always think about these things separately as well
The Controller should not know how the model gets the data, nor how the view presents it. The controller should just pass the data to the view and let the view determine how it should be displayed
The model should not know what the controller or view are going to do with the data, it should just process the data and return
The viewe should not care how the controller or model processed to give it data, it should just expect data in a mostly raw format and then present it somehow.

This approach of getting the view to do ALL formatting and presenting of the data is a bit more than most people do, and it also means that there is generally more PHP code in the view than some people like.
If you wish to reduce the PHP code in the view you can look at writing helper functions that help to process the display in the view as indicated above.

This is my take on how to do this, as indicated above it is just a guideline and you are not forced to do things this way. If you want you can make the change you want to the data in any of the controller, model or view. All I suggest is that you pick one approach and always follow that, at least in the same application.
#6

[eluser]bunoire14[/eluser]
Thanks Guys,

Heres how i did it, I think i followed the logic correctly.

I took the suggested route from @johnpeace and created a helper with the function in it:

Code:
//Change any Bool Result into Tick or Cross
    function boolean_icon($in)
    {
        $in = (int)$in; // cast as int just to be sure
        $out = array(0 => '<img src="'.base_url().'assets/img/site/cross.png" />', 1 => '<img src="'.base_url().'assets/img/site/tick.png" />');
        return $out[$in];
    }

As you can see I editied it slightly to return a full image tag.

I then called this helper in my controller like this:

Code:
class Staff extends Controller{
    
    //Site Staff Roster Page Method
        function roster()
        {
            $this->load->model('staff_model');
            $this->load->helper('ui');
            $data['staff_records'] = $this->staff_model->getStaff();
            $this->load->view('site_head');
            $this->load->view('staff_roster', $data);
            $this->load->view('site_foot');
        }
}

Then I used the method in the view like this:

Code:
&lt;?php foreach($staff_records as $row) :?&gt;
<tr>
    <td>&lt;?php echo $row->pid; ?&gt;</td>
    <td>&lt;?php echo $row->rank.' '.$row->fname.' '.$row->lname.' '; ?&gt;</td>
    <td>&lt;?php echo $row->cfs_role; ?&gt;</td>
    <td>&lt;?php echo $row->callsign; ?&gt;</td>
    <td>&lt;?php echo boolean_icon($row->efts); ?&gt;</td>
    <td>&lt;?php echo boolean_icon($row->one_fts); ?&gt;</td>
    <td>&lt;?php echo boolean_icon($row->two_fts); ?&gt;</td>
    <td>&lt;?php echo boolean_icon($row->three_fts); ?&gt;</td>
    <td>&lt;?php echo boolean_icon($row->four_fts); ?&gt;</td>
</tr>

&lt;?php endforeach; ?&gt;


This has the desired resuklt on the page itself, whether I have followed the MVC pattern doing it this way is another matter! LOL :red:

Thanks for the help Chaps,
#7

[eluser]johnpeace[/eluser]
Quote:his approach of getting the view to do ALL formatting and presenting of the data is a bit more than most people do, and it also means that there is generally more PHP code in the view than some people like.

It's not that it's more PHP than people like...

It's that there's more PHP, that's identical to other PHP, scattered across multiple view files.

It looks like this strict, dogmatic adherence to your idea of what MVC architecture is would have us writing a procedural if/else to display the icon throughout the site all over the place. That same code would need to appear wherever the icon appears.

What if the icon changes filenames or other attributes?
What if I decide to do it with '+' and 'x' characters instead?

If I run the code in a helper function that is called in the view files, I only have one function to maintain/change.

I don't know that I even agree with the premise that putting this code in a helper ISN'T putting it in the view. If a view uses a helper...does the helper not become part of the view? What about all of the CI native helpers (date, language, etc)...they can be used in views, right?

Like most things, there's not a great necessary need for absolute, black/white dogmatic rules about how to do things. Architectures and design patterns are great starting points...but really shouldn't be leveraged AGAINST your own ingenuity and creativity. As long as you aren't 'coding stupid' and are building an app that you can maintain...<shrug> go for it.
#8

[eluser]johnpeace[/eluser]
Quote:good to see some aviation professionals floating about on here
Quote:glad to know not just all programmer professionals here ^^

Personally...PHP development was my career before I got into aviation. I took a 3 year hiatus to train and work toward flying as a career and then got an airline job.

I had enough time on reserve that I picked up some contract clients and started building apps again. Then I was furloughed for almost two years. Good to have a second career!

Now I'm back in the cockpit and pretty busy, but maintain about 30 hrs a month of PHP billable time and am working on my own projects.

Specialization is for insects.




Theme © iAndrew 2016 - Forum software by © MyBB