Jump to content

REPEAT Output ERROR.


php4ever

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/230760-repeat-output-error/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/230760-repeat-output-error/#findComment-1188130
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/230760-repeat-output-error/#findComment-1188134
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.