Help .. how to generate html table using PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Help .. how to generate html table using PHP?

Post by phpnewbieguy »

Hello ! :P a newbie here :)

I'd like to create a html table with php that would look like this: :D

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
Last edited by phpnewbieguy on Wed Feb 10, 2010 4:17 pm, edited 1 time in total.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: how to generate table using PHP?

Post 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
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Re: how to generate table using PHP?

Post 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>
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: Help .. how to generate html table using PHP?

Post 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...
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Re: Help .. how to generate html table using PHP?

Post 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?
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Help .. how to generate html table using PHP?

Post 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.
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Re: Help .. how to generate html table using PHP?

Post 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
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Help .. how to generate html table using PHP?

Post 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.
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Re: Help .. how to generate html table using PHP?

Post 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 :)
phpnewbieguy
Forum Newbie
Posts: 6
Joined: Wed Feb 10, 2010 3:37 pm

Re: Help .. how to generate html table using PHP?

Post 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>";
}
?>
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Help .. how to generate html table using PHP?

Post 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
Post Reply