dotelauv682684 2013-11-17 04:14
浏览 38
已采纳

在PHP中填充2D数组

How would I input the same randomized values 27 times into that array $data?

It outputs a correctly formatted table with one row of randomized values, now just need to redo these values 27 times.

The array is two dimensional would that makes things harder?

Any and all help is appreciated very much I am just a beginner.

    <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>
  • 写回答

1条回答 默认 最新

  • dongying9756 2013-11-17 04:35
    关注

    You were almost there.. with a few modifications to your code, you can get the data correctly into the array, and print it... I'm going to add comments to the modified code, so you can see what I changed, and why.

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里