Jump to content


Photo

[SOLVED] Ignore blank lines from text file


  • Please log in to reply
5 replies to this topic

#1 Nandini

Nandini

    Advanced Member

  • Members
  • PipPipPip
  • 160 posts

Posted 07 May 2009 - 08:52 AM

Hi

I am exporting my database values in to text file while click on some link. I am getting a text file with the database values. But i am getting first line as a blank line. I have used trim() function. But no use. I want to export with out any blank lines. how can i do this?

Here is my code:

$text_dt=date("Y-m-d-H-i-s");
$file_name=$text_dt;
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$file_name.'.txt"');
$sql_rate=mysql_query("select * from lcdial_rates'");
while($row=mysql_fetch_array($sql_rate))
{
echo trim($row['prefix']." ".trim($row['rate']);
}

pls help me out.

#2 taith

taith

    Advanced Member

  • Members
  • PipPipPip
  • 1,514 posts
  • LocationCanada

Posted 07 May 2009 - 08:54 AM

while($row=mysql_fetch_array($sql_rate)){
if(empty($row[prefix])||empty($row[rate])) continue;
echo trim($row['prefix']." ".trim($row['rate']);
}
90% of computer issues exist between the seat, and the keyboard.

#3 Ken2k7

Ken2k7

    Advanced Member

  • Members
  • PipPipPip
  • 5,197 posts

Posted 07 May 2009 - 08:59 AM

Why not just do that in the SQL?

SELECT * FROM lcdial_rates WHERE prefix <> '' AND rate <> '';

A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.


#4 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,266 posts
  • LocationBelgium

Posted 07 May 2009 - 09:02 AM

This function already exists:
http://be.php.net/ma...nction.file.php

use as:

<?php
$fileLines = file('filename.txt', FILE_SKIP_EMPTY_LINES);
?>


#5 Nandini

Nandini

    Advanced Member

  • Members
  • PipPipPip
  • 160 posts

Posted 08 May 2009 - 01:41 AM

Thanx for ur reply.

In database there no empty lines. So no need fire query as  SELECT * FROM lcdial_rates WHERE prefix <> '' AND rate <> '' and check values are empty or not.

And

I am not reading a file. just i am exporting database values into text file. That means downloading a text file. I dont have empty line in database table. but i got first line as empty in text file.

#6 Nandini

Nandini

    Advanced Member

  • Members
  • PipPipPip
  • 160 posts

Posted 08 May 2009 - 02:13 AM

Hi all

Problem was solved. This problem was occured about header content type.

Thanx for all reply.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com