drsxobip501258 2018-02-27 09:05
浏览 44
已采纳

循环中的SQL查询累积每个循环

I am building a student-following system where instructors can post the results for the different demands per diploma in SQL. Then, the parents are able to view those results.

In this code the parents can see the results. The $kindid is filled in when they open the page.

The code works like a charm, exept for one thing. Every time the loop is repeated, it echo's the results from the previous loop(s) first, and then the new ones. (See picture)

Problem The first two loops are displayed here, in the field "Opmerking" you can see, the first two results in the second loop are the same as in the first loop.

How do I fix this?

I tried resetting the pointer (How do I reset a PHP pointer for a MySQL resource?), didn't do anything. Also, I tried resetting $row at the beginning of every loop, didn't do anything.

Code:

<?

$diplomanumber = 0; //Start at diploma number 0
$i = 0;
$times_to_run = 7; //Amount diplomas
$array = array();

while ($i++ < $times_to_run)
{ 

$query = "SELECT * FROM Leerlingvolgsysteem WHERE kindid='$kind_id' AND diploma='$diplomanumber' ORDER BY datum DESC;";
$result = mysqli_query($connection,$query);

if(mysqli_num_rows($result) == 0){
    $diplomanumber++; //No results, skip to next loop 
    }

    else {  
        echo'<h2 id="'.$diplomanumber.'">';                 //Echo diploma name
        if($diplomanumber == 0){echo'Spetter Ready';} 
        elseif($diplomanumber == 1){echo'Spetter 1';}
        elseif($diplomanumber == 2){echo'Spetter 2';}
        elseif($diplomanumber == 3){echo'Spetter 3';}
        elseif($diplomanumber == 4){echo'Diploma A';}
        elseif($diplomanumber == 5){echo'Diploma B';}
        elseif($diplomanumber == 6){echo'Diploma C';}
        echo'</h2>';

        while ($row = mysqli_fetch_assoc($result))
        { 
            $phpdate = strtotime($row['datum']);
            $date = date( 'd-m-Y', $phpdate ); //Put date in European format

            $datum .= '<td><b>'.$date.'</b></td>';                              //Define all <td></td> which are looped
            $eis1 .= '<td class="'.$row['eis1'].'">'.$row['eis1'].'</td>';
            $eis2 .= '<td class="'.$row['eis2'].'">'.$row['eis2'].'</td>';
            $eis3 .= '<td class="'.$row['eis3'].'">'.$row['eis3'].'</td>';
            $eis4 .= '<td class="'.$row['eis4'].'">'.$row['eis4'].'</td>';
            $eis5 .= '<td class="'.$row['eis5'].'">'.$row['eis5'].'</td>';
            $eis6 .= '<td class="'.$row['eis6'].'">'.$row['eis6'].'</td>';
            $eis7 .= '<td class="'.$row['eis7'].'">'.$row['eis7'].'</td>';
            $eis8 .= '<td class="'.$row['eis8'].'">'.$row['eis8'].'</td>';
            $eis9 .= '<td class="'.$row['eis9'].'">'.$row['eis9'].'</td>';
            $eis10 .= '<td class="'.$row['eis10'].'">'.$row['eis10'].'</td>';
            $eis11 .= '<td class="'.$row['eis11'].'">'.$row['eis11'].'</td>';
            $eis12 .= '<td class="'.$row['eis12'].'">'.$row['eis12'].'</td>';

            if(!empty($row['opm'])) {
               $opm .= '<td style="white-space: normal;"><b>'.$row['opm'].'</b></td>';
            }
            else{
                $opm .= '<td style="white-space: normal;">&nbsp;</td>';         // No $opm.
            }

            if(!empty($row['instructeur'])) {
               $inst .= '<td style="white-space: normal;"><b>'.$row['instructeur'].'</b></td>';
            }
            else{
            $inst .= '<td style="white-space: normal;">&nbsp;</td>';            // No $inst
            }

        }

        echo '
        <div class="volgsysteem"><table class="table table-condensed table-bordered neutralize volgsysteem"> 
        <tbody>';                                                                                                   //Start table with a div and tbody


        $query2 = "SELECT * FROM Leerlingvolgsysteem_eisen WHERE diploma='$diplomanumber' ORDER BY nr;";                //Query to select all demands

        $result2 = mysqli_query($connection,$query2);
        if(mysqli_num_rows($result2) == 0){
            $diplomanumber++; echo 'Er is een fout opgetreden, waardoor de eisen voor dit diploma niet aan uw kind gekoppeld kunnen worden.</br>';}
        else {
            echo'
            <tr><th class="headcol"><b>Datum:</b></th>'.$datum.'</tr>';
            echo '<th class="headcol"><b>Instructeur</b></th>'.$inst.'</tr>';

                $counter = 0;
                while ($row2 = mysqli_fetch_assoc($result2))    
                {
                    $counter++;         // counter + 1

                    echo '
                    <tr><th class="headcol">'; 

                    echo substr($row2['eis'],0,15);         // shorten demand
                    echo '...';

                    $verandernaameisnr = 'eis' . $counter;  // change number of the demand
                    $eis = $$verandernaameisnr;

                    echo '</th>'.$eis.'';                   // echo demand

                    echo' </tr>';
                }

            echo "<th class='headcol'><b>Opmerking</b></th>";
            echo ''.$opm.'';
            echo' </tr>';
            $diplomanumber++; // Diplomanumber +1
        }

        echo'</tbody>';             //End table
        echo '</table></div> ';

    }

}

?>
  • 写回答

1条回答 默认 最新

  • doulingqiu4349 2018-03-04 09:51
    关注

    Turned out, it worked by resetting all variables which are set with a ".=". So by using "reset($datum);" and all others.

    But I started over building this script, because I want to make a better script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭