473,804 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fgetcsv() Skip empty rows

code green
1,726 Recognized Expert Top Contributor
I am trying to skip empty rows in a csv file
The manual states
Note:
A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.
But none of these tests seem to work.
Anybody know why
[PHP]while(($data = fgetcsv($this->handle)) !== false)
{
if(!is_null($da ta[0])) #no
if(!empty($data )) #no
if(!empty($data[0])) #no
{[/PHP] And variations thereof
Aug 13 '08 #1
2 17159
Atli
5,058 Recognized Expert Expert
Hi.

You are testing to see if there is a null, right?
That is, if the field is null, you want the if to evaluate true?
If so, why do you flip the result of the if statement?

Testing this on my own, the empty function worked perfectly.
Even a simple boolean check against the empty array element worked.

The is_null function doesn't work, however. This would be due to the fact that it apparently only checks for a variable set with the NULL constant, which the fgetcsv doesn't do for empty cells.

All of this worked for me:
Expand|Select|Wrap|Line Numbers
  1. if($_v == null)
  2. if(empty($_v))
  3. if(!$_v);
  4.  
If that isn't working for you, could there be a problem with the CSV data?

Edit:
Ok, so I kind of missed the point there. Was thinking about empty cells :P

But, with completely empty rows, these three still worked for me:
Expand|Select|Wrap|Line Numbers
  1. if($data[0] != null) {
  2.   # Show row
  3. }
  4. if($data[0]) {
  5.   # Show row
  6. }
  7. if(!empty($data[0])) {
  8.   # Show row
  9. }
  10.  
Aug 14 '08 #2
code green
1,726 Recognized Expert Top Contributor
Thanks for the Edit. You nearly lost me there Atli.
Did some testing. These are the results

[PHP]FAILED
if(!is_null($da ta[0]))
if(!empty($data ))
if(($data))
if(!is_null($da ta))
if($data != null)

WORKED
if(!empty($data[0]))
if($data[0] != null)
if($data[0])
if(!empty($data[0])) [/PHP]
Conclusion
Looks pretty obvious that the first element must be tested.
The NULL value in element 0 means an empty element but a non-empty array.
I didn't realise this was the case.
Don't know whats going on with is_null

I suppose if($data[0] != null) is the safest to use just in case the first column of a row is empty.

ADDITION
[PHP] if($data[0] !== null)
if(!($data[0] === null))[/PHP] Don't work
Aug 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6361
by: Joe Randstein | last post by:
Hi! I get CSV-data from a post-request. How can I work on that? Currently I save the data to a temp-file, then read it back with fgetcsv and then delete the temp-file. That works, but I don't really like it ... Is there a way to directly parse the CSV data from the string (with php-4.2.4) Thanks for your answers!
3
26385
by: puzzlecracker | last post by:
I want to read lines and skip blank lines: would this work considering the lines can contain tabs, spaces, etc.? file.in: ------ line1 line2
5
2328
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following code, sometimes the Person may not have given us one, both, or either of their addresses. The serialize function will still put in a "<BillingAddress />" tag indicating no data. I want it to completely omit the tag if the class is empty, similar...
59
6882
by: AK | last post by:
I tried to google "skip scan DB2" but came up with nothing. Does DB2 have the feature under a different name?
2
6246
by: Stephen Preston | last post by:
I have an xls worksheet I wish to export as a text or csv file to import with fgetcsv() The 'save as' function on excell lets me save as a comma separated value or tab delimited text files. Trouble is, some of my cells already have commas in them, so a tab or something else would be better. In the delimiter option of fgcsv(), can you specify a tab and how? My other option (but a bit of a chore) is to copy and paste the xls data into...
0
1617
by: tjonsek | last post by:
I am working with directories in PHP for the first time. I have code that I've changed multiple times to try different things. I would think this is pretty standard fare so I'm not sure why I can't seem to get it right. What I would like to see happen: The code opens the directory and loops through the files, opening them and processing them.
0
1583
by: et | last post by:
fgetcsv() do unwanted "left trim" on fields started with locale characters (code page win-1250). Problem persist only if local character is first character in field for example, let say that C is locale character, and this is CSV file: BCB;CAAA fgetcsv(CSV file) fill array with:
5
3186
by: mantrid | last post by:
I am using fopen() and fgetcsv() to open an excel file and extract data for upload to mysql database. The doesnt seem to be a parameter in either of these functions for ommitting the first row of the excel file. I am currently using While ...... statement to read the rows and do things with them. Can I ommit the first line without replacing my 'While....' loop with a 'For......' loop? Ian
3
3553
by: jrrdnx | last post by:
I have a form set up to allow someone to upload a .csv and populate its data into a database using the fgetcsv() function. All of the code works fine because everything in the .csv file is populated into the database just fine, except for the very last row of data. I have tried several different ways of using the fgetcsv(), including: while ($result = fgetcsv($handle)) { while (($result = fgetcsv($handle)) != false) { while (($result...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.