Re: Formatting output



Patrick wrote:
Jerry Stuckle wrote:

Patrick wrote:

Jerry Stuckle wrote:

Patrick wrote:

Hi All,

Using Version 4.3.9 (Stuck with it for now).

Anyhow, I am trying to do formatted output of single digit decimals. I have a form and present you an abbreviated example for getting a month value.

<select class="inform" name="logmonth">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>

and upon submit it runs through another php process

//variable set
$logmonth = $_POST[logmonth];

//and my attempt to format the output
fwrite($out,"%02d\n",$logmonth);

so that it will write to a file

and put a leading 0 in front of single digit months.

Example, for February the output to the file would be 02.

The fwrite doesn't seem to work.

Any suggestions would be appreciated.

Thanks


That's because fwrite() doesn't accept a formatting string. You write a string; the third parameter is optional and is the number of bytes to write.

You need fprintf() instead.


fprintf doesn't seem to work for me. It stops things in its tracks and does nothing through the rest of the script beginning at that line.

//I have it as
fprintf($out,"%02d\n",$logmonth);

Is this right?


Enable all errors and display them. On your development system, your php.ini file should always have:

error_reporting=E_ALL
display_errors=on

Sounds to me like J.O. has the right idea.


Wow, I turned on display_errors and Boom! Got some, but there was this fatal one,

Fatal error: Call to undefined function: fprintf() in /<snip>/PostDepProcess.php on line 147

So it is telling me that there is a problem with

fprintf($out,"%02f\n",$logmonth);

although I have been told it looks right. Why would it be an undefined function?

It's a long time since played with php4, fprintf was introduced in php5, it seems like you have to process the string with sprintf() and then write the output to the file.

fwrite($out,sprintf("%02f\n",$logmonth));

I think that should fix your trouble.


--

//Aho
.



Relevant Pages

  • Re: File I/O and Out of range or non-integer values truncated during conversion
    ... I don't see the benefit of attempting to use line in fprintf here. ... or else it is '' (the empty string). ... Note that the calling sequence for fprintf is usually ... be converted by the format string. ...
    (comp.soft-sys.matlab)
  • Re: Historical question, why fwrite and not binary specifier for fprintf?
    ... unix write, but no fwrite. ... standard C function fprintf to allow binary operations? ... possible if the format string is not a string literal. ...
    (comp.lang.c)
  • Re: Formatting output
    ... I am trying to do formatted output of single digit decimals. ... //and my attempt to format the output ... You write a string; the third parameter is optional and is the number of bytes to write. ... You need fprintf() instead. ...
    (alt.php)
  • Re: Formatting output
    ... I am trying to do formatted output of single digit decimals. ... //and my attempt to format the output ... You need fprintf() instead. ... Why would it be an undefined function? ...
    (alt.php)
  • Re: Date format problem
    ... >>> dim str as String ... >>> there a function for this if the return date is in string ... > My problem is the date format can change i.t may look like ... make sure the sample date uses a single digit month and day so we ...
    (microsoft.public.vb.general.discussion)