简体   繁体   English

PHP循环Foreach + While

[英]PHP loop Foreach + While

I don't know why the result is return duplicate rows. 我不知道为什么结果会返回重复的行。 This code is try to fetching every coloumn & row data. 这段代码试图获取每个列和行数据。

$n=array();
$s=array();

i declared array for moving data coloumn to variable 我声明了将数据列移动到变量的数组

$i =0;
$x =0;
$matkul = 0;
while ($matkul<$ttlrow){    
        //try to fecth all coloumns data every row.
            foreach($dom->find('td[style="text-align:left;"]') as $b) {
                $n[$x]=$b->plaintext;
                $x++;                    
            }
        //try to show the data before insert to Database
        $k_mk= $n[0];
        $n_mk= $n[1];
        echo $k_mk . ' | ';
        echo $n_mk. ' | ';
            //try to fecth all coloumns data every row.
            foreach($dom->find('td[style="text-align:center;"]') as $a) {
                $s[$i]=$a->plaintext;
                $i++;
            }
        //try to show the data before insert to Database
        $sks= $s[0];
        $grd = $s[1];
        $bbt = $s[2];
        $nl = $s[3];
        $uid = $uid;
        echo $sks . ' | ';
        echo $grd. ' | ';
        echo $nl. '<br>';
        /*
       $sql = "INSERT INTO fokusipk_ks.jadwal (`uid`, `kd_mk`, `kd_sms`,  
       `nm_mk`, `nm_dsn`, `kd_kls`, `hari`, `jam`) 

       VALUES ('$uid', '$mk', '$sms', '$nmk', '$nmd', '$kls', '$hari', 
       '$jam');";
            if ($conn->query($sql) === TRUE) {
                #echo '.';
            }
        */
        $matkul++;
       //refresh the value to re-start fetching from the first coloumn
        $i=0;
        $x=0;
}


Code   | Courses                       | W | G | V

the results something like this:

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

TPLB02 | PENGANTAR TEKNOLOGI INFORMASI | 2 | A | 8

Since your 2 foreach loops of $dom->find are being located in a single while loop you're basically iterating over all the DOM elements that answers your criteria ( td[...] ). 由于$dom->find 2个foreach loops位于单个while loop ,因此基本上可以遍历所有可满足您条件的DOM元素( td[...] )。 Those loops have no dependency in the while's loop so I would say it's not efficient. 这些循环在while循环中没有依赖关系,所以我会说这没有效率。

Moreover, you're using constant keys for the $s , $n results so you'd always get the same results. 此外,您在$s$n结果中使用了常量键,因此您始终可以获得相同的结果。

In order to have a better performance and working solution, you should put those 2 foreach loops outside (and above) the while loop. 为了获得更好的性能和可行的解决方案,您应该将这两个foreach循环放在while循环之外(或之上)。 So the $s and $n arrays would contain all the desired elements and in your main loop just set a counter and increase it. 因此, $s$n数组将包含所有所需的元素,并且在您的主循环中只需设置一个计数器并增加它。

Keep in mind that in order to get 2 following element, you should do something like: $s[$i] , $s[$i+1] . 请记住,为了获得2个以下元素,您应该执行以下操作: $s[$i]$s[$i+1]

Make sure you check that the $i and $i+1 are in the range of the array's limits. 确保检查$i$i+1是否在数组限制的范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM