Note that there are some explanatory texts on larger screens.

plurals
  1. POParse Comma Delimited File in PHP then Add to DB
    text
    copied!<p>This may be a toughie so you get bonus points!</p> <p><strong>THIS IS A .TXT FILE NOT CSV</strong></p> <p>I am trying to parse the following comma delimited file into a readable table, and then import everything into my table. Here's the info I got:</p> <p>Here's the "template" of what I am importing from. (This is the first line in the file):</p> <pre><code> "Public/Private","Record Manager","Company","Contact","Address 1","Address 2","Address 3","City","State","Zip","Country","ID/Status","Phone","Fax","Home Phone","Mobile Phone","Pager","Salutation","Last Meeting","Last Reach","Last Attempt","Letter Date","Title","Assistant","Last Results","Referred By","User 1","User 2","User 3","User 4","User 5","User 6","User 7","User 8","User 9","User 10","User 11","User 12","User 13","User 14","User 15","Home Address 1","Home Address 2","Home City","Home State","Home Zip","Home Country","Alt Phone","2nd Contact","2nd Title","2nd Phone","3rd Contact","3rd Title","3rd Phone","First Name","Last Name","Phone Ext.","Fax Ext.","Alt Phone Ext.","2nd Phone Ext.","3rd Phone Ext.","Asst. Title","Asst. Phone","Asst. Phone Ext.","Department","Spouse","Record Creator","Owner","2nd Last Reach","3rd Last Reach","Web Site","Ticker Symbol","Create Date","Edit Date","Merge Date","E-mail Login","E-mail System" </code></pre> <p>Here's a sample of what someone would normally enter:</p> <pre><code> "Public","John Doe”,"Einstein Construction","Bill Gates","3441 State Route 1","","","Somecity","CA","15212","","","724-555-2135","","","","","Jill","","4/18/2011","11/23/2009","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","Bill","Gregor","235","","","","","","","","","","Tom Jones","SuperMart","","","www.website.com","","11/14/2009","4/19/2011","","","" </code></pre> <p>Here's the fields I have in the database (MySQL): (obviously without the dollar signs.)</p> <pre><code> $rep $date $account $areacode $number $address1 $address2 $city $state $zip $country $fax $descmaker1 $descmaker2 $title $email $cvendor $cequipment $leaseexp1 $leaseexp2 $leaseexp3 $leaseexp4 $leaseexp5 $leaseexp6 $volume $notes $lastchange </code></pre> <p>Here's what I need them mapped to.. (My field on right, the files on the left)</p> <pre><code> Record Manager -&gt; $rep (record manager i'd rather use our variable which is = $session-&gt;userinfo['fullname'] Create Date -&gt; $date (can the "/" be removed so its MMDDYYYY) Company -&gt; $account Phone (can you explode the areacode off?) -&gt; $areacode Phone (and put the number after the areacode here?)-&gt; $number Address 1 -&gt; $address1 Address 2 -&gt; $address2 City -&gt; $city State -&gt; $state Zip -&gt; $zip Country -&gt; $country Fax -&gt; $fax Salutation -&gt; $descmaker1 $descmaker2 Title -&gt; $title Email Login -&gt; $email Edit Date -&gt; $lastchange </code></pre> <p>What I have right now (that really doesn't work) is below. It shows the data in a big jumble.</p> <pre><code> &lt;?php $a = file_get_contents( "upload/contacts.txt" ); $a = str_replace( array( "\r\n", "\t") , array( "[NEW*LINE]" , "[tAbul*Ator]" ) , $a); print "&lt;table border=\"1\"&gt;"; foreach( explode( "[NEW*LINE]" , $a ) AS $lines){ echo "&lt;tr&gt;"; foreach( explode( "[tAbul*Ator]" , $lines ) AS $li ) { echo "&lt;td&gt;"; echo $li ; } echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload