[Help] Numbering PHP table

01/26/2013 11:03 Hikarim#1
Hi,
i am trying to make a table using PHP, and i have a problem numbering each cell with a different number. So if the table is 3x3, like this
1 2 3
4 5 6
7 8 9

Here is what i got (i know what is worng, i am displaying td numbers, but i dont know how to do it like i want...) :
Code:
<?php 

function drawTable(){

$rows = 4; // amout of tr
$cols = 4;// amjount of td

echo "<table border='1'>"; 


for($tr=1;$tr<=$rows;$tr++){ 

    echo "<tr>";
    for($td=1;$td<=$cols;$td++){
    	echo "<td align='center'>".$td."</td>";
    }
	
    echo "</tr>";
}
 
echo "</table>";
}

drawTable();
?>
01/26/2013 11:44 MrPuschel#2
PHP Code:
<?php 

function drawTable(){

$rows 4// amout of tr
$cols 4;// amjount of td

echo "<table border='1'>"
$cnt 1;

for(
$tr=1;$tr<=$rows;$tr++){ 

    echo 
"<tr>";
    for(
$td=1;$td<=$cols;$td++){
        echo 
"<td align='center'>".$cnt."</td>";
$cnt++;
    }
    
    echo 
"</tr>";
}
 
echo 
"</table>";
}

drawTable();
?>
01/26/2013 16:35 Hikarim#3
Ok, 1 more thing, how can i make the user to input number of rows and cols? ($rows; $cols; )
01/26/2013 17:09 MrPuschel#4
[Only registered and activated users can see links. Click Here To Register...]

Don't get me wrong, but it is kinda obvious that you need this for school. Other people doing that for you won't help you if it comes to exams.
01/26/2013 17:11 Hikarim#5
yea, i also checked that but i couldnt get it to work... im not sure what the "action=""" to set to, as my whole code is in one file...
01/26/2013 17:30 kissein#6
[Only registered and activated users can see links. Click Here To Register...]
Quote:
'PHP_SELF'
The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address [Only registered and activated users can see links. Click Here To Register...] would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
PHP Code:
action="<? $_SERVER['PHP_SELF'?>"
Modify your function drawTable to accept params. Check if Post params are set and combine it with your function.
01/26/2013 17:35 tayfe#7
You can just leave it blank if it's the same file:
HTML Code:
<form action="" method="post">
Otherwise you just use your file name. So if your file is called "index.php" you just write
HTML Code:
<form action="index.php" method="post">
It's that easy :)

And if you want to add some parameters you can just attach them using a "?":
HTML Code:
<form action="index.php?page=createTable" method="post">
But you should already know this stuff :)
01/26/2013 17:58 Hikarim#8
ok, thank you for that,... but still, how can i change my variable with using $_GET ?
because there is already a variable $rows=5; and it draws a table with 5 rows... how can i make it to change the $rows:5; to lets say $rows=9; if the user sets so? -It is easy to do it with just basic name submision forms, but this is confusing as nothing that i tryed works :S
01/26/2013 18:42 MrPuschel#9
First, you should use the superglobal [Only registered and activated users can see links. Click Here To Register...]. Everything else is deprecated and shall not be used.

Your code would look like:

PHP Code:

$row 
5;
$myRow =  $_REQUEST['myRow'] ;

// Has $myRow any content?
// Keep in mind that it checks only for content. Not valid content.

if ( $myRow 
   
$row $myRow 
01/26/2013 18:43 tayfe#10
You really dont know?

I could tell you but this wont help you much. You should really read a tutorial! This are the absolutly basics!

But to give you a hint: Use something like this:

PHP Code:
$rows $_POST['rows']; 
But you will need to add some lines to your formular. Just read a tutorial and you will be able to do it on your own very quick!