Help with an array of pliiz. There is a vote_teach table

 id int(3) teach int(3) ocenka int(1) 

The essence of this. Students are taught by teachers; their number can be 2-3-4-5 and so on. 2 times a year, students evaluate them. I need to make a mold. Need to choose the number of teachers. (For example, selects Digit 5 ​​- gets 5 drop-down select menus). I can do on a fixed number of such forms, without choosing a quantity. It turns out the following algorithm. For each teacher, 2 fields - 1 from the database displays Fio, 2 - point. So, if I do for 3 teachers - 6 fields - 6 variables, I enter in the database as follows:

 $result = mysql_query ("INSERT INTO vote VALUES ('$teach1','$teach1v')"); $result2 = mysql_query ("INSERT INTO vote VALUES ('$teach2','$teach2v')"); $result3 = mysql_query ("INSERT INTO vote VALUES ('$teach3','$teach3v')"); 

Where $ teach1 is the teacher's id, $ teach1v is the teacher's assessment. Question. Is it possible to do this in an associative array? And How? Type if I display the list like this:

 for($i=1; $i<=$count_of_teacher; $i++) {echo '<select name="teach1">'; <?php $result = mysql_query("SELECT id,teach_fam, teach_name FROM teachers"); $myrow = mysql_fetch_array($result2); do { printf ("<option value='%s'>%s %s</option>",$myrow['id'],$myrow["teach_fam"],$myrow["teach_name"]);} while ($myrow = mysql_fetch_array($result)); ?> echo '</select>';} 

Here I understood that there will be 5 drop-down lists. And how to put them in the database as well in the loop? As if in an array of this kind: teach [id] [rating] .

alt text

    1 answer 1

    Somehow messy you described everything ... To begin with, I do not understand why you need a drop-down list of teachers? Why not remove from the database all the names of teachers and against each set a drop-down list with grades? Secondly, it makes several entries at once in one query like this:

     $result = mysql_query ("INSERT INTO vote ('teacher_id', 't_vote') VALUES ('$teach1','$teach1v'), ('$teach2','$teach2v') , ('$teach3','$teach3v')"); 

    Thirdly, in order to get an array of select values ​​from the form, write this:

     <select name="teach_vote_array[id_учителя_из_бд]"> <option value="1">Очень плохо</option> <option value="2">Плохо</option> <option value="3">Удовлетворительно</option> <option value="4">Хорошо</option> <option value="5">Отлично</option> </select> 

    Next, in the handler, we form the request:

     $teach_vote_array = $_POST['teach_vote_array']; // формируем запрос // поля таблицы заменить на свои $query = "INSERT INTO vote ('teacher_id', 't_vote') VALUES "; foreach($teach_vote_array as $key => $val){ $query .= "('$key', '$val'),"; } // удаляем последнюю лишнюю запятую $query = substr($query, 0, strlen($query)-1); // вносим данные в БД $res = mysql_query($query, $db); 
    • The fact is that students usually have 5-6 teachers. Students of 4 courses can have from 3 to 5. And therefore I wanted to do the following algorithm: 1. Choice of the number of teachers 2. Voting (choice of a teacher from the list and rating). This all goes to an assass. Massif of the type vote_teach [id] [ocenka] 3. It is recorded in the database 4. The admin looks at the statistics. - sollex
    • Now I am on point 2. How to write such an array) - sollex
    • I have a feeling that you yourself are confused, and all the jacks have been shuffled in my head)) There comes a 4th year student. For the fourth year, only those teachers are shown who can teach there (for example, five). Let only three have this labuha. Well, so let these three and put the assessment, and the rest does not touch. They will have a default score of NULL. This would mean that he did not study with this teacher, but drank beer at that time. - Deonis