php csv 在提交时跳过一行
php csv skips one line on submit
代码似乎有问题,结果被跳过了一行。
比如我写:642641
结果应该是:“642641”,"testgatan 1"
但相反,它显示:“762755”,"testgatan 2"
我该如何修复才能真正提交输入?
我有一个link让你明白我的意思:http://snaland.com/herestheidnummer/test.html
这是 csv:
ID,Gata
"642641","testgatan 1"
"762755","testgatan 2"
"346468","testgatan 3"
"114564","testgatan 4"
"758925","testgatan 5"
我使用了 Fred -ii-
Find if a value exist in a CSV file with PHP 中的 php 代码
并修改为:
<?php
$search = $_GET['subject'];
$lines = file('http://snaland.com/herestheidnummer/anlaggningsnmr.csv');
$line_number = false;
while (list($key, $line) = each($lines) and !$line_number) {
$line_number = (stripos($line, $search) !== FALSE);
}
if($line_number){
echo "Found result: " .$line;
}
else{
echo "Can't find result: " .$search;
}
?>
Html 形式:
<form name="form" action="http://snaland.com/herestheidnummer/verifiera.php" method="get">
<input type="text" name="subject" id="subject" value="000000">
<input type="submit" value="Submit">
</form>
你的问题是while循环中的条件。在检查 !$line_number 之前执行对键和列表的赋值。如果你像这样交换两个条件,它应该可以工作
while (!$line_number and list($key, $line) = each($lines) ) {
$line_number = (stripos($line, $search) !== FALSE);
}
每次都推进数组游标,因此当您找到结果时,下一个值已经加载。更多信息 http://php.net/manual/en/function.each.php.
一个更简单的解决方案是将循环替换为:
for($i = 0; $i<count($lines);$i++){
if(stripos($lines[$i], $search) !== false){
$line = $lines[$i];
break;
}
}
如果:
if($line){
echo "Found result: " .$line;
}
代码似乎有问题,结果被跳过了一行。
比如我写:642641 结果应该是:“642641”,"testgatan 1" 但相反,它显示:“762755”,"testgatan 2"
我该如何修复才能真正提交输入?
我有一个link让你明白我的意思:http://snaland.com/herestheidnummer/test.html
这是 csv:
ID,Gata
"642641","testgatan 1"
"762755","testgatan 2"
"346468","testgatan 3"
"114564","testgatan 4"
"758925","testgatan 5"
我使用了 Fred -ii-
Find if a value exist in a CSV file with PHP 中的 php 代码并修改为:
<?php
$search = $_GET['subject'];
$lines = file('http://snaland.com/herestheidnummer/anlaggningsnmr.csv');
$line_number = false;
while (list($key, $line) = each($lines) and !$line_number) {
$line_number = (stripos($line, $search) !== FALSE);
}
if($line_number){
echo "Found result: " .$line;
}
else{
echo "Can't find result: " .$search;
}
?>
Html 形式:
<form name="form" action="http://snaland.com/herestheidnummer/verifiera.php" method="get">
<input type="text" name="subject" id="subject" value="000000">
<input type="submit" value="Submit">
</form>
你的问题是while循环中的条件。在检查 !$line_number 之前执行对键和列表的赋值。如果你像这样交换两个条件,它应该可以工作
while (!$line_number and list($key, $line) = each($lines) ) {
$line_number = (stripos($line, $search) !== FALSE);
}
每次都推进数组游标,因此当您找到结果时,下一个值已经加载。更多信息 http://php.net/manual/en/function.each.php.
一个更简单的解决方案是将循环替换为:
for($i = 0; $i<count($lines);$i++){
if(stripos($lines[$i], $search) !== false){
$line = $lines[$i];
break;
}
}
如果:
if($line){
echo "Found result: " .$line;
}