PHP中的动态数组
我有下一个:
$students=array('student1','student2','student3','student4');
$date=array('date1','date2','date3');
$students 代表文件, $date 代表列。 对于每个 $student 和 $date 都有一个不同的值。
例如:
$students['student1']$dates['date1']='value11';
$students['student1']$dates['date2']='value12';
$students['student1']$dates['date3']='value13';
$students['student2']$dates['date1']='value21';
.... …… 等等。
date1 date2 date3
student1 value11 value12 value13
student2 value21 value22 value23
student3 value31 value32 value33
我想填写表格中的值。 我想动态地将这些值加载到网格中。 $students
和 $dates
的计数动态变化。
这些值填写在我需要插入数据库的表中。 我需要将值更新到数据库中。
我在数据库中的表有以下字段:
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
你能帮助我吗?谢谢。
$students 是通过数据库中的查询生成的。 $dates 由 php 中的一系列日期生成。
这个想法是制作一个带有文本输入 $students x $dates 的表单,其中应包含数据。通过这种形式,我应该获得值数据。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有很多要求,并且在不了解上下文的情况下,选择正确的数据结构并非易事。从你的例子来看,我猜你有一个包含学生的数组和一个包含日期的数组。数组的大小可以更改,但您始终需要将日期数组附加到每个学生,对吧?
因此,根据我的假设,我认为您不需要将两个数组重新排列为一个。您可以使用给定的结构完成您的任务。
在表中输出每个学生的日期:
插入数据库:
更新:
但是这种方法在动态变化方面存在一些明显的缺陷。因此,如果您要使用数据库,则应该使用与表的列名相对应的键构建关联数组:
动态更新查询将如下所示:
通过使用数据结构,您可以实现更多功能(例如,单独应用和更改每个学生的日期,选择学生姓名作为关联数组的键,以便您可以按姓名访问每个学生等) 。因此,首先您应该弄清楚您需要数据的用途,然后据此选择合适的结构。我想您已经明白了,希望对您有所帮助!
//更新
根据您更新的问题,我认为这可能是结构的一个好方法(尽管我仍然不知道数据来自哪里以及如何/是否可以动态填充该数组/programatically):
现在,让我们将其输出到网格中:
插入表格:
更新表格中给定学生的日期值更新
表格中每个学生的日期值
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:
Inserting in a database:
Updating:
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:
The dynamic update query would then look like so:
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):
So now, let's output it into a grid:
Insert into a table:
Update the date-values in a table for a given student
Update the date-values in a table for each student
这有帮助吗:
Does this help: