I have the next:
$students=array('student1','student2','student3','student4');
$date=array('date1','date2','date3');
$students represents the files and $date represents the columns. For each $student and $date there is a value different.
For example:
$students['student1']$dates['date1']='value11';
$students['student1']$dates['date2']='value12';
$students['student1']$dates['date3']='value13';
$students['student2']$dates['date1']='value21';
.... ..... etc.
date1 date2 date3
student1 value11 value12 value13
student2 value21 value22 value23
student3 value31 value32 value33
I want fill the values in a table.
I want to load these values in a grid dynamically.
The count for $students
and $dates
change dynamically.
These values filled in a table I need to insert into database.
I need to update the values into database.
My table in database has the follow fields:
id_tabla estudiante date values
1 estudiante1 date1 value11
2 estudiante1 date2 value12
3 estudiante1 date3 value13
4 estudiante2 date1 value21
5 estudiante2 date2 value22
6 estudiante2 date3 value23
can you help me? Thanks.
The $students is genertated by a query in database. $dates is generated by a range of the dates in php.
The idea is to do a form with text input $students x $dates where the datas should be included. With this form I should get the values datas.
You have quite a lot of requirements and without knowing the context it's not trivial to choose the right data structure. From your example I guess that you have an array with students and an array with dates. The size of the arrays can change, but you always need to attach the dates-array to each student, right?
So based on my assumption, I think you don't need to rearrange the two arrays into one. You can do your enlisted tasks with the given structure.
Outputting each student with dates in a table:
<table>
<?php foreach($students as $student) { ?>
<tr>
<td><?php echo $student; ?></td>
<?php foreach($dates as $date) { ?>
<td><?php echo $date; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Inserting in a database:
<?php
$insert_dates = "'" . implode("','", $dates) . "'";
foreach($students as $student) {
$qry = "INSERT INTO `students` (`name`, `date1`, `date2`, `date3`)
VALUES ('" . $student . "', " . $insert_dates . ")";
//execute query
}
Updating:
<?php
$qry = "UPDATE `students`
SET date1='".$dates[0]."', date2='".$dates[1]."', date2='".$dates[2]."'
WHERE name IN ('" . implode("','", $students) . "')";
//execute query
BUT this approach has some obvious flaws concerning the dynamic changes. So if you are going to work with databases, you should build associative arrays with the keys corresponding to the column names of the table:
$students=array();
$students[] = array('name' => 'student1');
$students[] = array('name' => 'student2');
$students[] = array('name' => 'student3');
$students[] = array('name' => 'student4');
$dates=array();
$dates[] = array('date1'=>'15/04/20', 'date2'=>'20/05/15', 'date3'=>'18/11/10');
The dynamic update query would then look like so:
$set_data = '';
foreach($dates as $key => $value) {
$set_data .= $key . "='".$value."',";
}
$set_data = substr($set_data, 0, -1); //remove last comma
$qry = "UPDATE `students`
SET " . $set_data . "
WHERE name IN ('" . implode("','", $students) . "')";
//execute query
There's a whole lot more you can achieve by playing with data structures (e.g., applying and changing the dates for each student individually, choosing the student-name as the key of the associative array so you can access each student by name, etc.). So at first you should figure out what you need the data for and then choose an appropriate structure based on that. I think you get the idea and I hope it helps!
//UPDATE
Based on your updated question, I think this could be a good approach for the structure (although I still don't know where the data comes from and how/if you can populate that array dynamically/programatically):
$students = array();
$students[] = array(
'name' => 'student1',
'dates' => array(
'date1' => 'code123kil',
'date2' => 'dadfdre145',
)
);
$students[] = array(
'name' => 'student2',
'dates' => array(
'date1' => 'daytetyeyy',
'date2' => 'dafdfe335',
'date3' => 'code123kil'
)
);
//and so on
So now, let's output it into a grid:
<table>
<?php foreach($students as $student) { ?>
<tr>
<td>Name: <?php echo $student['name']; ?></td>
<td>Dates: <?php echo implode(', ', $student['dates']); ?></td>
</tr>
<?php } ?>
</table>
Insert into a table:
<?php
foreach($students as $student) {
foreach($student['dates'] as $key => $value) {
$qry = "INSERT INTO `your_table` (`estudiante`, `date`, `values`)
VALUES ('".$student['name']."','".$key."','".$value."')";
//execute query
}
}
Update the date-values in a table for a given student
<?php
//find the given student in the array
$student_to_update = 'student2';
$student_info = null;
foreach($sudents as $student) {
if($student['name'] == $student_to_update) {
$student_info = $student;
break;
}
}
//update the student in database:
if(!is_null($student_info))
foreach($student_info['dates'] as $key => $value) {
$qry = "UPDATE `your_table`
SET `values`='".$value."'
WHERE `estudiante` = '".$student_info['name']."'
AND `date`='".$key."'";
//execute query
}
}
Update the date-values in a table for each student
<?php
foreach($students as $student) {
foreach($student['dates'] as $key => $value) {
$qry = "UPDATE `your_table`
SET `values`='".$value."'
WHERE `estudiante` = '".$student['name']."'
AND `date`='".$key."'";
//execute query
}
}