1

I have 2 tables: Events and Registration.

Events Fields: EventID and EventName

Registration Field: MemberID and Name

I want to pull the EventName and the Name- of the person. How do I do this with a model? The code I have:
Model

function getAll()
{
    $query = $this->db->get('tblRegistration');
    if( $query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    $query->free_result();
    return $data;
}

controller

public function getallevents() {
    $this->load->model('events_model');

    $data['records'] = $this->events_model->getAll();

    $this->load->view('view-events', $data);
}

View

              foreach ($records as $row):
                        echo "<tr>";
                        echo "<td> $row->Name</td>";
                        // echo "<td> $row->intLocationID</td>";
                        echo "</tr>";
                    endforeach;

Edit

OK I have added the data that I want out of the table.

model

   function getAll()
{
    // get all the records from the schools table

    $this->db->select('*');
    $this->db->from('tblRegistration as reg');
    $this->db->join('tblMeetRace as met', 'reg.intMeetRaceID = met.intEventID');
    $query = $this->db->get();

    // if the number of rows returned is more than 0
    if( $query->num_rows() > 0) {
        // loop through each record (row) and place that
        // record into an array called $data
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    $query->free_result();
    // return the array to the controller
    return $data;
}

view

     foreach ($records as $row):
                        echo "<tr>";
                        echo "<td> $row->intMeetRaceID/td>";
                        echo "<td> $row->intEventID</td>";
                        echo "</tr>";
                    endforeach;

but I get an error of:

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/view-events.php
Line Number: 89

1 Answer 1

0

First of all you need to add another column in your events table for storing the MemberId, say memberid. Once you have that, when ever a member registers for an event, you can store his ID in the events table and relate it.

Then you can use Joins to fetch the datas

$this->db->select('*');
$this->db->from('registration as reg');
$this->db->join('events as evt', 'reg.id = evt.memberid');

$query = $this->db->get();
4
  • Thanks for the replay so do i have to put things in the select('*') or the get()? or dose that select everything? Commented May 18, 2015 at 11:14
  • It selects everything
    – Abhinav
    Commented May 18, 2015 at 11:25
  • if you can look at the edit i have made now i am getting this error Commented May 18, 2015 at 11:35
  • Thats because I think the result is empty. try printing the result and see what you get. Also try $this->db->last_query(), this prints the latest query that has run, check whether that is the desired query which you wanted to run
    – Abhinav
    Commented May 18, 2015 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.