PHP loop Foreach + While -


i don't know why result return duplicate rows. code try fetching every coloumn & row data.

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

i declared array moving data coloumn variable

$i =0; $x =0; $matkul = 0; while ($matkul<$ttlrow){             //try fecth coloumns data every row.             foreach($dom->find('td[style="text-align:left;"]') $b) {                 $n[$x]=$b->plaintext;                 $x++;                                 }         //try show data before insert database         $k_mk= $n[0];         $n_mk= $n[1];         echo $k_mk . ' | ';         echo $n_mk. ' | ';             //try fecth coloumns data every row.             foreach($dom->find('td[style="text-align:center;"]') $a) {                 $s[$i]=$a->plaintext;                 $i++;             }         //try show data before insert database         $sks= $s[0];         $grd = $s[1];         $bbt = $s[2];         $nl = $s[3];         $uid = $uid;         echo $sks . ' | ';         echo $grd. ' | ';         echo $nl. '<br>';         /*        $sql = "insert 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 value re-start fetching first coloumn         $i=0;         $x=0; }   code   | courses                       | w | g | v  results this:  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8  tplb02 | pengantar teknologi informasi | 2 | | 8 

since 2 foreach loops of $dom->find being located in single while loop you're iterating on dom elements answers criteria (td[...]). loops have no dependency in while's loop it's not efficient.

moreover, you're using constant keys $s,$n results you'd same results.

in order have better performance , working solution, should put 2 foreach loops outside (and above) while loop. $s , $n arrays contain desired elements , in main loop set counter , increase it.

keep in mind in order 2 following element, should like: $s[$i], $s[$i+1].

make sure check $i , $i+1 in range of array's limits.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -