2-D Array question

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
hsiwux07
Forum Newbie
Posts: 9
Joined: Mon Oct 16, 2006 3:05 am

2-D Array question

Post by hsiwux07 »

I'm trying to generate a table automatically made by this PHP code.

function.php

Code: Select all

<?php
function process()
{
	$value=$_POST['table'];
	
	if(empty($value['row']))
	{
		$res="PLEASE ENTER ROWS";
	}
	elseif(empty($value['col']))
	{
		$res="PLEASE ENTER COLUMNS";
	}
	else
	{
	$x=$value['row'];
	$y=$value['col'];
	$res="<table border='1'>";
	for($i=1;$i<=$x;$i++)
	{
	$res.="<tr>";
	for($j=1;$j<=$y;$j++)
		{
		$res.="<td>$j</td>";
		}
	$res.="</tr>";
	}
	$res.="</table>";
	}
	return $res;
}

function table()
{
	$res="<form action='table.php' method='post'>
	<input type='text' name='table[row]'>ROW
	<input type='text' name='table[col]'>COLUM
	<input type='hidden' name='op' value='process'>
	<input type='submit' value='SHOW TABLE'></form>";
	return $res;
}
?>
index.php

Code: Select all

<?php
include("function.php");
$opt=isset($_REQUEST['op']) ? $_REQUEST['op'] : null;
switch($opt)
{
	case "process":
		$html=process();
	break;

	default:
		$html=table();
	break;
}
?>

<html>
<head><title>TABLE</title></head>
<body>
<?php echo $html; ?>
</body>
</html>
but here I'm trying to make this table shown in 2-D array and output things like
1-1,1-2,1-3,...
2-1,...
3-1,... and so on(depends on the entries).

I'm just curious about how to modify this code to change it from 1-D to 2-D arrays.

Thanks very much.
gunman
Forum Newbie
Posts: 10
Joined: Fri Aug 26, 2005 12:07 pm

Answer

Post by gunman »

You should just add

Code: Select all

echo "<td>".$i." - ".$j."</td>";
instead of

Code: Select all

echo "<td>$j</td>";
and it will work also i don't know where would you place that peace of code but it would be really good if you check post before creation of table from post
Post Reply