douweiluo0600 2016-04-29 11:10 采纳率: 0%
浏览 167
已采纳

foreach循环不断将前一个结果添加到每个表行的下一个结果中

I am trying to create a table that shows the date of a training plan, the plan its self and the members of the group that can attend. The first two are no problem but its who can attend that is the issue. The attendees is stored as a string in the database and explode it to an array but when i use a foreach loop to display the arrays it adds the previous row to the next.

The code I have is:

    $sql_group_name = mysqli_query($db_conx,"SELECT groupName,accepted FROM maingroup");
    while ($row = mysqli_fetch_array($sql_group_name)){
$accepted= $row["accepted"];
$groupName = $row["groupName"];
$accept_array = explode(",", $accepted);
if (in_array($u,$accept_array)){
    $sql = "SELECT * FROM training WHERE groupName='$groupName' ORDER BY sessDate ASC";
    $training_query = mysqli_query($db_conx, $sql);

    while ($row = mysqli_fetch_array($training_query, MYSQLI_ASSOC)) {
        $id = $row["id"];
        $date = $row["sessDate"];
        $session = htmlspecialchars_decode(stripslashes($row['plan']), ENT_QUOTES);
        $groupName = $row["groupName"];
        $attending = $row["attending"];
        $attend_array = explode(",",$attending);
        foreach($attend_array as $value){
            $attendList .=  "" .$value. "<br/>";
        } 

        if (in_array($u,$attend_array)){
            $attendBtn = '<form name="sessUnAttend" id="sessUnAttend" method="post">
                                <input type="submit" value="Unattend">
                                <input type="hidden" name="id" id="id" value="' .$id. '">
                                <input type="hidden" name="unattend" id="unattend">
                                </form>';

        } else { $attendBtn = '<form name="sessAttend" id="sessAttend" method="post">
                                <input type="submit" value="Attend">
                                <input type="hidden" name="id" id="id" value="' .$id. '">
                                <input type="hidden" name="attend" id="attend">
                                </form>';

    }

        $sessTable .= '<tr>
                        <td>' .$date. '</td>
                        <td>' .$session. '</td>     
                        <td>' .$groupName. '</td>
                        <td>' .$attendList. '</td>
                        <td>' .$attendBtn. '</td>
                        </tr>';
    }
}
    }

the result I get is:

Date | Plan | Attending

4.4.16 | some plan | me

5.4.16 | some plan | me, me

6.4.16 | some plan | me, me

Obviuosly "me" can only attend once. It would keep repeating me,me until some one else said they could attend and then add their name and repeat that until some one else said the could attend and so on.

I have read that the while loop should be mysqli_fetch_assoc instead of mysqli_fetch_array but that seems to mess every thing else up.

  • 写回答

2条回答 默认 最新

  • doufen3838 2016-05-02 00:10
    关注

    The comment from @MarkBaker did the trick with some more help from a friend.

    All i had to do was reset the $attendList variable.

         $sessTable .= '<tr>
                        <td>' .$date. '</td>
                        <td>' .$session. '</td>     
                        <td>' .$groupName. '</td>
                        <td>' .$attendList. '</td>
                        <td>' .$attendBtn. '</td>
                        </tr>';
                        $attendList = ''; 
    

    What was happening was the variable was being set from the previous loop and then it just stayed as that until more got added to it.

    What happens now is each time the loop finishes it resets the variable so the new id starts from scratch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵