help putting zero in front of single digits in select

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick

    help putting zero in front of single digits in select

    Hello All,

    Kind of new to PHP. I have an html form where a person inputs a date via
    a drop down select. How do I make php put a zero in front of the numbers
    1-9 ie. 01 02 03 when writing to the file? I have tried making the
    value="01" etc. but this doesn't work. Below is a snippet of my form and
    the code I am using to write to the file. I hope I have made myself
    clear enough. Your help is greatly appreciated.


    //form part
    <select class="inform" name="logday">
    <option value="1">1
    <option value="2">2
    <option value="3">3
    <option value="4">4
    <option value="5">5
    <option value="6">6
    <option value="7">7
    <option value="8">8
    <option value="9">9
    <option value="10">10
    etc,

    //code part
    fputs($out,"$_P OST[logday] ");


    Thanks,
    Patrick
    --
    Patrick A. Smith Assistant System Administrator
    Ocean Circulation Group – USF - College of Marine Science
    http://ocgweb.marine.usf.edu Phone: 727 553-3334

    The trouble with doing something right the first time is that nobody
    appreciates how difficult it was. - La Rochefoucauld


  • Justin Koivisto

    #2
    Re: help putting zero in front of single digits in select

    Patrick wrote:[color=blue]
    > Hello All,
    >
    > Kind of new to PHP. I have an html form where a person inputs a date via
    > a drop down select. How do I make php put a zero in front of the numbers
    > 1-9 ie. 01 02 03 when writing to the file? I have tried making the
    > value="01" etc. but this doesn't work. Below is a snippet of my form and
    > the code I am using to write to the file. I hope I have made myself
    > clear enough. Your help is greatly appreciated.
    >
    >
    > //form part
    > <select class="inform" name="logday">
    > <option value="1">1
    > <option value="2">2
    > <option value="3">3
    > <option value="4">4
    > <option value="5">5
    > <option value="6">6
    > <option value="7">7
    > <option value="8">8
    > <option value="9">9
    > <option value="10">10
    > etc,
    >
    > //code part
    > fputs($out,"$_P OST[logday] ");[/color]

    use something more like:
    fputs($out,spri ntf('%02d',intv al($_POST['logday'])));

    Comment

    • Alan Little

      #3
      Re: help putting zero in front of single digits in select

      Carved in mystic runes upon the very living rock, the last words of
      Patrick of comp.lang.php make plain:
      [color=blue]
      > Kind of new to PHP. I have an html form where a person inputs a date
      > via a drop down select. How do I make php put a zero in front of the
      > numbers 1-9 ie. 01 02 03 when writing to the file? I have tried making
      > the value="01" etc. but this doesn't work.[/color]

      Unless you're doing something in your code to explicitly change the type
      of $logday to numerical, there's no reason that zero-padding in the
      VALUE attribute shouldn't work.

      <?php
      $x = '01';
      echo $x;
      ?>

      Outputs 01

      <?php
      $x = '01';
      $x*= 1;
      echo $x;
      ?>

      Outputs 1

      --
      Alan Little
      Phorm PHP Form Processor

      Comment

      • Patrick

        #4
        Re: help putting zero in front of single digits in select

        Patrick wrote:
        [color=blue]
        > Hello All,
        >
        > Kind of new to PHP. I have an html form where a person inputs a date via
        > a drop down select. How do I make php put a zero in front of the numbers
        > 1-9 ie. 01 02 03 when writing to the file? I have tried making the
        > value="01" etc. but this doesn't work. Below is a snippet of my form and
        > the code I am using to write to the file. I hope I have made myself
        > clear enough. Your help is greatly appreciated.
        >
        >
        > //form part
        > <select class="inform" name="logday">
        > <option value="1">1
        > <option value="2">2
        > <option value="3">3
        > <option value="4">4
        > <option value="5">5
        > <option value="6">6
        > <option value="7">7
        > <option value="8">8
        > <option value="9">9
        > <option value="10">10
        > etc,
        >
        > //code part
        > fputs($out,"$_P OST[logday] ");
        >
        >
        > Thanks,
        > Patrick[/color]

        I figured it out.

        $logday = $_POST[logday];

        and then

        fprintf($out,"% 02d",$logday);

        instead of the fputs, which gives me my leading zero.

        Patrick

        --
        Patrick A. Smith Assistant System Administrator
        Ocean Circulation Group – USF - College of Marine Science
        http://ocgweb.marine.usf.edu Phone: 727 553-3334

        The trouble with doing something right the first time is that nobody
        appreciates how difficult it was. - La Rochefoucauld

        Comment

        • omanzi

          #5
          Re: help putting zero in front of single digits in select

          Use this

          <?

          for ($i==1;$i<= 31;$i++)
          {
          if ($i < 10)
          {
          ?>

          <option value="<? echo $i; ?>"> 0<? echo $i; ?> </option>

          <? } else { ?>
          <option value="<? echo $i; ?>"> <? echo $i; ?> </option>

          <?
          }
          }
          ?>

          Enjoy it

          omanzi

          Comment

          • David Haynes

            #6
            Re: help putting zero in front of single digits in select

            Justin Koivisto wrote:[color=blue]
            > Patrick wrote:[color=green]
            >> Hello All,
            >>
            >> Kind of new to PHP. I have an html form where a person inputs a date
            >> via a drop down select. How do I make php put a zero in front of the
            >> numbers 1-9 ie. 01 02 03 when writing to the file? I have tried making
            >> the value="01" etc. but this doesn't work. Below is a snippet of my
            >> form and the code I am using to write to the file. I hope I have made
            >> myself clear enough. Your help is greatly appreciated.
            >>
            >>
            >> //form part
            >> <select class="inform" name="logday">
            >> <option value="1">1
            >> <option value="2">2
            >> <option value="3">3
            >> <option value="4">4
            >> <option value="5">5
            >> <option value="6">6
            >> <option value="7">7
            >> <option value="8">8
            >> <option value="9">9
            >> <option value="10">10
            >> etc,
            >>
            >> //code part
            >> fputs($out,"$_P OST[logday] ");[/color]
            >
            > use something more like:
            > fputs($out,spri ntf('%02d',intv al($_POST['logday'])));[/color]

            or
            fprintf($out, '%02d', $_POST['logday']);

            Also, for kicks, the form part could be:

            <select class="inform" name="logday">
            <?php
            foreach( range(1, 10) as $v ) {
            printf("<option value="%d">%d</option>\n", $v);
            }
            ?>

            -david-

            Comment

            • David Haynes

              #7
              Re: help putting zero in front of single digits in select

              David Haynes wrote:
              Typed too fast, correction below:[color=blue]
              > fprintf($out, '%02d', $_POST['logday']);
              >
              > Also, for kicks, the form part could be:
              >
              > <select class="inform" name="logday">
              > <?php
              > foreach( range(1, 10) as $v ) {
              > printf("<option value="%d">%d</option>\n", $v);[/color]
              printf("<option value=\"%02d\"> %d</option>\n", $v);[color=blue]
              > }
              > ?>
              >
              > -david-
              >[/color]

              Comment

              Working...