settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to create table in PHP and HTML?

+7 votes
Hi. I want to create table with PHP with 10 rows and 3 columns;Also it needs to be in HTML. How can I do it?

Thanks
asked in PHP category by user paulcabalit

2 Answers

+5 votes
 
Best answer

Here is mine with the escaped characters \n and \t (linefeed and horizontal tab):

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>PHP Table </title>
</head>
<body>

<?php

$i = 1;
echo "<table border='1'>\n";
while ( $i <= 10 ) {
	echo "\t<tr>\n";
	$n = 1;
	while ( $n <= 3 ) {
		echo "\t\t<td>Row $i | Col $n</td>\n";
		$n++;
	}
	echo "\t</tr>\n";
	$i++;
}
echo '</table>';

?>

</body>
</html>

See more about escaped characters HERE

answered by user sam
selected by user golearnweb
+5 votes

Here is the code (I've used while). See line #17 - I've used double quotes instead of singles ones:

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>

<?php

$i = 1;
echo '<table border="1">';
while ( $i <= 10 ) {
	echo '<tr>';
	$n = 1;
	while ( $n <= 3 ) {
		echo "<td>Row $i | Col $n</td>";//Use double quotes for $i and $n
		$n++;
	}
	echo '</tr>';
	$i++;
}
echo '</table>';

?>

</body>
</html>

...and the result in the browser:

how to create php table in html

answered by user samfred5830
You forgot to use \n (new row) and \t (tab)...
...