Question

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. 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. 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.

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. :)

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.

Was it helpful?

Solution

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:

@$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].

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). 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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top