PHP 循环 Foreach + While
PHP loop Foreach + While
我不知道为什么结果是 return 重复行。
此代码尝试获取每一列和每一行数据。
$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
由于您的 $dom->find
中的 2 foreach loops
位于单个 while loop
中,您基本上是在遍历所有满足您条件的 DOM 元素(td[...]
).这些循环在 while 循环中没有依赖性,所以我认为它效率不高。
此外,您对 $s
、$n
结果使用常量键,因此您总是会得到相同的结果。
为了获得更好的性能和有效的解决方案,您应该将这 2 个 foreach 循环放在 while
循环之外(和上方)。所以 $s
和 $n
数组将包含所有需要的元素,在你的主循环中只需设置一个计数器并增加它。
请记住,为了获得 2 个以下元素,您应该执行以下操作:$s[$i]
、$s[$i+1]
.
确保检查 $i
和 $i+1
是否在数组的限制范围内。
我不知道为什么结果是 return 重复行。 此代码尝试获取每一列和每一行数据。
$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
由于您的 $dom->find
中的 2 foreach loops
位于单个 while loop
中,您基本上是在遍历所有满足您条件的 DOM 元素(td[...]
).这些循环在 while 循环中没有依赖性,所以我认为它效率不高。
此外,您对 $s
、$n
结果使用常量键,因此您总是会得到相同的结果。
为了获得更好的性能和有效的解决方案,您应该将这 2 个 foreach 循环放在 while
循环之外(和上方)。所以 $s
和 $n
数组将包含所有需要的元素,在你的主循环中只需设置一个计数器并增加它。
请记住,为了获得 2 个以下元素,您应该执行以下操作:$s[$i]
、$s[$i+1]
.
确保检查 $i
和 $i+1
是否在数组的限制范围内。