By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,854 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,854 IT Pros & Developers. It's quick & easy.

printing html table elements inside php script

P: n/a
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_players">
<input name="Submit" type="submit"></form>
<?php
$x=0;
while ($x<$number_players){
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)

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
*** go****@charliefortune.com wrote/escribió (10 Jun 2005 08:50:45 -0700):
How do I put html inside a php loop.
That's explained in the very first chapter of the manual:

http://php.grn.es/manual/en/introduction.php

There you have a link to further explanations:

http://php.grn.es/manual/en/language...syntax.phpmode
<?php
$x=0;
while ($x<$number_players){
print ("Place : $x<br>");
$variable ="<input type="textarea" name=$x>";
print($variable);
$x++;
}
</body>


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
--
Jul 17 '05 #2

P: n/a


go****@charliefortune.com wrote (in part):
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_players">
<input name="Submit" type="submit"></form>
<?php
$x=0;
while ($x<$number_players){
print ("Place : $x<br>");
$variable ="<input type="textarea" name=$x>";
print($variable);
$x++;
}
</body>


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_players']))?' value="' .
$_POST['number_of_players'] . '"':'';
$tmp[] = 'Number of players: <input type="text"
name="number_of_players"' . $tmpv . '>';
if (isset($_POST['number_of_players']))
for ($i=0;$i<$_POST['number_of_players'];$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

Jul 17 '05 #3

P: n/a
go****@charliefortune.com wrote:
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_players">
<input name="Submit" type="submit"></form>
<?php
$x=0;
while ($x<$number_players){
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)

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_players) {
?>
/ 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=textareaname></textarea>

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Jul 17 '05 #4

P: n/a
Chuck Anderson wrote:
go****@charliefortune.com wrote:
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_players">
<input name="Submit" type="submit"></form>
<?php
$x=0;
while ($x<$number_players){
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)


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_players) {
?>
/ 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=textareaname></textarea>

Not sure how those slashes (/) got there, but they're not supposed to be
there.

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

<?php
$x++;
}
?>
</body>
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.