
October 30th, 2004, 07:25 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
The Answer
I found a simple script that accomplished displaying results horizontally (aka Horizontal looping) but it did not add the additional <td> tags at the end of the table if there were an uneven amount of results. Here is my take on this: It seems to work fine.
Code:
<?php
require_once('Connections/info.php');
$sql = "SELECT * FROM inventory";
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr>
<?php
$numColumns = 3; //Enter how many columns you want your table to be
$extraCells = $numColumns - ($num_rows % $numColumns) ; //determines how many EMPTY extra cells the resulting table may have
$resultingRows = substr($num_rows / $numColumns,0,1) ;//Determines how many COMPLETE html table rows will be diplayed on page
for($i=0; $i < $num_rows; $i++) { //loops through results of query
$row = mysql_fetch_array($result);
if (substr($i/$numColumns,0,1) != $resultingRows){ //if its not the last complete row
if ($i % $numColumns != $numColumns - 1){ //if its not the last cell of the current row
echo "<td>".$row['serialNumber']."</td>\n";
}else{ //else it is the last cell of the current row, so a </tr> is added
echo "<td>".$row['serialNumber']."</td></tr>\n";
}
}else{ //else it is the last complete row
echo "<td>".$row['serialNumber']."</td>\n";
}
if ($i==$num_rows - 1){ // if it's the last record, we need to add the amount of EMPTY extra cells needed.
for($j=0; $j < $extraCells; $j++) {
echo "<td> </td>\n";
}
echo"</tr>\n";
}
}
?>
</table>
</body>
</html>
-- Mike Puglisi of South Florida
|