简体   繁体   English

PHP CSV文件查找帮助

[英]PHP csv file lookup assistance

One of our company websites has a page that runs a bit of code that reads a csv file and dependant on the serial number input, returns a list of manuals for that part. 我们公司的一个网站上有一个页面,该页面运行一些代码,该代码读取一个csv文件,并取决于输入的序列号,并返回该部分的手册列表。 Currently the csv file has just the two columns, one for the serial number, the other for the part number to match up to the similarly named pdf in the folder. 目前,csv文件只有两列,一列是序列号,另一列是零件号,以匹配文件夹中类似命名的pdf。 We moved to Sage 200 recently and have a new list of part numbers (for the existing parts) and would like to be able to add a third column with these new part codes. 我们最近搬到了Sage 200,并有了一个新的零件编号列表(对于现有零件),并且希望能够在第三列中添加这些新的零件代码。

I inherited this site and know very little php if any at all so thought maybe you guys could point me in the right direction. 我继承了这个网站,并且几乎不了解php,所以以为你们可能会指出正确的方向。 :) :)

The code I can find on the page is: 我可以在页面上找到的代码是:

@$serial = $_POST['serialnobox'];
echo "You searched for serial $serial";
$file = nl2br(file_get_contents("./tkmanuals/serialnos.csv"));
if($strposition = strpos($file, "$serial")) {
    $strposition=$strposition+7;
    $br = strpos(substr($file, "$strposition", "50"),"
");
    $tspart = substr($file, "$strposition", "$br");
    $tspart = trim($tspart);
    echo "This is Track System: $tspart.
";
}
else
{
    echo "You searched for: $serial. Search Again.

Any help or pointers would be much appreciated. 任何帮助或指针将不胜感激。

If going to a database isn't an option, the following php code (adapted from example 1 at http://us2.php.net/fgetcsv ) may give you a start: 如果没有选择进入数据库,那么以下php代码(改编自http://us2.php.net/fgetcsv的示例1)可以为您提供一个起点:

@$serial = $_POST['serialnobox'];
$row = 1;
if (($handle = fopen("./tkmanuals/serialnos.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($data[0]=="$serial") {
            echo "The first number is $data[1] and the second number is $data[2]<br>";
        }
    }
    fclose($handle);
}

How you want to handle these is up to you. 您要如何处理这些取决于您。 I can't tell from your code if the serial number is a string - in that case you should use string comparison, not the == sign, to check for the serial number in the first column data[0] . 我无法从您的代码中确定序列号是否为字符串-在这种情况下,应使用字符串比较而不是==符号来检查第一列data[0]的序列号。

I would reiterate my recommendation to make a database; 我再次建议建立一个数据库。 you can create a simple user interface for the CAD engineer to add / edit parts and part numbers (in other words - "hide" the fact that this is a database from the user). 您可以为CAD工程师创建一个简单的用户界面,以添加/编辑零件和零件编号(换句话说,“隐藏”这是用户的数据库这一事实)。 You are sure to end up with only one copy of the information (plus backups) which is a vital step in data integrity: the moment you have more than one copy of the information, they will get out of sync... 您肯定会最终只获得一份信息(包括备份),这是数据完整性的重要一步:当您拥有一份以上的信息时,它们将不同步...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM