1

I am creating a module where a students can join a class. Now to do this I need to insert id of the students to get their information.

I have created a query but it only update the table field, so the result is the teacher can only see 1 student in his/her class.

Is there a way to insert multiple ids in one table field to access all students who joined?

Here is my model - so everytime I join a class it updates the field. everytime another user logged in and join class then the value update. So the result is the teacher can only see 1 student and not those who joined in. How can I change this so that students can join in.

public function find_class(){
    $data = array(
        'student_id' => $this->user_id
    );

    $this->db->where('code', $this->input->post('code'));
    $this->db->like('code', $this->input->post('code'));
    return $this->db->update('groups', $data);
}

Controller

public function find_class(){
    $this->student_model->find_class();

    redirect('students/home');

}

And in my View

<?php echo form_open('students/find_class') ?>
    <input type="hidden" value="1" name="studId" />
        <div class="form-group">
            <h5>Class Code:</h5>
            <small><li>Only teachers shall give you code</li></small><br>
            <input type="text" class="form-control col-md-6" placeholder="Enter class code to join" name="code" required />
        </div>
        <hr>
        <button type="submit" class="btn btn-info btn-md">Join Class</button>
</form>
  • u can insert many ids with , like: 12,3,4,5,6 and use php explode for extract them – Fox Mar 22 '20 at 14:14
  • 3
    Adding multiple values in one column is a very bad practice with some serious drawbacks (ex: hard to search, can't be indexed, much manual handling when adding/updating/removing etc). Read up on [database normalization](https://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/) instead. Basically, you create one table with students, one table with classes and one. many-to-many, table with class_id and student_id. Then every user can belong to multiple classes and one class can have multiple students. – M. Eriksson Mar 22 '20 at 14:22
  • @Fox That is a bad recommendation, https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – user3783243 Mar 22 '20 at 14:29
  • @Fox, how can I do it? can you provide an answer? – Madara Code Mar 22 '20 at 14:31
  • 2
    The first suggestion is a bad solution that will come back and bite you sooner or later. Do it correctly from the start instead. – M. Eriksson Mar 22 '20 at 14:35
  • @MagnusEriksson - can you discuss briefly, cause I did not get it complete what you would like to happen the right way? cause I have student and classes table already but i dont know how to connect them\ – Madara Code Mar 22 '20 at 14:38
  • Read the page I linked to in my first comment. After that, do some more research about it. You can search for "database normalization". – M. Eriksson Mar 22 '20 at 14:40
  • @MagnusEriksson - I need your help, Do you mean in the student table ill create a field called `class_id` and the `code_id` ? therefore, the groups will be stored there? – Madara Code Mar 22 '20 at 14:44
  • @MadaraCode if u want working in two tabels u need create on table of all students and add there column of joined, default joined=0 and if student joined so u update to 1 very simply:) – Fox Mar 22 '20 at 16:11
  • see 'Many to Many' relationship... 'Link Table'... Maybe useful? [Here are some real-world examples of the types of relationships](https://stackoverflow.com/a/36631276/3184785) – Ryan Vincent Mar 22 '20 at 16:14
  • @user3783243 You're right! But for starters, this is a simple way to apply what he asked for ... – Fox Mar 22 '20 at 16:15
  • @Fox thank you, for understand things for starters like me. – Madara Code Mar 22 '20 at 16:17
  • @MadaraCode Do you understand what I wrote or do you need more help? – Fox Mar 22 '20 at 16:25
  • @Fox I am already trying to make my tables join so I will not implode or insert ids into one field. I am just having trouble for some time but can you help me a new question i have its just verifying if I have the data exists or not? thanks – Madara Code Mar 22 '20 at 16:28

0 Answers0