zwlanham Posted March 25, 2013 Share Posted March 25, 2013 Basically we have a script that reads a csv file and breaks the file into words and puts them into rows and columns. Column 7 and 8 need to be handled differently. If it has a certain phrase in it needs to go to column 7, if not, column 8. Then the script needs to continue reading from the log. Currently, the script will read all the column 1-6 and input it into the database....then read column 7 and 8 and it handles it correctly but I need it to be in the same rows as when it first read column 1-6. Then after that, it needs to continue reading column 9-20 and imputing it into the same rows as 1-8. <?php $conn = mysql_connect("localhost", "root", "") or die (mysql_error()); mysql_select_db("syslog",$conn); if(isset($_POST['submit'])) { $file = $_FILES['file']['tmp_name']; $handle = fopen($file,"r"); while(($fileop = fgetcsv($handle,1000, " ")) !== false) { $Column1 = $fileop[0]; $Column2 = $fileop[1]; $Column3 = $fileop[2]; $Column4 = $fileop[3]; $Column5 = $fileop[4]; $Column6 = $fileop[5]; $Column7 = $fileop[6]; $Column8 = $fileop[6]; $mgtid = array("options" => array("regexp" => "/^m(.*)/")); /** if(filter_var($Column7,FILTER_VALIDATE_REGEXP,$mgtid)) { $Column7 = " "; $Column8 = $fileop[6]; } else { $Column8 = " "; $Column7 = $fileop[6]; } **/ if(filter_var($Column7,FILTER_VALIDATE_REGEXP,$mgtid)) { $sql = mysql_query("INSERT INTO events (Column8) VALUES ('$Column8')"); } else { $sql = mysql_query("INSERT INTO events (Column7) VALUES ('$Column7')"); } $Column9 = $fileop[7]; $Column10 = $fileop[8]; $Column11 = $fileop[9]; $Column12 = $fileop[10]; $Column13 = $fileop[11]; $Column14 = $fileop[12]; $Column15 = $fileop[13]; $Column16 = $fileop[14]; $Column17 = $fileop[15]; $Column18 = $fileop[16]; $Column19 = $fileop[17]; $Column20 = $fileop[18]; $sql = mysql_query("INSERT INTO events (Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10, Column11, Column12, Column13, Column14, Column15, Column16, Column17, Column18, Column19, Column20) VALUES ('$Column1', '$Column2', '$Column3', '$Column4', '$Column5', '$Column6', '$Column7', '$Column8', '$Column9', '$Column10', '$Column11', '$Column12', '$Column13', '$Column14', '$Column15', '$Column16', '$Column17', '$Column18', '$Column19', '$Column20')"); } if($sql) { echo 'Data uploaded succesfully'; } } ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> </head> <body> <div id="main"> <form method="post" action="phpfile.php" enctype="multipart/form-data"> <input type="file" name="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/276141-php-read-from-csv-file-handle-a-couple-of-columns-differently/ Share on other sites More sharing options...
davidannis Posted March 25, 2013 Share Posted March 25, 2013 Looks like the code that you have commented out did what your asking and you replaced it. Why? Link to comment https://forums.phpfreaks.com/topic/276141-php-read-from-csv-file-handle-a-couple-of-columns-differently/#findComment-1420992 Share on other sites More sharing options...
Barand Posted March 25, 2013 Share Posted March 25, 2013 if(filter_var($Column7,FILTER_VALIDATE_REGEXP,$mgtid)) { $sql = mysql_query("INSERT INTO events (Column8) VALUES ('$Column8')"); } else { $sql = mysql_query("INSERT INTO events (Column7) VALUES ('$Column7')"); } that's the code that should be commented out Link to comment https://forums.phpfreaks.com/topic/276141-php-read-from-csv-file-handle-a-couple-of-columns-differently/#findComment-1420995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.