dennismonsewicz Posted October 30, 2008 Share Posted October 30, 2008 how do i parse the data in a csv file to read every chunk of data? i want to place the contents of the csv file into an array with the delimiter being a comma Quote Link to comment Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 how do i parse the data in a csv file to read every chunk of data? It's already parsed with commas... comma-separated values. I'm not sure what you're trying to accomplish but this maybe close: $i=0; $handle = @fopen("your_file.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $one = $pieces[0]; $two = $pieces[1]; $three = $pieces[2]; $your_array[$i]=$one.",".$two.",".$three; $i++; } } So you can refer to the array key and explode each line. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 thanks for the code... i will be testing it now... this is a good start at least. I am going to be needing to compare two csv files and where ever there is a match up between the two files it is to insert the contents in to a set of db tables. And the ones that could not match up there is supposed to be a error report generated showing the entries that could not be matched up Quote Link to comment Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Could you post the format of the two different CSV files you're comparing and any the conditions? There may be an easy way to do this using, in_array(), but in your situation it may not work. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 this is the code i just updated and i am getting a blank screen: foreach($_FILES['file']['name'] as $n => $name) { if(!empty($name)) { $target_path = "upload_path/" . $name; if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) { $i=0; $handle = @fopen($name, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $one = $pieces[0]; $two = $pieces[1]; $three = $pieces[2]; $your_array[$i]=$one.",".$two.",".$three; echo $your_array . "<br />"; $i++; } } } } } Quote Link to comment Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Can you debug by printing out variables like $name? Also, do you have display errors on? ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 i get this error message Notice: Undefined offset: 2 Then the word Array is printed all the way down the screen Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 here is what line of code from one of the files looks like 000-100-034,"FEED FRAME INSERT ",83,000-100-034.jpg,328 NEW STYLE,754,"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","" Quote Link to comment Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Then you're going to need to have another while loop inside the EOF while loop to grab all the values. Something like: ***NOT TESTED p.s.- use print_r() to print arrays not echo. $pieces = explode(",", $buffer); $x=0; while($pieces[$x]) { $your_array[$i].=$pieces[$x]; } print_r($your_array); $i++; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 ok so it created an infinite loop... updated code: foreach($_FILES['file']['name'] as $n => $name) { if(!empty($name)) { $target_path = "upload_path/" . $name; if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) { $i=0; $handle = @fopen($target_path, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $x=0; while($pieces[$x]) { $your_array[$i].=$pieces[$x]; } print_r($your_array); $i++; } } } } } Quote Link to comment Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Cause $x is not incremented. Sorry for that, like I said this is not tested. $x=0; while($pieces[$x]) { $your_array[$i].=$pieces[$x]; $x++; } print_r($your_array); $i++; Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 yeah the code just creates an inifinite loop and causes my browser to crash... updated code: foreach($_FILES['file']['name'] as $n => $name) { if(!empty($name)) { $target_path = "Upload/" . $name; if(move_uploaded_file($_FILES['file']['tmp_name'][$n], $target_path)) { $i=0; $handle = @fopen($target_path, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $x=0; while($pieces[$x]) { $your_array[$i].=$pieces[$x]; $x++; } print_r($your_array); $i++; } } } } } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 30, 2008 Share Posted October 30, 2008 Have you looked at the function fgetcsv()? It might simplify your code. Ken Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 ok i have the code update and its reading the file contents updated code: $action = $_GET['action']; $session_id = $_GET['osCAdminID']; $targetPath = 'Upload/'; $randomNumber = rand(1000, 9999); switch($action) { case "upload": foreach($_FILES['file']['tmp_name'] as $key => $value) { if(move_uploaded_file($_FILES['file']['tmp_name'][$key], $targetPath . $randomNumber . '-' . $_FILES['file']['name'][$key])) { $aryFiles[] = $randomNumber . '-' . $_FILES['file']['name'][$key]; } else { $continue = 99; } if($continue == 99) { echo '<strong>There was a problem uploading your files</strong>'; } else { foreach($aryFiles as $key => $file) { $lnct = 0; $fileRead = fopen($targetPath . $file, 'r'); while(!feof($fileRead)) { $fileContent[$key][$lnct] = explode(',',fgets($fileRead)); foreach($fileContent[$key][$lnct] as $subkey => $column) { $quoFix = $column; if(substr($quoFix, 0, 1) == '"') $quoFix = substr($quoFix, 1, (strlen($quoFix)-1)); if(substr($quoFix, (strlen($quoFix)-1), 1) == '"') $quoFix = substr($quoFix, 0, (strlen($quoFix)-1)); if($quoFix) $fileContent[$key][$lnct][$subkey] = $quoFix; } $lnct++; } fclose($fileRead); } } } print_r ($fileContent); Now i want to compare the contents from array 1 and contents from array 2. The file contents are read line for line and placed into an array. $fileContent[0] is one array and $fileContent[1] is another array. I can't use array_diff() cause the column counts are different in each file... any ideas here? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 30, 2008 Author Share Posted October 30, 2008 bump 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.