【发布时间】:2015-07-03 23:10:50
【问题描述】:
我不知道为什么结果是返回重复的行。 此代码尝试获取每个列和行数据。
$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
【问题讨论】:
-
我相信
foreach循环应该在主循环之外,因为它们不依赖于$matkul。这就像你有一个循环,总是收获相同的页面,但只打印第一个元素。 -
$dom->find 如何在 while 循环的每次迭代中得到不同的结果?
-
谢谢你,它工作。而且重量更轻。
-
我可以把它写成答案,你会批准吗?
-
@OfirBaruch 是的,请。
标签: php loops foreach while-loop fetch