Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading flat file to array and compare to MySQL array

Status
Not open for further replies.

BobLaw

Programmer
Oct 2, 2000
27
0
0
US
Hello,

I am very new to PHP, sorry, but have an issue I am trying to resolve, any help would be appreciated.

I have a unix style flat file filled with item numbers delimited by a LF (OA).
Something like this (has about a thousand values):
234
1286
15678
255
12999

Then I have a MySQL database table that may or may not have these item numbers also in a field of the table.
table products
products_number (this may have the item number)

This is an inventory check, I need to loop over an array of the flat text file and see if each entry is already in the database table field for products_number.

Well I am lost, I tried for about a week now, I used the PHP file command to read the file, this apparently puts the data into an array, when I echo that it just returns "Array".

Then I was able to read and put into an array the data in that MYSQL table, I was able to read them and echo each entry from the table.

But, I have no clue how to pick each item from the flat file, like 1235 and loop over the MySQL array to see if it is there. This is for discontinued/out of stock items, there is another field products_active, value 0/1, if there is a match between the item number in the flat file and a value in the MySQL array I need to change the products_active value to 0 to make it inactive.

If anyone can understand what I am trying to say here and can offer some help I would really apprecite it.

Thank you.



 
I suggest a different approach:

1. Load the flat file.
2. Iterate the flat file and issue a MySQL query and inspect in the result if the item is in the table or not.

Code:
$flatFile = file('whatever.txt');

// iterate the array
foreach ($file AS $line){
   // here comes the code that makes the MySQL query
   $SQL = "SELECT count(*) AS numItem FROM itemTable 
           WHERE item_id=".trim($line);
   $result = mysql_query($SQL) OR die('Query error: '.mysql_error());
   $row = mysql_fetch_assoc($result) OR die('Retrieval error: '.mysql_error());
   // here's the check for existence: If $row['numItem']==0 it is not in there, if it is 1, it is.
   // etc.
 
Thank you DRJ478, I will try it this way and see how it works out, seems a better way than I was trying...
 
Just to let you know, you gave me enough to get this working! Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top