Page 1 of 1
Help .. how to generate html table using PHP?
Posted: Wed Feb 10, 2010 3:46 pm
by phpnewbieguy
Hello !

a newbie here
I'd like to create a html table with php that would look like this:
A D G J
B E H K
C F I L
The option for this table:
eg: inputs
4 columns
3 rows
Ouput1:
1 2 3 4
5 6 7 8
9 10 11 12
Output2:
1 4 7 10
2 5 8 11
3 6 9 12
Output3:
12 11 10 9
8 7 6 5
4 3 2 1
Output4:
12 9 6 3
11 8 5 2
10 7 4 1
Re: how to generate table using PHP?
Posted: Wed Feb 10, 2010 3:54 pm
by Brad7928
What you could do is get php to choose a random number between 1 and 4 and save it as a variable, then have your 4 different outputs set for each random number.
Code: Select all
<?
$i = rand(1, 4);
IF i = 1 THEN
?>
<table>
...
</table>
<?
END IF
IF i = 2 THEN
...
etc
Re: how to generate table using PHP?
Posted: Wed Feb 10, 2010 4:02 pm
by phpnewbieguy
@ Brad7928
Thanks for the very quick Reply,

I appreciate it
my problem is how to generate number inside the table.
I have my code, it's currently working with output 1 and output 3.
Can't figure out how to get the result output for output 2 and 4.
heres my code for output 1:
Code: Select all
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter number of rows <input name="rows" type="text" size="4"> and columns <input name="columns" type="text" size="4"> <input type="submit" name="submit" value="Draw Table">
</form>
<?php
if (isset($_POST['submit'])) {
Echo "<h1>Table</h1>";
Echo "<table border='1' width '250'>";
$rows = $_POST['rows'];
$columns = $_POST['columns'];
$total = $rows * $columns;
for ($number = 1; $number <=$total; $number++)
{ echo "<td>";
echo $number;
echo "</td>";
$j ++;
if($j==$columns){echo "</tr><tr>";
$j=0;}
}
Echo "</table>";
}
?>
</body>
</html>
Re: Help .. how to generate html table using PHP?
Posted: Wed Feb 10, 2010 4:59 pm
by Brad7928
oh, i see what you are trying to do now... i'll get back to you with this, i need to think about it and am currently at work...
Re: Help .. how to generate html table using PHP?
Posted: Thu Feb 11, 2010 9:10 am
by phpnewbieguy
@Brad7928
Hope you could help me out
nice forum, I get very quick replies
the different outputs may have different script. I'm still working it out
anyone can contribute?
Re: Help .. how to generate html table using PHP?
Posted: Thu Feb 11, 2010 10:54 am
by limitdesigns
You can do a different for loop for each row. Example: for($number = 1; $number <= max_rows; $number + X), and you'd get X by finding out how many rows/columns you have. In your example, X would be 3. Then you can run through that loop with different presets for $number to populate the rest of the rows.
Re: Help .. how to generate html table using PHP?
Posted: Thu Feb 11, 2010 11:20 am
by phpnewbieguy
limitdesigns wrote:You can do a different for loop for each row. Example: for($number = 1; $number <= max_rows; $number + X), and you'd get X by finding out how many rows/columns you have. In your example, X would be 3. Then you can run through that loop with different presets for $number to populate the rest of the rows.
but my problem is how to put numbers inside?
this the output I'm trying to get :
Ouput1:
1 2 3 4
5 6 7 8
9 10 11 12
Output2:
1 4 7 10
2 5 8 11
3 6 9 12
Output3:
12 11 10 9
8 7 6 5
4 3 2 1
Output4:
12 9 6 3
11 8 5 2
10 7 4 1
Re: Help .. how to generate html table using PHP?
Posted: Thu Feb 11, 2010 12:54 pm
by limitdesigns
Well, you did it for example 1 and 3, right? So before each for loop, type <tr>, then within the for loop have <td><?php echo $number;?></td>, and at the end of the for loop close your row with </tr>. Then repeat for however many rows you want.
Re: Help .. how to generate html table using PHP?
Posted: Thu Feb 11, 2010 1:09 pm
by phpnewbieguy
limitdesigns wrote:Well, you did it for example 1 and 3, right? So before each for loop, type <tr>, then within the for loop have <td><?php echo $number;?></td>, and at the end of the for loop close your row with </tr>. Then repeat for however many rows you want.
Thanks @ limitdesigns
I'm going to try that, I will update it if I have something

Re: Help .. how to generate html table using PHP?
Posted: Tue Feb 16, 2010 2:26 pm
by phpnewbieguy
BUMP
this what I got after, but still I can get the numbers to display at the right cell

(
Code: Select all
<?php
if (isset($_POST['submit'])) {
Echo "<h1>Table</h1>";
Echo "<table border='1' width '250'>";
$rows = $_POST['rows'];
$columns = $_POST['columns'];
$total = $rows * $columns;
$oi = 1;
echo "<tr>";
for ($x = 1; $x <=$total; $x++)
{
echo "<td>".$x."</td>";
}
echo "</tr>";
Echo "</table>";
}
?>
Re: Help .. how to generate html table using PHP?
Posted: Tue Feb 16, 2010 5:44 pm
by jraede
You need multiple for loops, one for each row. And instead of writing $i++, you would need to write $i + 3 (in this case), because the numbers in each row of your table increase by 3 from left to right. Assuming you have three rows, you would write:
Code: Select all
<table>
<tr>
<?php
for($i=1;$i<=9; $i+3) {
echo "<td>".$i."</td>";
}
?>
</tr>
<tr>
<?php
for($i=2;$i<=9; $i+3) {
echo "<td>".$i."</td>";
}
?>
</tr>
<tr>
<?php
for($i=3;$i<=9; $i+3) {
echo "<td>".$i."</td>";
}
?>
</tr>
</table>
This will make a 3x3 table with
1 4 7
2 5 8
3 6 9