我不知道为什么结果是返回重复的行。这段代码试图获取每个coloum&row数据。
$n=array();
$s=array();我声明了将数据颜色移动到变量的数组
$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发布于 2015-04-28 07:06:15
由于您的2 foreach loops of $dom->find位于单个while loop中,因此您基本上是在迭代符合标准(td[...])的所有DOM元素。那些循环在while的循环中没有依赖性,所以我想说它没有效率。
此外,您对$s、$n结果使用常量键,这样就可以得到相同的结果。
为了获得更好的性能和工作解决方案,您应该将这两个foreach循环放在while循环之外(及以上)。因此,$s和$n数组将包含所有所需的元素,在主循环中只需设置一个计数器并增加它。
请记住,为了获得以下两个元素,您应该执行如下操作:$s[$i]、$s[$i+1]。
确保检查$i和$i+1是否在数组的限制范围内。
https://stackoverflow.com/questions/29862871
复制相似问题