如何将相同的随机值27次输入到该数组$ data中?
它输出一个格式正确的表,其中包含一行随机值,现在只需重新设置这些值27次。
数组是二维会让事情变得更难吗?
非常感谢任何和所有帮助我只是一个初学者。
<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>
答案 0 :(得分:1)
你几乎就在那里..对你的代码做了一些修改,你可以正确地将数据输入到数组中,然后打印出来......我要为修改后的代码添加注释,这样你就可以看到我改变了,为什么。
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
}