douweiluo0600 2016-04-29 11:10
浏览 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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献