View Full Version : As easy as A1, B2, C3?
06-22-2010, 10:03 PM
I want to sell things online. My supplier provides me a link to a tab-delimited ASCII file I can tap into for current stock and pricing.
"ITEM" "NAME" "DESCRIPTION" "BRAND" "TITLE" "GENDER" "RETAIL" "PRICE" "IMAGE" "URL" "QTY"
When I load it all in Excel, it separates all the items nice and neat with "ABC" columns and "123" rows.
Is there a way to do this in PHP where if I need certain information from that ASCII page, all I have to do is somehow tell it I want the information in cell B7 or F6041 and it will be included on the website.
I'm hoping for something fairly easy.
06-22-2010, 10:18 PM
Can you use CSV (comma separated variables) instead?
That would be easier than tabs.
If your supplier had their database online and provided an API,
that would be the best way. You'll discover that they won't keep
their actual stock and Excel file the same (or current). This will
cause orders to be placed, but inventory will be zero (back-ordered).
Customers will assume the items are in stock. Very awkward.
06-22-2010, 10:25 PM
Yah, there's no way directly as far as I know. Think about it this way.
When your brain wants information from B7, it (usually) scans for row B, then for column 7 (or for some people, the other way around). PHP would react the same way. It would have to read the content till it reaches B7. This would basically require testing each row/column to test if it is B and 7.
On the other hand, if you have access to a DB, this would work out perfectly from a database, where you could select by ID with ease, or an API like mlseim suggested.
From experience: using files as databases can be hectic, messy, and very very annoying... just delimiting becomes an issue.
06-22-2010, 10:27 PM
Assuming you don't want to bother with a database and want to stick with file manipulation, here's how I'd do it:
1. Dump the file into an array
2. loop through the array and explode each line into fields
3. You now have a 2-dimensional matrix just like a spreadsheet. Access any element of data on this array using the array indexes. For example, F6041 would be accessed with $itemArray. You could even use a letter as the array index when you explode each record, so you could refer to $itemArray['F'].
$fileArray = file("itemfile.txt");
foreach ($fileArray as $key => $val)
//assumes there are 10 fields in each record
, $itemArray['J'][$key]) = explode(chr(9), $val);
06-22-2010, 10:38 PM
What you do is use file() to load this into an array (unless its too big, then we need to iterate it). Each entry of file will be that "ITEM" "NAME"... and so forth. We can then explode that on spaces, but if any of the items in here can have unencoded spaces we need to use preg_split in order to do this (just noticed you said tab delimited, thats fine in an explode("\t", $fileline). This gives us another array.
PHP uses 0 based arrays. So, to get the first entry would be $itm for example, and to get the four column (brand) would be $item. These can also be iterated to allow line by line output and line by line descriptions of each.
One thing I would instead check (for simplicity on yourself) is how often this is updated. If its like once per day, you may be better off scanning this into a temporary database table and querying it when you need data. This would make lookups much much faster.
Aw jeez. I really gotta stop leaving my browser open when I step out >.<
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.