Getting PHP variables to display fixed length

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

    Getting PHP variables to display fixed length

    Hello,
    I am coding a menu using PHP. Selections on the menu have code like :

    <li> <a href="Updatefin datesedit.php"> $fin_name1 $fin_date1 </a></li>
    <li> <a href="Updatefin datesedit.php"> $fin_name2 $fin_date2 </a></li>
    etc


    My problem is the $fin_name1 variables can be anywhere from 1 - 20
    characters

    so my menu items come out looking like
    Joe 2000/01/01
    Fred 2001/01/02
    Alexander 2001/01/03

    Can I get the $fin_name* variable to take up a fixed amount of space
    when displaying? My idea is to use strlen on fin_name and put a number
    of &nbsp's between, but that sure seems like a dumb way to do it. I
    figure someone with more knowledge can tell me an easier way to do
    it....
    TIA
    Japhy

  • ZeldorBlat

    #2
    Re: Getting PHP variables to display fixed length

    You want str_pad() :

    Pad a string to a certain length with another string


    Comment

    • Jeff North

      #3
      Re: Getting PHP variables to display fixed length

      On 28 Oct 2005 20:00:11 -0700, in comp.lang.php "Japhy"
      <japhyrider2005 @yahoo.com> wrote:
      [color=blue]
      >| Hello,
      >| I am coding a menu using PHP. Selections on the menu have code like :
      >|
      >| <li> <a href="Updatefin datesedit.php"> $fin_name1 $fin_date1 </a></li>
      >| <li> <a href="Updatefin datesedit.php"> $fin_name2 $fin_date2 </a></li>
      >| etc
      >|
      >|
      >| My problem is the $fin_name1 variables can be anywhere from 1 - 20
      >| characters
      >|
      >| so my menu items come out looking like
      >| Joe 2000/01/01
      >| Fred 2001/01/02
      >| Alexander 2001/01/03
      >|
      >| Can I get the $fin_name* variable to take up a fixed amount of space
      >| when displaying? My idea is to use strlen on fin_name and put a number
      >| of &nbsp's between, but that sure seems like a dumb way to do it. I
      >| figure someone with more knowledge can tell me an easier way to do
      >| it....[/color]

      You might have to re-think the layout. HTML will automatically remove
      extraneous spaces. You will need to use &nbsp; for padding.

      Also, you will need to use a fixed width font i.e. monospace.
      <li style="font-family: monospace"> <a
      href="Updatefin datesedit.php"> $fin_name1 $fin_date1 </a></li>

      Alternatively, use a table for the layout.
      ---------------------------------------------------------------
      jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
      ---------------------------------------------------------------

      Comment

      • Ian B

        #4
        Re: Getting PHP variables to display fixed length

        What do you want to do?
        * Stretch the text (probably not a good idea)
        * Left justify the name and right justify the date? (A table is
        probably easiest)
        * Have the whole width clickable (set the width of the <a> and have a
        background colour which makes this obvious)

        Ian

        Comment

        • VS

          #5
          Re: Getting PHP variables to display fixed length

          Japhy wrote:
          [color=blue]
          > <li> <a href="Updatefin datesedit.php"> $fin_name1 $fin_date1 </a></li>
          > <li> <a href="Updatefin datesedit.php"> $fin_name2 $fin_date2 </a></li>
          > etc
          >
          >
          > My problem is the $fin_name1 variables can be anywhere from 1 - 20
          > characters
          >
          > so my menu items come out looking like
          > Joe 2000/01/01
          > Fred 2001/01/02
          > Alexander 2001/01/03[/color]

          Why not use a table ??

          --
          VS

          Comment

          • boclair

            #6
            Re: Getting PHP variables to display fixed length

            Japhy wrote:[color=blue]
            > Hello,
            > I am coding a menu using PHP. Selections on the menu have code like :
            >
            > <li> <a href="Updatefin datesedit.php"> $fin_name1 $fin_date1 </a></li>
            > <li> <a href="Updatefin datesedit.php"> $fin_name2 $fin_date2 </a></li>
            > etc
            >
            >
            > My problem is the $fin_name1 variables can be anywhere from 1 - 20
            > characters
            >
            > so my menu items come out looking like
            > Joe 2000/01/01
            > Fred 2001/01/02
            > Alexander 2001/01/03
            >
            > Can I get the $fin_name* variable to take up a fixed amount of space
            > when displaying? My idea is to use strlen on fin_name and put a number
            > of &nbsp's between, but that sure seems like a dumb way to do it. I
            > figure someone with more knowledge can tell me an easier way to do
            > it....
            > TIA
            > Japhy
            >[/color]
            Can I suggest that the date needs to be moved to a known position to the
            right of the name and that sufficient space be provided for the longest
            name. An html table is one way. The other is CSS, eg,


            <ul>

            <li style="width:12 em;">
            <div style="float:ri ght;width:4px;t ext-align:right;">y yyy/mm/dd</div>
            fin_name
            </li>


            <li style="width:12 em;">
            <div style="float:ri ght;width:4px;t ext-align:right;">y yyy/mm/dd</div>
            fin_longer-name</li>

            </ul>

            adjust the width of the containing div and the float div to suit.

            Ideally the css declarations will be in selectors in a style block or
            style sheet, e.g.

            <style type="text/css">
            ul.name-date li { width:12em; }
            ul.name-date li div { float:right;wid th:4px;text-align:right; }
            </style>

            </head>

            <body>

            <ul class="name-date">

            <li>
            <div>yyyy/mm/dd</div>
            fin_name
            </li>


            <li>
            <div>yyyy/mm/dd</div>
            fin_longer-name</li>

            </ul>

            Louise

            Comment

            • Japhy

              #7
              Re: Getting PHP variables to display fixed length

              Thanks to all who replied. You've given me a lot of options to
              explore.
              As for using a table, I got the idea for the menu at Dynamic Drive, and
              didn't want to mess
              with the 'design' too much.
              Thanks again to all!

              Comment

              Working...