Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP says bad argument for implode but still works!!

Status
Not open for further replies.

gmail2

Programmer
Jun 15, 2005
987
IE
OK here's basically what I've done:
1. Uploaded csv file and opened it using fopen
2. Read entire contents into an array ($fileContent)
3. Use a variable to say how many fields should be required for each record going into the table (eg 4)
4. Loop through the $fileContent array and for each loop check to make sure it contains the correct amount of fields
5. If it contains correct amount then insert into mysql
6. If it doesn't then I want to implode it using "," as delimiter and put that as one record into an errors table for the user to review

My problem is that although the implode function does what I want it to do it still says Warning: implode(): Bad arguments. Why is this - the function is working but it still says this. I think it might have something to do with the fact that the array is multidimensional and I'm saying implode(",",$fileContent[$i]) where $i is the iteration of the loop (if that makes sense !!). But I can't say implode(",",$fileContent) because I only want that one "record" from the array to be imploded. If I use this it returns Array. But as I said, the way I'm doing it works just that it returns the warning also !!

Sorry if this sounds confusing - it's just that my code is quiet big so I wanted to avoid posting it if possible. I'd appreciate any help anybody can provide.

Thanks
 
Your logic is correct on how to do the implode so i think we need the code to be able to debug it (and perhaps some sample data - feel free to send by email if you want[justin.adie AT mlegal.net].

the code below works for me (outputs to the screen and to files rather than a db but ...

Code:
<?
$filename = "c:/dummydb.csv"; //insert file name
$errfile = "c:/errfile.txt"; //ditto
$table= "inserttablename";//insert table name
$fh= fopen($filename, "rb");
$fh2 = fopen ($errfile, "wb");
$fileContents=array();
$headers = array();
$row = 0;
while ($data = fgetcsv($fh, 1000000, ",")):
  if ($row === 0  && count ($data)===4)
  {
     $header = $data;
  }
  elseif(!is_array($data) || count($data) != 4)
  {
	$d[] = array("row"=> $row, "values" => implode(",", $data));
    
	fwrite ($fh2, implode(",", $d[count($d)-1])) or die ("something wrong with error file write");  //NB make sure you give write permissions to the relevant folder
  }
  else
  { 
	  echo "<div style=\"background-color:lightblue;\"><pre>";
	  print_r($data);
	
	  $sql = "Insert into $table set ".$header[0] ." = ".$data[0]. ", ".$header[1] ." = ".$data[1]. ", " .$header[2] ." = ".$data[2]. ", ".$header[3] ." = ".$data[3];
	   echo "<br/>$sql";
		#   mysql_query($sql); // do the db write here. make sure to include the connection params somewhere
	   echo "</pre></div><br/>  ";
	}
	$row++;
endwhile;

echo "<div style=\"background-color:red\"><fieldset><legend>Errors</legend><table><tr><td>Row</td><td>values</td></tr>";
foreach ($d as $var):
echo "<tr><td>".$var['row']."</td><td>".$var['values']."</td></tr>";
endforeach;
echo "</table></fieldset></div>";
?>
 
Once again you come to the rescue !! Yea - at the moment I'm just trying to echo it out. Here's the code right from where it starts iterating through the loop
Code:
for($i = 0; $i < $recordCount; $i ++)
        {
                if(count($fileContent[$i]) == $requiredFields)
                        {
                                $insertQuery = "INSERT INTO ".$table." VALUES (";
                                        for($f = 0; $f < $requiredFields; $f ++)
                                                {
                                                        $insertQuery .= "\"";
                                                        $insertQuery .= $fileContent[$i][$f];
                                                        if($f == ($requiredFields - 1))
                                                                {
                                                                        $insertQuery .= "\"";
                                                                } else {
                                                                        $insertQuery .= "\",";
                                                                }
                                                }
                                        $insertQuery .= ")";
                                //echo $insertQuery."\n";
                                if(mysql_query($insertQuery) == False)
                                        {
                                                $failedInsert = implode(",",$fileContent[$i]);
                                                echo $failedInsert;
                                        }
                        } else {
                                                $corruptRecord = implode(',',$fileContent[$i]);
                                                echo "incorrect no of fields (".count($fileContent[$i]).") on record ".$corruptRecord."\n";
                                                

                        }
        }

Reading the values into the array etc is working fine now (thanks to you !!) but it's in this part that I'm getting stuck.
 
i'll take a look at the code. does my version work on your csv file? if so it will narrow down the search!
 
ok - it's failing on the last insert because it's a blank line and therefore not an array. test the condition with adding an "if_array($fileContent[$i])" before the implode function.

 
one other thought - neither of us are calling fgetcsv properly (ie testing for false). i'd suggest changing the call for

Code:
while (($fileContents[] = fgetcsv($filehandle, 1000000, ",")) !== FALSE):
endwhile;

this will then test at the beginning of the while loop whether it has hit the end of a file which should obviate the need for is_array testing, although it is better to test for all eventualities!

sorry not to spot this earlier!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top