Welcome Guest, Not a member yet? Register   Sign In
Passing possible arguments to functions
#1

[eluser]rhopek[/eluser]
I have a function in a controller as follows:

Code:
function show($event_id)
      {
        $this->load->model('volunteers_model');
        $data['records'] = $this->volunteers_model->getVols($event_id);
        $this->load->view('volunteers_show', $data);
      }

In my model, I have:

Code:
function getVols($event_id)
    {
        $this->db->select('*');
        $this->db->from('volunteers');
        $this->db->join('mapVolunteersToEvents', 'mapVolunteersToEvents.volunteers_id = volunteers.id', 'left');
        $this->db->where('mapVolunteersToEvents.events_id', $event_Id);
        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

The problem is that it won't always receive a value for that $event_id argument. If I pass one on the URL (like '/show/1'), everything is fine, but if I don't (/show), my view throws:

---
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Volunteers:Confusedhow()
Filename: controllers/volunteers.php
Line Number: 9
---

---
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: event_id
Filename: controllers/volunteers.php
Line Number: 11
---

What is the proper way to approach this? I've been pulling my hair out for hours and figured it was just time to suck it up and ask others. I think if I get past the first error, the second one will go away (or at least be easier to correct).

Thanks.
#2

[eluser]tonanbarbarian[/eluser]
Code:
function getVols($event_id=0)
    {
        $this->db->select('*');
        $this->db->from('volunteers');
        $this->db->join('mapVolunteersToEvents', 'mapVolunteersToEvents.volunteers_id = volunteers.id', 'left');
        if (!empty($event_id))
            $this->db->where('mapVolunteersToEvents.events_id', $event_id);
        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }
#3

[eluser]rhopek[/eluser]
Thanks.

That didn't quite work (doing it exactly as you outlined caused even more errors), but it got me on the right track.

I had to set $event_id=0 on the Controller's show() function, and then just set the if(!empty()) clause within the Model's getVols() function.

All is working now; thanks for setting me on the right path in the troubleshooting.
#4

[eluser]gyo[/eluser]
It's weird that default values for parameters don't work, which version of PHP are you using?
#5

[eluser]smilie[/eluser]
Well, I guess that depending on his DB structure, maybe it should be:

function show($event_id='0') {...}

But yes, generally, it is best to do if($event_id != '0').

Regards,
Smilie
#6

[eluser]gyo[/eluser]
True, or maybe something like this...

Code:
function getVols($event_id=false)

...AND...

Code:
if ($event_id) {

...




Theme © iAndrew 2016 - Forum software by © MyBB