Vincent Cerone
Vincent Cerone

Reputation: 48

Check if db row has specific values

I am creating an event, each event will have attendees, I got this working fine, the problem I am having is an attendee can attend the same event twice, which is not good.

Event View

<?php if($this->session->userdata('user_id')): ?>
<hr>
<?php echo form_open('/attendees/add/'.$event['id']); ?>
    <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']; ?>">
    <input type="submit" value="I'm in!" class="btn btn-success">
</form>
<?php endif; ?>

Attendees Controller

<?php
class Attendees extends CI_Controller {
    public function add($event_id) {
        // Check login
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }
        $this->form_validation->set_rules('user_id', 'required|callback_check_userid_eventid');
        if($this->form_validation->run() === FALSE){
            $this->session->set_flashdata('attendee_not_added', 'You are already on the list.');
            redirect('home');
        } else {
            $this->attendee_model->add_attendee($event_id);
            // Set message
            $this->session->set_flashdata('attendee_added', 'You have been added to this event.');
            redirect('events');
        }
    }
}

Attendees Model

<?php
class Attendee_model extends CI_Model {
    public function __contruct() {

    }

    public function get_attendees($id = FALSE){
        if($id === FALSE) {
            $query = $this->db->get('attendees');
            return $query->result_array();
        }
        $this->db->select('attendees.id, attendees.team, attendees.is_goalie, event.id, user.first_name, user.last_name');
        $this->db->from('attendees');
        $this->db->join('event', 'attendees.event_id = event.id', 'inner');
        $this->db->join('user', 'attendees.user_id = user.id', 'inner');
        $this->db->where('event.id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function add_attendee($event_id){
        $data = array(
            'event_id' => $event_id,
            'user_id' => $this->session->userdata('user_id')
        );
        return $this->db->insert('attendees', $data);
    }

    // Check attendee exists in event
    public function check_userid_eventid($event_id, $user_id){
        $query = $this->db->get_where('attendees', array('user_id' => $user_id, 'event_id' => $event_id));
        if(empty($query->row_array())){
            return true;
        } else {
            return false;
        }
    }
}

As you can see I tried creating a custom callback on the button form validation but it did not work.

If anyone can point me in the right direction, let me know if I am even somewhat close.

Thanks in advance.

Upvotes: 0

Views: 59

Answers (3)

Anfath Hifans
Anfath Hifans

Reputation: 1598

You are not passing the $event_id value to the callback function. Replace your validation with the following line

$this->form_validation->set_rules('user_id', 'User ID', 'required|callback_check_userid_eventid['.$event_id.']');

add following callback function inside the Attendees Controller file

public function check_userid_eventid($user_id, $event_id){
    $CI =& get_instance();
    $CI->load->database();
    $CI->form_validation->set_message('user_id_unique', "Sorry, that %s is already being used.");
    return isset($CI->db) ? ($CI->db->limit(1)->get_where('attendees',compact('user_id','event_id'))->num_rows() == 0) : false;
}

Upvotes: 1

Ryuk Lee
Ryuk Lee

Reputation: 744

Put the call back function into Controller

<?php
class Attendees extends CI_Controller {
    public function add($event_id) {
        // Check login
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }
        $this->form_validation->set_rules('user_id', 'required|callback_check_userid_eventid');
        if($this->form_validation->run() === FALSE){
            $this->session->set_flashdata('attendee_not_added', 'You are already on the list.');
            redirect('home');
        } else {
            $this->attendee_model->add_attendee($event_id);
            // Set message
            $this->session->set_flashdata('attendee_added', 'You have been added to this event.');
            redirect('events');
        }
    }         
    // Check attendee exists in event
    public function check_userid_eventid($event_id, $user_id){
        $query = $this->db->get_where('attendees', array('user_id' => $user_id, 'event_id' => $event_id));
        if(empty($query->row_array())){
            return true;
        } else {
            return false;
        }
    }      
}     

Upvotes: 0

Alex
Alex

Reputation: 9265

Your callback is in a model that form validation knows nothing about. If you look at the docs: callbacks should be in the same controller as the form validation method that uses it.

However, you can use a function in a model as a callback with a different syntax:

$this->form_validation->set_rules(
        'username', 'Username',
        array(
                'required',
                array($this->users_model, 'valid_username')
        )
);

as documented here.

Finally, on a false return, you need to make sure that you are setting a message as seen in this example:

public function username_check($str)
        {
                if ($str == 'test')
                {
                        $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                }
                else
                {
                        return TRUE;
                }
        }

In conclusion: the documentation has all the answers.

Upvotes: 0

Related Questions