Web Hosting Talk







View Full Version : Dynamic PHP Help


2kau
10-26-2005, 06:29 AM
Here is what I need to do with PHP:

I have a table with two columns and a dynamic number of rows.

I need for the script to put the content like this:

Column 1 Column 2
DATA 1 DATA 2
DATA 3 DATA 4

What is an easy way to do that?

Jakiao
10-27-2005, 10:26 PM
Wrong forum, first off. Belongs in Programming.

Writing a while() {} loop would do you well. Say you had the following variables:

$column1[n] and $column2[n] where n is a value in an array and each values line up

<?php

$print = "

<table width=\"500\">
<tr>
<td width=\"250\">
Column 1
</td>
<td width=\"250\">
Column 2
</td>
</tr>

";

$column1 = array("Data value 1", "Another value", "A third value");
$column2 = array("For 1", "For 2", "For 3");
$x = 0;

while ( $column[$x] ) {

$print .= "

<tr>
<td width=\"250\">
" . $column1[$x] . "
</td>
<td width=\"250\">
" . $column2[$x] . "
</td>
</tr>

";

$x++;

}

$print .= "

</table>

";

print $print;

?>

You'd just simply change $column1 and $column2 array values for the values you wish. Also, if you don't want to use array(), you can go to long way of:

$column1[] = "Data value 1";
$column1[] = "Another value";
$column1[] = "A third value";

$column2[] = "For 1";
$column2[] = "For 2";
$column2[] = "For 3";

It will order the values the same way as array() would.

Oh, one added note, if you just want it to do value 1, value 2, value 3, value 4, and so on doing two per row and not actually having column one and column two related, you could do:

<?php

$print = "

<table width=\"500\">
<tr>
<td width=\"250\">
Column 1
</td>
<td width=\"250\">
Column 2
</td>
</tr>

";

$column = array("Data value 1", "Another value", "A third value", "Another", "And another");

$x = 0;

while ( $column[$x] ) {

$print .= "

<tr>
<td width=\"250\">
" . $column1[$x] . "
</td>

";

$x++;

$print .= "

<td width=\"250\">
" . $column1[$x] . "
</td>
</tr>

";

$x++;

}

$print .= "

</table>

";

print $print;

?>

choon
10-27-2005, 10:53 PM
Moved from Technical & Security Issues to Programming forum.