I am currectly completing a progect using os commerce,

I have managed to add a box with a link to relevant page however i am having problems coding a a simple table, all i want to do is have three columns and several rows.

Could anybody help?

Assuming you already have an array, say $rows that represent the data in the table like:

$rows = array(0=>array('foo', 'bar'), 1=>array('f o o', 'b a r'));

For tables you'll normally want a two dimensional array..

That is: an array holding arrays that represent the rows of the table.

When you select from a mysql database, this is the format you get automatically.

Once you have your array, you just need to loop through each row in the array. I'll use the foreach() loop for simplicity. It will loop through each index of an array. In our case, each index is another array.

eg: If you know the columns of your table in advance.

note: The table structure is taken from: http://www.htmlhelp.com/reference/html40/tables/table.html
I've just made them lowercase so that they are xHTML1.0 compliant. (I assume)

// assume the array looks like
$rows = array(
   0=>array('column1'=>'value1', 'column2'=>'value2'), 
   1=>array('column1'=>'value3', 'column2'=>'value4')
);

echo '<table>';
echo '<thead>';
echo '<tr>';

// list the table headers, if you need
echo '<th>Column 1</th><th>Column 2</th>';

echo '</tr>';
echo '</thead>';

echo '<tbody>';

foreach($rows as $index=>$row) {

	// $index is the row index
	// $row is the array containing the columns/indexes and corresponding column values
	
	echo '<tr class="row'.($index%2).'">';
	echo '<td class="column1">'.$row['column1'].'</td>';
	echo '<td class="column2">'.$row['column2'].'</td>';
	echo '</tr>';

}

echo '</tbody>';
echo '</table>';

Its pretty self-explanatory, except maybe for ($index%2)
The %, is the modulus operator. So $index%2 will alternate between 0 and 1 as the columns increment.

eg: If you don't know the columns in advance.

Its just the same as knowing the columns in advance, except you'll have to add another loop inside the main loop, to loop through the table data. (columns in each row).

// assume the array looks like
$rows = array(
   0=>array('column1'=>'value1', 'column2'=>'value2'), 
   1=>array('column1'=>'value3', 'column2'=>'value4')
);

echo '<table>';
echo '<thead>';
echo '<tr>';

// list the table headers, if you need
// since we don't know the columns in advance, we'll just iterate through the first row
foreach($rows[0] as $index=>$row) {
	echo '<th>'.$index.'</th>';
}

echo '</tr>';
echo '</thead>';

echo '<tbody>';

foreach($rows as $index=>$row) {

	// $index is the row index
	// $row is the array containing the columns/indexes and corresponding column values
	
	echo '<tr class="row'.($index%2).'">';
	
	// iterate through each column (td) in the current row (tr)
	foreach($row as $col_index=>$column) {
		echo '<td class="column'.($col_index%2+1).'">'.$column.'</td>';
	}
	
	echo '</tr>';

}

echo '</tbody>';
echo '</table>';

Off course how you iterate through the data depends on the structure of your array, however, I hope that gives you a start in the right direction...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.