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 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle