Search Results

Search found 30 results on 2 pages for 'fgetcsv'.

Page 1/2 | 1 2  | Next Page >

  • PHP - fgetcsv - Delimiter being ignored?

    - by KnockKnockWhosThere
    I'm trying to output each line in a csv file, and it seems like the delimiter is being ignored... I'm sure my syntax is wrong somewhere, but can't seem to pinpoint it... The CSV file looks like this: ID,Code,Count TM768889,02001,10 TM768889,02002,10 TM768889,02003,10 TM768889,02004,10 TM768889,02005,10 I'm trying to output: 0 - ID,Code,Count 1 - TM768889,02001,10 2 - TM768889,02002,10 3 - TM768889,02003,10 4 - TM768889,02004,10 5 - TM768889,02005,10 But instead, it's outputting this: 0 - ID 1 - Code 2 - Count TM768889 3 - 02001 4 - 10 TM768889 5 - 02002 6 - 10 TM768889 7 - 02003 8 - 10 TM768889 9 - 02004 10 - 10 TM768889 11 - 02005 12 - 10 Here's my code: $row = 0; if(($handle = fopen($_FILES["Filedata"]["tmp_name"], "r")) !== FALSE) { $string = ''; while(($line = fgetcsv($handle,1000,",")) !== FALSE) { $num = count($line); $row++; for($c=0; $c < $num; $c++) { $string .= $c.' - '.$line[$c].'<br />'; } } fclose($handle); echo $string; }

    Read the article

  • How to use PHP fgetcsv to create an array for each piece of data in csv file?

    - by Olivia
    I'm trying to import data from a csv file to some html code to use it in a graph I have already coded. I'm trying to use PHP and fgetcsv to create arrays for each separate piece of data to use PHP to put it into the html code. I know how to open the csv file and to print it using PHP, and how to print each separate row, but not each piece of data separated by a comma. Is there a way to do this? If this helps, this is the csv data I am trying to import. May 10,72,12,60 May 11,86,24,62 May 12,67,32,34 May 13,87,12,75 May 14,112,23,89 May 17,69,21,48 May 18,98,14,84 May 19,115,18,97 May 20,101,13,88 May 21,107,32,75 I hope that makes sense.

    Read the article

  • formatting fgetcsv output

    - by Patrick
    Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table so far i have this <?php $row = 1; $handle = fopen("csv.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "\n"; } } fclose($handle); ?>

    Read the article

  • using fopen on uploaded file with php

    - by Patrick
    Im uploading a file, and then attempting to use fgetcsv to do things to it. This is my script below. Everything was working fine before i added the file upload portions. Now its not showing any errors, its just not displaying the uploaded data. Im getting my "file uploaded" notice, and the file is saving properly, but the counter for number of entries is showing 0. if(isset($_FILES[csvgo])){ $tmp = $_FILES[csvgo][tmp_name]; $name = "temp.csv"; $tempdir = "csvtemp/"; if(move_uploaded_file($tmp, $tempdir.$name)){ echo "file uploaded<br>"; } $csvfile = $tempdir.$name; $fh = fopen($csvfile, 'r'); $headers = fgetcsv($fh); $data = array(); while (! feof($fh)) { $row = fgetcsv($fh); if (!empty($row)) { $obj = new stdClass; foreach ($row as $i => $value) { $key = $headers[$i]; $obj->$key = $value; } $data[] = $obj; } } fclose($fh); $c=0; foreach($data AS $key){ $c++; $phone = $key->PHONE; $fax = $key->Fax; $email = $key->Email; // do things with the data here } echo "$c entries <br>"; }

    Read the article

  • Parse a CSV file extracting some of the values but not all.

    - by Yallaa
    Good day, I have a local csv file with values that change daily called DailyValues.csv I need to extract the value field of category2 and category4. Then combine, sort and remove duplicates (if any) from the extracted values. Then save it to a new local file NewValues.txt. Here is an example of the DailyValues.csv file: category,date,value category1,2010-05-18,value01 category1,2010-05-18,value02 category1,2010-05-18,value03 category1,2010-05-18,value04 category1,2010-05-18,value05 category1,2010-05-18,value06 category1,2010-05-18,value07 category2,2010-05-18,value08 category2,2010-05-18,value09 category2,2010-05-18,value10 category2,2010-05-18,value11 category2,2010-05-18,value12 category2,2010-05-18,value13 category2,2010-05-18,value14 category2,2010-05-18,value30 category3,2010-05-18,value16 category3,2010-05-18,value17 category3,2010-05-18,value18 category3,2010-05-18,value19 category3,2010-05-18,value20 category3,2010-05-18,value21 category3,2010-05-18,value22 category3,2010-05-18,value23 category3,2010-05-18,value24 category4,2010-05-18,value25 category4,2010-05-18,value26 category4,2010-05-18,value10 category4,2010-05-18,value28 category4,2010-05-18,value11 category4,2010-05-18,value30 category2,2010-05-18,value31 category2,2010-05-18,value32 category2,2010-05-18,value33 category2,2010-05-18,value34 category2,2010-05-18,value35 category2,2010-05-18,value07 I've found some helpful parsing examples at http://www.php.net/manual/en/function.fgetcsv.php and managed to extract all the values of the value column but don't know how to restrict it to only extract the values of category2/4 then sort and clean duplicate. The solution needs to be in php, perl or shell script. Any help would be much appreciated. Thank you in advance.

    Read the article

  • PHP CSV, how to get previous and next 3 rows from current row

    - by Kalpesh Mehta
    I am currently using fgetcsv in php to get rows in CSV file. For each row, I need it's 3 previous rows and 3 next rows for some requirement. while(($data = fgetcsv($handle, 5000, ",")) !== FALSE) { //$data is current row data //I need previous rows and next rows data } I don't know how to try this, as each while iterate will be having the current row's information. Is there anything we can achieve this within the fgetcsv loop? I am also open for other alternatives for this problem.

    Read the article

  • unexpected T_TRY, expecting T_FUNCTION error message, not sure why ?

    - by Rachel
    I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as too why am getting that, can't we use try and catch block inside class like this: class Processor { protected $dao; protected $fin; try { public function __construct($file) { //Open File for parsing. $this->fin = fopen($file,'w+') or die('Cannot open file'); // Initialize the Repository DAO. $this->dao = new Dao('some name'); } public function initiateInserts() { while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) { $initiate_inserts = $this->dao->initiateInserts($data); } } public function initiateCUpdates() { while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) { $initiate_updates = $this->dao->initiateCUpdates($data); } } public function initiateExecuteIUpdates() { while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) { $initiate_updates = $this->dao->initiateIUpdates($data); } } } catch (Exception $e) { } }

    Read the article

  • Application Code Redesign to reduce no. of Database Hits from Performance Perspective

    - by Rachel
    Scenario I want to parse a large CSV file and inserts data into the database, csv file has approximately 100K rows of data. Currently I am using fgetcsv to parse through the file row by row and insert data into Database and so right now I am hitting database for each line of data present in csv file so currently database hit count is 100K which is not good from performance point of view. Current Code: public function initiateInserts() { //Open Large CSV File(min 100K rows) for parsing. $this->fin = fopen($file,'r') or die('Cannot open file'); //Parsing Large CSV file to get data and initiate insertion into schema. while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) { $query = "INSERT INTO dt_table (id, code, connectid, connectcode) VALUES (:id, :code, :connectid, :connectcode)"; $stmt = $this->prepare($query); // Then, for each line : bind the parameters $stmt->bindValue(':id', $data[0], PDO::PARAM_INT); $stmt->bindValue(':code', $data[1], PDO::PARAM_INT); $stmt->bindValue(':connectid', $data[2], PDO::PARAM_INT); $stmt->bindValue(':connectcode', $data[3], PDO::PARAM_INT); // Execute the statement $stmt->execute(); $this->checkForErrors($stmt); } } I am looking for a way wherein instead of hitting Database for every row of data, I can prepare the query and than hit it once and populate Database with the inserts. Any Suggestions !!! Note: This is the exact sample code that I am using but CSV file has more no. of field and not only id, code, connectid and connectcode but I wanted to make sure that I am able to explain the logic and so have used this sample code here. Thanks !!!

    Read the article

  • Parsing CSV File to MySQL DB in PHP

    - by Austin
    I have a some 350-lined CSV File with all sorts of vendors that fall into Clothes, Tools, Entertainment, etc.. categories. Using the following code I have been able to print out my CSV File. <?php $fp = fopen('promo_catalog_expanded.csv', 'r'); echo '<tr><td>'; echo implode('</td><td>', fgetcsv($fp, 4096, ',')); echo '</td></tr>'; while(!feof($fp)) { list($cat, $var, $name, $var2, $web, $var3, $phone,$var4, $kw,$var5, $desc) = fgetcsv($fp, 4096); echo '<tr><td>'; echo $cat. '</td><td>' . $name . '</td><td><a href="http://www.' . $web .'" target="_blank">' .$web.'</a></td><td>'.$phone.'</td><td>'.$kw.'</td><td>'.$desc.'</td>' ; echo '</td></tr>'; } fclose($file_handle); show_source(__FILE__); ?> First thing you will probably notice is the extraneous vars within the list(). this is because of how the excel spreadsheet/csv file: Category,,Company Name,,Website,,Phone,,Keywords,,Description ,,,,,,,,,, Clothes,,4imprint,,4imprint.com,,877-466-7746,,"polos, jackets, coats, workwear, sweatshirts, hoodies, long sleeve, pullovers, t-shirts, tees, tshirts,",,An embroidery and apparel company based in Wisconsin. ,,Apollo Embroidery,,apolloemb.com,,1-800-982-2146,,"hats, caps, headwear, bags, totes, backpacks, blankets, embroidery",,An embroidery sales company based in California. One thing to note is that the last line starts with two commas as it is also listed within "Clothes" category. My concern is that I am going about the CSV output wrong. Should I be using a foreach loop instead of this list way? Should I first get rid of any unnecessary blank columns? Please advise any flaws you may find, improvements I can use so I can be ready to import this data to a MySQL DB.

    Read the article

  • How to validate csv file ?

    - by Rachel
    How can we validate a CSV file ? I have an CSV file of structure: Date;Id;Shown 15-Mar-10;231;345 15-Mar-10;232;346 and so on and on !!! approx around 80,000 rows. How can I validate this CSV file before starting the parsing using fgetcsv ?

    Read the article

  • ob_start() -> ob_flush() doesn't work

    - by MB34
    I am using ob_start()/ob_flush() to, hopefully, give me some progress during a long import operation. Here is a simple outline of what I'm doing: <?php ob_start (); echo "Connecting to download Inventory file.<br>"; $conn = ftp_connect($ftp_site) or die("Could not connect"); echo "Logging into site download Inventory file.<br>"; ftp_login($conn,$ftp_username,$ftp_password) or die("Bad login credentials for ". $ftp_site); echo "Changing directory on download Inventory file.<br>"; ftp_chdir($conn,"INV") or die("could not change directory to INV"); // connection, local, remote, type, resume $localname = "INV"."_".date("m")."_".date('d').".csv"; echo "Downloading Inventory file to:".$localname."<br>"; ob_flush(); flush(); sleep(5); if (ftp_get($conn,$localname,"INV.csv",FTP_ASCII)) { echo "New Inventory File Downloaded<br>"; $datapath = $localname; ftp_close($conn); } else { ftp_close($conn); die("There was a problem downloading the Inventory file."); } ob_flush(); flush(); sleep(5); $csvfile = fopen($datapath, "r"); // open csv file $x = 1; // skip the header line $line = fgetcsv($csvfile); $y = (feof($csvfile) ? 2 : 5); while ((!$debug) ? (!feof($csvfile)) : $x <= $y) { $x++; $line = fgetcsv($csvfile); // do a lot of import stuff here with $line ob_flush(); flush(); sleep(1); } fclose($csvfile); // important: close the file ob_end_clean(); However, nothing is being output to the screen at all. I know the data file is getting downloaded because I watch the directory where it is being placed. I also know that the import is happening, meaning that it is in the while loop, because I can monitor the DB and records are being inserted. Any ideas as to why I am not getting output to the screen?

    Read the article

  • Function returns CSV File: How to go about checking it ?

    - by Rachel
    I am doing something like: $outputFile = getCurrentDBSnapshot($data); where $data is the resource stream that am passing in, basically from command prompt am passing an file and am opening it using fopen for writing with 'w+' permissions, now getCurrentDBSnapshot would get the current state of a table and would update the $data csv file, so basically $outputFile would be updated with the current state of database table, now I want to var_dump or print the value of $outputFile to see the data present into it. But when I do $this->fout = fopen($outputFile,'r') or die('Cannot open file'); $test = fgetcsv($outputFile,5000,":"); var_dump($test); It gives me an error saying that it expects parameter 1 to be a string type and am passing resource. My goal to see the contains of $outputFile and so my question is that How can I see the contains present in $outputFile or how can I see what getcurrentDBSnapshot function is returning me ?

    Read the article

  • CSV file read fail (PHP )

    - by user1020069
    I am trying to read a csv file (delimited by commas) but unfortunately, it isn't responding as it ought to. I am not so sure what I am doing wrong here, but I'll paste out the contents of the code and the CSV file both : $row = 0; if($handle = fopen("SampleQuizData.csv","r") !== FALSE) { // WORKS UNTIL HERE, SO FILE IS BEING READ while(!feof(handle)){ $line = fgetcsv($handle, 1024, ",") ; echo $line[2]; // DOES NOT WORK } } And the CSV file is (the emails and names have been changed here to protect the identities of the users) parijat,something,[email protected] matthew,durp, [email protected] steve,vai,[email protected] rajni,kanth,[email protected]

    Read the article

  • PHP Fingerprinting CMS Versions by their meta tags [migrated]

    - by Mud
    Hey guys I'm having some issues with the speed of my script. I'm a novice I know so getting past that - what suggestions would you have to speed up my script? I was originally just reading in the index.php and then searching the <head> of the page for an array of strings. Then I read about the get_meta_tags and went that way. Then I had issues with some sites having 300 redirects in place so I used curl to check the URL existed and to speed up things but it's still taking 5 minutes or so to execute. <?php function url_exist($url){ $c=curl_init(); curl_setopt($c,CURLOPT_URL,$url); curl_setopt($c,CURLOPT_HEADER,1); curl_setopt($c,CURLOPT_NOBODY,1); curl_setopt($c,CURLOPT_RETURNTRANSFER,1); curl_setopt($c,CURLOPT_FRESH_CONNECT,1); if(!curl_exec($c)){ return false; }else{ return true; } curl_close($c); } function checkVersion($url){ $tags = get_meta_tags($url); if (is_array($tags) && array_key_exists('generator', $tags)) { $v = "<span style='background-color:#7BF55D;color:#A3A0A0'>".$tags['generator']."</span"; }else{ $v="<span style='background-color:#F55D67;color:#A3A0A0'>Metatag not found!</span>"; } return $v; } $row = 1; echo "<table>"; if (($handle = fopen("url.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++; for ($c=0; $c < $num; $c++) { if(url_exist($data[$c])){ echo "<tr><td>".$data[$c]."</td><td>".checkVersion($data[$c])."</td></tr>"; sleep(2); }else{ echo "<tr><td>".$data[$c]."</td><td><td><span style='background-color:#F55D5D;color:#A3A0A0'>URL not valid!<span></td></tr>"; } } } fclose($handle); } echo "</table>"; ?>

    Read the article

  • Working with a CSV file with odd encapsulation // PHP

    - by Patrick
    I have a CSV file that I'm working with, and all the fields are comma separated. But some of the fields themselves, contain commas. In the raw CSV file, the fields that contain commas, are encapsulated with quotes, as seen here; "Doctor Such and Such, Medical Center","555 Scruff McGruff, Suite 103, Chicago IL 60652",(555) 555-5555,,,,something else the code I'm using is below <?PHP $file_handle = fopen("file.csv", "r"); $i=0; while (!feof($file_handle) ) { $line = fgetcsv($file_handle, 1024); $c=0; foreach($line AS $key=>$value){ if($i != 0){ if($c == 0){ echo "[ROW $i][COL $c] - $value"; //First field in row, show row # }else{ echo "[COL $c] - $value"; // Remaining fields in row } } $c++; } echo "<br>"; // Line Break to next line $i++; } fclose($file_handle); ?> The problem is I'm getting the fields with the commas split into two fields, which messes up the number of columns I'm supposed to have. Is there any way I could search for commas within quotes and convert them, or another way to deal with this?

    Read the article

  • Recursively MySQL Query

    - by Rachel
    How can I implement recursive MySQL Queries. I am trying to look for it but resources are not very helpful. Trying to implement similar logic. public function initiateInserts() { //Open Large CSV File(min 100K rows) for parsing. $this->fin = fopen($file,'r') or die('Cannot open file'); //Parsing Large CSV file to get data and initiate insertion into schema. $query = ""; while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) { $query = $query + "INSERT INTO dt_table (id, code, connectid, connectcode) VALUES (" + $data[0] + ", " + $data[1] + ", " + $data[2] + ", " + $data[3] + ")"; } $stmt = $this->prepare($query); // Execute the statement $stmt->execute(); $this->checkForErrors($stmt); } @Author: Numenor Error Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1 This Approach inspired to look for an MySQL recursive query approach.

    Read the article

  • define keys in multidimensional array from csv

    - by mourique
    I want to compare two arrays, one coming from a shoppingcart and the other one parsed from a csv-file. The array from the shopping cart looks like this: Array ( [0] => Array ( [id] => 7 [qty] => 1 [price] => 07.39 [name] => walkthebridge [subtotal] => 7.39 ) [1] => Array ( [id] => 2 [qty] => 1 [price] => 07.39 [name] => milkyway [subtotal] => 7.39 ) ) The array from my csv-file however looks like this Array ( [0] => Array ( [0] => 1 [1] => walkthebridge [2] => 07.39 ) [1] => Array ( [0] => 2 [1] => milkyway [2] => 07.39 ) ) and is build using this code $checkitems = array(); $file = fopen('checkitems.csv', 'r'); while (($result = fgetcsv($file)) !== false) { $checkitems[] = $result; } fclose($file); how can i get the keys in the second array to match those in the first one? ( So that 0 would be id, and 1 would be name and so on) thanks in advance

    Read the article

  • Using a function found in a different file in a loop

    - by Anders
    This question is related to BuddyPress, and a follow-up question from this question I have a .csv-file with 790 rows and 3 columns where the first column is the group name, second is the group description and last (third) the slug. As far as I've been told I can use this code: <?php $groups = array(); if (($handle = fopen("groupData.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $group = array('group_id' = 'SOME ID', 'name' = $data[0], 'description' = $data[1], 'slug' = groups_check_slug(sanitize_title(esc_attr($data[2]))), 'date_created' = gmdate( "Y-m-d H:i:s" ), 'status' = 'public' ); $groups[] = $group; } fclose($handle); } foreach ($groups as $group) { groups_create_group($group); } With http://www.nomorepasting.com/getpaste.php?pasteid=35217 which is called bp-groups.php. The thing is that I can't make it work. I've created a new file with the code written above called groupgenerator.php uploaded the .csv file to the same folder and opened groupgenerator.php in my browser. But, i get this error: Fatal error: Call to undefined function groups_check_slug() in What am I doing wrong?

    Read the article

  • working with a csv with odd encapsulation // php

    - by Patrick
    I have a CSV file that im working with, and all the fields are comma separated. But some of the fields themselves, contain commas. In the raw csv file, the fields that contain commas, are encapsulated with quotes, as seen here; "Doctor Such and Such, Medical Center","555 Scruff McGruff, Suite 103, Chicago IL 60652",(555) 555-5555,,,,something else the code im using is below <?PHP $file_handle = fopen("file.csv", "r"); $i=0; while (!feof($file_handle) ) { $line = fgetcsv($file_handle, 1024); $c=0; foreach($line AS $key=>$value){ if($i != 0){ if($c == 0){ echo "[ROW $i][COL $c] - $value"; //First field in row, show row # }else{ echo "[COL $c] - $value"; // Remaining fields in row } } $c++; } echo "<br>"; // Line Break to next line $i++; } fclose($file_handle); ?> The problem is im getting the fields with the comma's split into two fields, which messes up the number of columns im supposed to have. Is there any way i could search for comma's within quotes and convert them, or another way to deal with this?

    Read the article

  • csv file upload and update mysql db

    - by Indra
    I am very to new to php. i am using the following code to open a csv file and update my database. i need to check the value of first row-first column of the csv file. if it is matching "some text 1", then i need to run code1, if it is "some text 2", run code2, else code3. I can use if else condition but since i am using while loop It fails. Can anyone help me. $handle = fopen($file_tmp,"r"); while(($fileop = fgetcsv($handle,",")) !== false) { // I need to check here $companycode = mysql_real_escape_string($fileop[0]); $Item = mysql_real_escape_string($fileop[3]); $pack = preg_replace('/[^A-Za-z0-9\. -]/', '', $fileop[4]); $lastmonth = mysql_real_escape_string($fileop[5]); $ltlmonth = mysql_real_escape_string($fileop[6]); $op = mysql_real_escape_string($fileop[9]); $pur = mysql_real_escape_string($fileop[10]); $sale = mysql_real_escape_string($fileop[12]); $bal = mysql_real_escape_string($fileop[17]); $bval = mysql_real_escape_string($fileop[18]); $sval = mysql_real_escape_string($fileop[19]); $sq1 = mysql_query("INSERT INTO `sas` (companycode,Item,pack,lastmonth,ltlmonth,op,pur,sale,bal,bval,sval) VALUES ('$companycode','$Item','$pack','$lastmonth','$ltlmonth','$op','$pur','$sale','$bal','$bval','$sval')"); }

    Read the article

  • PHP : If...Else...Query

    - by Rachel
    I am executing this statement under while (($data=fgetcsv($this->fin,5000,";"))!==FALSE) Now what I want in else loop is to throw exception only for data value which did not satisfy the if condition. Right now am displaying the complete row as I am not sure how to throw exception only for data which does not satisfy the value. Code if ((strtotime($data[11]) &&strtotime($data[12])&&strtotime($data[16]))!==FALSE && ctype_digit($data[0]) && ctype_alnum($data[1]) && ctype_digit($data[2]) && ctype_alnum($data[3]) && ctype_alnum($data[4]) && ctype_alnum($data[5]) && ctype_alnum($data[6]) && ctype_alnum($data[7]) && ctype_alnum($data[8]) && $this->_is_valid($data[9]) && ctype_digit($data[10]) && ctype_digit($data[13]) && $this->_is_valid($data[14])) { //Some Logic } else { throw new Exception ("Data {$data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $data[10], $data[11], $data[12], $data[13], $data[14], $data[16]} is not in valid format"); } Guidance would be highly appreciated as to how can I throw exception only for data which did not satisfy the if value.

    Read the article

1 2  | Next Page >