Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Question As easy as A1, B2, C3?

    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.

    Thanks bunches!
    <div> - putting your mind in a box since 1997

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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[6][6041]. You could even use a letter as the array index when you explode each record, so you could refer to $itemArray['F'][6041].

    PHP Code:
    $fileArray file("itemfile.txt");
    foreach (
    $fileArray as $key => $val)
    {
        
    //assumes there are 10 fields in each record
        
    list($itemArray['A'][$key]
            , 
    $itemArray['B'][$key]
            , 
    $itemArray['C'][$key]
            , 
    $itemArray['D'][$key]
            , 
    $itemArray['E'][$key]
            , 
    $itemArray['F'][$key]
            , 
    $itemArray['G'][$key]
            , 
    $itemArray['H'][$key]
            , 
    $itemArray['I'][$key]
            , 
    $itemArray['J'][$key]) = explode(chr(9), $val);
    }

    echo 
    $itemArray['F'][6041]; 

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, but.
    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[0] for example, and to get the four column (brand) would be $item[0][4]. 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.

    Edit:
    Aw jeez. I really gotta stop leaving my browser open when I step out >.<
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •