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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示