douzuita7325 2017-11-23 05:35
浏览 24
已采纳

从可变长度2维数组中发布数据

I am having some issues with posting data from a 2 dimensional array and echoing them.

I have read several topics and tutorials on multidimensional array, but most of them used fixed length of arrays as examples. In my case both the dimensions of the array are variable. The project I am working with is to provide awards to students from various year-levels.

Example: Year-level 3 may have 5 awards and each award is given to variable number of students. Award A to 5 students, Award B to 2 students and so on. Similarly, Year 6 may have 15 awards and so on.

There are tables which has the record for which year-level has which all Awards and the quantity of awards to be allocated each year.

I have been able to display the year levels and their respective awards in one form (in html/php). But I am desperately trying to figure out how to post the data from the 2 dimensional array and echo them (the idea is to insert them into tables if I can echo them).

The list of students are coming from a table (displaying the student name but selecting the student code)

<?php
    include 'db_connect.php';
    $query = mysqli_query($conn, "SELECT a.awardcode, a.year_grp, a.awardqty, b.awardname FROM awardallocation AS a INNER JOIN award AS b
        ON a.awardcode = b.awardcode
        WHERE a.year_grp = '$level' AND b.awardstatus != '0' ORDER BY b.awardname ASC ");

    $row = mysqli_num_rows($query);

    if($row > 0) {

        while ($result = mysqli_fetch_assoc($query)){
            $awardname = trim($result['awardname']);
            $awardcode = trim($result['awardcode']);
            $awardqty  = trim($result['awardqty']);
?>  
            <table>
              <tr>
                <th>Award Name: <?php echo $awardname; ?></th>          
              </tr>

              <tr style="background-color:#FFF9C4">
                    <?php
                    for($i = 0; $i < $awardqty; $i++ ){
                        ?>
                    <td>                
                        <select name="stud_code[$awardcode][$i]">
                            <option disabled selected value> -- Select Student -- </option>
                            <?php
//                          include 'db_connect.php';
                            $sql = mysqli_query($conn, "SELECT stud_code, surname, preferred_name FROM student WHERE year_grp = '$level' ORDER BY surname ASC ");
                            while ($row1 = mysqli_fetch_assoc($sql)){
                                $name = trim($row1['surname']).', '.trim($row1['preferred_name']);
                                echo '<option value = " '.$row1['stud_code'].'"> '.$name.' </option>';
                            } 
                            ?> 
                        </select>   
                    </td>
                    <?php } ?>  
              </tr>

            </table>
<?php           
        }
    }
?>      
        <table>
            <tr>
                <td><lable>Lock data to prevent changes in future?</lable><input type="checkbox" name="lock" value="1"/></td>
            </tr>
        </table>    
            <div class="button-section">            
                <input id="save_button" type="submit" name="submit" value="Save Selection"> 
                <input id="exit_button" type="submit" name="exit" value="Exit"  onclick="window.history.go(-1); return false;">
            </div>  
    </form> 
    </div> 
</body>

<?php

if(isset($_POST['submit'])) {

for ($row=1; $row<=count($_POST ['stud_code["awardcode"]']); $row++){   
    echo $_POST["awardcode"];
        for ($col = 0; $col < 3; $col++){
//      echo("<li>".$_POST['stud_code']['awardcode'][$i]."</li>");  
//$student = $_POST['stud_code[$awardcode][$i]'];

}
}
$lock = (isset($_POST['$lock']) ? $_POST['$lock'] : '0');       
$next_year = date("Y")+1;

}

Please forgive me if the explanation above is not very clear. The code I have written is included.

Any suggestion and help will be highly appreciated as I can have a good night’s sleep after this.. :)

  • 写回答

1条回答 默认 最新

  • douxing8323 2017-11-23 06:07
    关注

    You forgot to echo out within your name attribute keys:

    <select name="stud_code[<?=$awardcode?>][<?=$i?>]">
    

    I'd recommend changing the name to something like award_allocations.

    <select name="award_allocations[<?=$awardcode?>][<?=$i?>]">
    

    Dump $_POST to check it is as you want. You can iterate through your allocations something like this:

    if(isset($_POST['submit']))
    {
        foreach($_POST['award_allocations'] as $award_code => $student_codes) {
            printf("Award code: %s allocated to Student codes: %s
    ",
                $award_code,
                implode(', ', $student_codes)
            );
        }
        $lock = isset($_POST['lock']) ? true : false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗