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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.