Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:38:23+00:00 2026-06-03T13:38:23+00:00

I am writing a Text file with php using mysql db fields, which are

  • 0

I am writing a Text file with php using mysql db fields, which are dynamic in nature. I am unable to keep the column width intact, pl see the below data and columns to get a better idea of what I mean.

Text file output:

                450 65445 90900 87954          112
              90900 45875 24565 15484          KA01 23232

php script for the above:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

if ($quer2[DCNO1]>0){fwrite($fh, str_pad($quer2[DCNO1],19, " ", STR_PAD_LEFT));} if ($quer2[DCNO2]>0) {fwrite($fh, str_pad($quer2[DCNO2],6, " ", STR_PAD_LEFT));} if ($quer2[DCNO3]>0) {fwrite($fh, str_pad($quer2[DCNO3],6, " ", STR_PAD_LEFT));} if ($quer2[DCNO4]>0) {fwrite($fh, str_pad($quer2[DCNO4],6, " ", STR_PAD_LEFT));} 
fwrite($fh, str_pad($dchd2['PO_No'],13, " ", STR_PAD_LEFT));
$stringData = "\n";
fwrite($fh, $stringData);

if ($quer2[DCNO5]>0) {fwrite($fh, str_pad($quer2[DCNO5],19, " ", STR_PAD_LEFT));} if ($quer2[DCNO6]>0) {fwrite($fh, str_pad($quer2[DCNO6],6, " ", STR_PAD_LEFT));} if ($quer2[DCNO7]>0) {fwrite($fh, str_pad($quer2[DCNO7],6, " ", STR_PAD_LEFT));} if ($quer2[DCNO8]>0) {fwrite($fh, str_pad($quer2[DCNO8],6, " ", STR_PAD_LEFT));}
fwrite($fh, str_pad($dchd2['Vehicle_no'],20, " ", STR_PAD_LEFT));
$stringData = "\n";
fwrite($fh, $stringData);

fclose($fh);

Now if one value in the column is 0 then the space gets removed because of my IF condition, in such a situation, how do i calculate the space and give those extra spaces.

text file output if one of the values are 0 in my IF condition:

                450 90900           112
              90900 45875 24565 15484          KA01 23232
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:38:25+00:00Added an answer on June 3, 2026 at 1:38 pm

    Use tab stops (“\t”). It ensures each item on different lines start on the same columns.

    It means that when item is 2 chars long and can’t fill the 4 space tab, 2 whitespaces are added.

    Bear in mind: different editors have different notions for tabs, some typically insert spaces instead of tabs.

    Edit: Actually more simple solution will be to have an else clause and write str_pad(“”, 20);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue in writing fixed width text file using php and data
i'm having problem writing to a text file using php. this might sound simple
I tested writing to a text file simply via shell command or php. Using
I'm writing to a text file using the following declaration: void create_out_file(char file_name[],long double
i am writing a viewer using datagridview to load a text file and display
I'm writing a multiple user log in system using PHP and MySql, the way
I'm learning web-centric programming by writing myself a blog, using PHP with a MySQL
Im writing to a file using php, and i have no idea why in
I'm writing a PHP script and the script outputs a simple text file log
I'm writing a web app using php. The main file (index.php) has some Hebrew

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.