php4ever Posted March 15, 2011 Share Posted March 15, 2011 I have a script that opens up a CSV file and outputs to a table for testing. It ultimately inserts into a MySQL but the code below literally duplicates every third row consistently. I'm 95% sure its in the "WHILE" area. Is there enough code here for someone to point out an error. If not I can put the code up live and if you want a fee for fixing just PM me please. $fp = fopen("residential-data.csv", "r"); $listings=-1; function getZip($full) { $pieces = explode(' ', $full); $zip = explode('(', $pieces[count($pieces)-1]); $zip = explode(')', $zip[1]); return $zip[0]; } $pre=""; while (($data = fgets($fp,4096)) !== FALSE) { $data=str_replace("\r\n","",$data); if(trim($data)=="" or substr($data,strlen($data)-1,1)!='"' or substr($data,strlen($data)-2,2)==',"') { $pre.=$data; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 16, 2011 Share Posted March 16, 2011 That is not enough code. The while block isn't closed and we can't see what else the script does. Ken Quote Link to comment Share on other sites More sharing options...
php4ever Posted March 16, 2011 Author Share Posted March 16, 2011 Ok here you go // the column-array.php is just the array matching that in the data.csv. require_once("column-array.php"); $fp = fopen("data.csv", "r"); // getZip not used this version function getZip($full) { $pieces = explode(' ', $full); $zip = explode('(', $pieces[count($pieces)-1]); $zip = explode(')', $zip[1]); return $zip[0]; } $pre=""; while (($data = fgets($fp,4096)) !== FALSE) { $data=str_replace("\r\n","",$data); if(trim($data)=="" or substr($data,strlen($data)-1,1)!='"' or substr($data,strlen($data)-2,2)==',"') { $pre.=$data; } else { $data=$pre.$data; $data=str_replace(array(',""','","','"',"'","html","\r\n"),array("","|","","","PHP"," "),$data); $data=preg_replace("/\r|\n|\v|\r\n/smUi"," ",$data); $data_elements=explode("|",$data); } //////////////////////////////////////////////////////////////////////////// // USED TO ECHO FIELD COLUMNS IN A TABLE - Used For Testing Output //////////////////////////////////////////////////////////////////////////// echo("<table border=2 cellpadding=5 cellspacing=5>"); $i++; echo("<tr>"); foreach($data_elements as $k) { echo("<td valign=top width='20' style='overflow:hidden;'>"); echo($k); echo("</td>"); } echo("</tr>"); echo("</table>"); continue; $listings++; //} } fclose($fp); Quote Link to comment Share on other sites More sharing options...
php4ever Posted March 16, 2011 Author Share Posted March 16, 2011 I posted it here; http://livedemosite.com/fixcsv/testing_full.php If you run the script, it just duplicates the output in a loop of some sort. The idea behind the above script is to take that nasty CSV file and fix the text data by removing all those extra breaks and html code. I know it now works properly for formatting because looking at the 2 left columns I see the main ID and the secondary ID consistently. What doesn't work, is it keeps looping. You can for instance open your FireFox, hit CTRL+F and search for 3966497 and you will see that the output has been produced again and again. There are 200 rows in the CSV, none are the same. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.