在PHP中填充2D数组
如何将相同的随机值27次输入到数组$data中 它输出一个格式正确的表,其中包含一行随机值,现在只需将这些值重做27次 数组是二维的,这会让事情变得更难吗 非常感谢所有的帮助,我只是一个初学者在PHP中填充2D数组,php,arrays,2d,Php,Arrays,2d,如何将相同的随机值27次输入到数组$data中 它输出一个格式正确的表,其中包含一行随机值,现在只需将这些值重做27次 数组是二维的,这会让事情变得更难吗 非常感谢所有的帮助,我只是一个初学者 <html> <STYLE type="text/css"> td{ border-left: 1px solid #f09d09; } th{ border: 1px solid #f09d09; } &l
<html>
<STYLE type="text/css">
td{
border-left: 1px solid #f09d09;
}
th{
border: 1px solid #f09d09;
}
</STYLE>
<body>
<table>
<tr>
<th>Number</th>
<th>Student Number</th>
<th>Coursework Mark</th>
<th>Exam Mark</th>
<th>Module Mark</th>
<th>Module Result</th>
<th>Comments</th>
</tr>
<?php
$examPassmark = 40; //Hardcoded exam pass mark
$courseworkPassmark = 40; // Hardcoded coursework passmark
$n = rand(1,27);
$sn = "B00" . rand(200000,599999); //randomised student number with the prefix B00 e.g B00-299999
$cwm = rand(25,100); //randomised coursework mark
$em = rand(25,100); // randomised exam mark
$mm = round(((($cwm / 200) * 20) + (($em / 200) * 80) * 2)) ; //exam weighting is cw/e = 20/80
$mr = 'Fail';
$stack = array("");
if($em > $examPassmark && $cwm > $courseworkPassmark) //This if statement states that ONLY if both Coursework and the exam are passed will a student pass the module
{
$mr = 'Pass';
}else{
$mr = 'Fail';
}
if($cwm < $courseworkPassmark) //Checks to see if the student passed coursework
{
$com = 'Resit CourseWork';
}
else if($em < $examPassmark) // Checks to see if the student passed the exam
{
$com = 'Resit Exam';
}else{
$com = 'None'; //outputted if both are passed
}
for($i = 0; $i <= 27; $i++)
{
$data = array( array($n, $sn, $cwm, $em, $mm, $mr, $com) //Here we have an two dimensional array that will be filled with the values created above
);
$data[$i] .= $stack;
}
for ($row = 0; $row < 27; $row++) //rows (I use 8 to give each column padding so it isnt squeezed together)
{
for ($col = 0; $col < 7; $col++) //columns output to the number of entries in the array $data
{
echo "<td>".$data[$row][$col]."</td>"; //within each column print out the value held within $data
}
}
echo '</table>'; //end the table
?>
</body>
运输署{
左边框:1px实心#f09d09;
}
th{
边框:1px实心#f09d09;
}
数
学号
课程成绩
考试成绩
模块标记
模块结果
评论
你就快到了。。只需对代码进行一些修改,就可以将数据正确地放入数组,并将其打印出来。。。我将在修改后的代码中添加注释,这样您就可以看到我更改了什么,以及更改的原因
for($i = 0; $i <= 27; $i++)
{
$data[] = array($n, $sn, $cwm, $em, $mm, $mr, $com, $stack);
// There is no need to use $i in the array assignment here, the array inserts already start at zero
// On top of that, there is no need to have a 3 dimensional array, if you're trying to
// Get the values to correctly print into a table.
// Lastly, you can simply add $stack to the array, rather than having to add it on with another line.
}
for ($row = 0; $row < 27; $row++)
{
echo "<tr>"; // This needs to exist, of course, in order to separate the rows
for ($col = 0; $col < 7; $col++) //columns output to the number of entries in the array $data
{
echo "<td>".$data[$row][$col]."</td>";
// Otherwise, your code here is fine.
}
echo "</tr>"; // see above
}
用于($i=0;$i作为一个二维数组,我不知道如何增加到数组中的下一个值。然后如何打印该数组的值。我只能希望我没有误解你的目标。棒极了,效果很好。我现在只需要将每个条目随机化,但困难的部分到此为止谢谢!欢迎;很高兴我能提供帮助。还有,至于随机化每一行的数据,你可以把你的作业推到$data
循环中。这正是我所做的。看起来有点循环和冗长,但它很有用,可以清理。再次感谢!