/    Sign up×
Community /Pin to ProfileBookmark

What I am trying to do is this:

I have a warehouse tracking site. People in the warehouse walk around with PDAs and scan VIN numbers. Once they are done, they generate a txt file with all of the scanned VIN numbers. Here is where I come in. When they upload this file I have to loop through the file and check to see if the VIN is currently in our Units table. If not then I need to give them a list to print out of all of these units that are in the system but not in the warehouse.

With that said, what I have right now I am only echoing the results to see what is going on and of course the echo below is in a for loop so I expect to see some entries more than once (this is just for testing). But what is happening is I am only getting one VIN echoing through the entire for loop and that VIN is in both the system and the file. So it should NOT be assigned to my array.

Here is my code :

[code]
$db = db_connect();

//we open our file
$fp = fopen($upfile, “r”);

//now we check to see if the VIN is already in our system. If not, then we
//put it in our array to notify them

//here we grab all of the VINs currently in MUMs to do our check against the file’s contents
$query = “select Serial_VIN from units”;

$result = @mysql_query($query);

//we initialize our counter for our array at 0
$j = 0;

//now we loop through our query results
for ($i = 0; $i < @mysql_num_rows($result); $i++)
{
//we set/reset our $found var that tells us whether or not we have the VIN in both the file and the units table
$found = false;

//we grab a row
$row = @mysql_fetch_array($result);

//now we rewind the file to the beginning so we can start over
rewind($fp);

//now we loop through until we reach the end of our file
while (!feof($fp))
{
//grab a line from our file
$line = trim(fgets($fp,512));

//if we find the Vin in the file, we set $found to true
if ($row[“Serial_VIN”] == $line)
$found = true;
}

//now if we did not find the VIN in our file, we add the VIN to our array for
//later display
if (!$found)
{
$HTTP_SESSION_VARS[“VIN”][$j] = $line;

echo $HTTP_SESSION_VARS[“VIN”][$j].”<br>”;

//increment array counter
$j++;
}
}

//close file
fclose($fp);

//here we delete the file now that we are done.
unlink($upfile);
[/code]

BTW if you see a less resource taxing way to pull this off, by all means let me know. There could potentially be thousands of records in both the file and the units table. I am not sure my way is the best, because I am looping through the table grabbing a row then looping through my file for each row. I know there has to be an easier way to do this. Maybe the query? Keep in mind I have to know what is in the DB table but not in the file.

Thank you for any help

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 19.2005 — Take a look at this less convoluted (to me! ? ) approach, and see if it works any better:
[code=php]
# read VIN scan results into array:
$scanResults = file() or die("Unable to read in scan results file.");
# strip out newlines:
foreach($scanResults as $key => $value)
{
$scanResults[$key] = trim($value);
}
# do DB connection stuff:
$db = db_connect() or die("Unable to connect to DB.");
# get VINs from DB:
$query = "select Serial_VIN from units";
$result = mysql_query($query) or die("QUERY FAILED: ".mysql_error());
# check each DB result row to see if it's in the scan results:
while($row = mysql_fetch_assoc($result))
{
if(!in_array($row["Serial_VIN"], $scanResults))
{
$notFound[] = $row["Serial_VIN"];
}
}
# output the not-found results:
foreach($notFound as $value)
{
echo "$value<br>n";
}
[/code]
Copy linkTweet thisAlerts:
@tripwaterauthorJul 19.2005 — Thank you. Yes that is much better
Copy linkTweet thisAlerts:
@NogDogJul 19.2005 — In case you didn't catch it yet, I just noticed I left out a closing paren in this line:

while($row = mysql_fetch_assoc($result)[b][color=red])[/color][/b]

(I've updated my original post.)
Copy linkTweet thisAlerts:
@tripwaterauthorJul 19.2005 — I have one more question.

I have to bring the results up in a popup window to allow printing because this site uses frames. How do I get access to the $notfound array so I can print them? I tried setting the results to a session var array and it changed the results.

Sorry if this seems like a stupid question but as you can tell from my original post, I do not use some of the methods you used and I am trying to adapt it to what I need to do next.

I need access to this array on a popup page.
<i>
</i>$notFound[] = $row["Serial_VIN"];


I am using your code and it works. I just need to have access to that array on another page now

Thank you again for your time
Copy linkTweet thisAlerts:
@tripwaterauthorJul 19.2005 — Nevermind. I think I got it. I was right it was a stupid question. Thanks again.
Copy linkTweet thisAlerts:
@NogDogJul 19.2005 — Nevermind. I think I got it. I was right it was a stupid question. Thanks again.[/QUOTE]
LOL - glad it worked. ?
×

Success!

Help @tripwater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...