printing html table elements inside php script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • google@charliefortune.com

    printing html table elements inside php script

    I would like to print a table, using a while loop to insert as many
    blank text fields as players specified by the user. How do I put html
    inside a php loop.


    This is what I have....

    <body>
    <form name="results" method="post">
    Number of players:<input type ="textarea" name="number_pl ayers">
    <input name="Submit" type="submit"></form>
    <?php
    $x=0;
    while ($x<$number_pla yers){
    print ("Place : $x<br>");
    $variable ="<input type="textarea" name=$x>";
    print($variable );
    $x++;
    }


    </body>


    (I did a search, but didn't understand the answer I found)

  • Alvaro G Vicario

    #2
    Re: printing html table elements inside php script

    *** google@charlief ortune.com wrote/escribió (10 Jun 2005 08:50:45 -0700):[color=blue]
    > How do I put html inside a php loop.[/color]

    That's explained in the very first chapter of the manual:



    There you have a link to further explanations:


    [color=blue]
    > <?php
    > $x=0;
    > while ($x<$number_pla yers){
    > print ("Place : $x<br>");
    > $variable ="<input type="textarea" name=$x>";
    > print($variable );
    > $x++;
    > }
    >
    >
    > </body>[/color]

    Didn't you get a parse error?


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Ken Robinson

      #3
      Re: printing html table elements inside php script



      google@charlief ortune.com wrote (in part):[color=blue]
      > I would like to print a table, using a while loop to insert as many
      > blank text fields as players specified by the user. How do I put html
      > inside a php loop.
      >
      >
      > This is what I have....
      >
      > <body>
      > <form name="results" method="post">
      > Number of players:<input type ="textarea" name="number_pl ayers">
      > <input name="Submit" type="submit"></form>
      > <?php
      > $x=0;
      > while ($x<$number_pla yers){
      > print ("Place : $x<br>");
      > $variable ="<input type="textarea" name=$x>";
      > print($variable );
      > $x++;
      > }
      >
      >
      > </body>[/color]

      You almost have it right. Here's one way of doing what you want.
      <?
      $tmp = array();
      $tmp[] = '<form action="' . $_SERVER['PHP_SELF'] . '" name="results"
      method="post">' ;
      $tmpv = (isset($_POST['number_of_play ers']))?' value="' .
      $_POST['number_of_play ers'] . '"':'';
      $tmp[] = 'Number of players: <input type="text"
      name="number_of _players"' . $tmpv . '>';
      if (isset($_POST['number_of_play ers']))
      for ($i=0;$i<$_POST['number_of_play ers'];$i++)
      $tmp[] = 'Place ' . $i . ': <input type="text" name="place[' .
      $i . ']">';
      $tmp[] = '<input type="submit" name="submit" value="Submit"> ';

      echo implode("<br>\n ",$tmp)."\n ";
      ?>

      Some explanation.

      The first time a user brings up the form, just input box labeled
      "Number of players" is displayed. When the user fills it in and presses
      "submit", the form is redisplayed with the appropriate number of input
      boxes.

      More code is needed for this form to do anything else and I have done
      no error checking, such as making sure that the input is really a
      number.

      Ken

      Comment

      • Chuck Anderson

        #4
        Re: printing html table elements inside php script

        google@charlief ortune.com wrote:
        [color=blue]
        >I would like to print a table, using a while loop to insert as many
        >blank text fields as players specified by the user. How do I put html
        >inside a php loop.
        >
        >
        >This is what I have....
        >
        ><body>
        ><form name="results" method="post">
        >Number of players:<input type ="textarea" name="number_pl ayers">
        ><input name="Submit" type="submit"></form>
        ><?php
        > $x=0;
        > while ($x<$number_pla yers){
        > print ("Place : $x<br>");
        > $variable ="<input type="textarea" name=$x>";
        > print($variable );
        > $x++;
        > }
        >
        >
        ></body>
        >
        >
        >(I did a search, but didn't understand the answer I found)
        >
        >
        >[/color]
        I recently saw a post saying this was bad form or something, but it's
        what I always do. You can use open and close tags all you want, so I
        close php and just put in the HTML (echoing any Php variables):

        <?php
        $x=0;
        while ($x<$number_pla yers) {
        ?>
        / Place : <?= $x ?> <br>
        <input type="textarea" name=<?= $x ?>>

        /<?php
        $x++;
        }
        ?>
        </body>

        I find that this makes the script much more readable and it's easier to
        place correct and well structured HTML.

        BTW, there is no input type=textarea. It's type=text or if you really
        meant a textarea, they have their own tags <textarea
        name=textareana me></textarea>

        --
        *************** **************
        Chuck Anderson • Boulder, CO

        Integrity is obvious.
        The lack of it is common.
        *************** **************

        Comment

        • Chuck Anderson

          #5
          Re: printing html table elements inside php script

          Chuck Anderson wrote:
          [color=blue]
          >google@charlie fortune.com wrote:
          >
          >
          >[color=green]
          >>I would like to print a table, using a while loop to insert as many
          >>blank text fields as players specified by the user. How do I put html
          >>inside a php loop.
          >>
          >>
          >>This is what I have....
          >>
          >><body>
          >><form name="results" method="post">
          >>Number of players:<input type ="textarea" name="number_pl ayers">
          >><input name="Submit" type="submit"></form>
          >><?php
          >> $x=0;
          >> while ($x<$number_pla yers){
          >> print ("Place : $x<br>");
          >> $variable ="<input type="textarea" name=$x>";
          >> print($variable );
          >> $x++;
          >> }
          >>
          >>
          >></body>
          >>
          >>
          >>(I did a search, but didn't understand the answer I found)
          >>
          >>
          >>
          >>
          >>[/color]
          >I recently saw a post saying this was bad form or something, but it's
          >what I always do. You can use open and close tags all you want, so I
          >close php and just put in the HTML (echoing any Php variables):
          >
          ><?php
          >$x=0;
          >while ($x<$number_pla yers) {
          >?>
          >/ Place : <?= $x ?> <br>
          ><input type="textarea" name=<?= $x ?>>
          >
          >/<?php
          >$x++;
          >}
          >?>
          ></body>
          >
          >I find that this makes the script much more readable and it's easier to
          >place correct and well structured HTML.
          >
          >BTW, there is no input type=textarea. It's type=text or if you really
          >meant a textarea, they have their own tags <textarea
          >name=textarean ame></textarea>
          >
          >
          >[/color]
          Not sure how those slashes (/) got there, but they're not supposed to be
          there.

          <?php
          $x=0;
          while ($x<$number_pla yers) {
          ?>
          Place : <?= $x ?> <br>
          <input type="textarea" name=<?= $x ?>>

          <?php
          $x++;
          }
          ?>
          </body>


          --
          *************** **************
          Chuck Anderson • Boulder, CO

          Integrity is obvious.
          The lack of it is common.
          *************** **************

          Comment

          Working...