L
L
LP-DIMAN2015-08-25 11:14:52
MySQL
LP-DIMAN, 2015-08-25 11:14:52

Why does the loop enter the same values ​​five times?

public function edit_rating($id,$dateid,$obj){
         

         		$result = $this->get_pupils($id);
             	$pupils = array();
             	foreach ($result as $pupil) 
           		$pupils[] = $pupil['id_user'];

        
           	for($i=0,$j=0;$i<count($pupils),$j<count($this->input->post('rating[]'));$i++,$j++){

          
      

            $this->db->where('id_journal',$id);
            $this->db->where('id_object',$obj);
            $this->db->where('id_pupil',$pupils[$i])
            $this->db->where('date',$dateid);
            $this->db->set('rating',$this->input->post("rating[$j]"));
            $this->db->update('view_journal');
            
          
          }
           }

The code is supposed to update grades for 5 students, but that's not the point. The problem is that he enters data about the last student into the database 5 times. Let's say we have students: Vasya, Petya, Masha, Fedya and Vanya. They have grades: 5,4,3,2,1. The function should add 5 of these students and their grades to the database. But she will contribute only 5 times Vanya and 1, respectively. var_dump output the correct data. I will not understand, where a jamb
Before it wrote similar function of an insertion of the data. Everything is working

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Walt Disney, 2015-08-25
@ruFelix

Your code is a mess. You don't understand what's going on there, much less we.
However:
1) You are using the word "inject" which is most likely $this->db->insert and not $this->db->update
2) The implementation of $this->db is not clear, it looks like you once you reassign where and set for 5 update requests, and the execution of the requests themselves is called somewhere else. Perhaps you should have something like:

$db = $this->db->getNewInstance(); // возможно  $db = $this->db->update('view_journal');
    $db->where('id_journal', $id);
    $db->where('id_object', $objectID);
    $db->where('id_pupil', $pupilID);
    $db->where('date', $date);
    $db->set('rating', $rating);
    $db->update('view_journal');
    $db->exec();

D
Dmitry, 2015-08-25
@Thelema

If var_dump in a loop gives the correct results, then look for an error in the $this->db methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question