Format a table with PHP

Status
Not open for further replies.

Mike

New member
Jun 27, 2006
6,779
114
0
50
On the firing line
Okay, here's what I'm trying to do:

I've got a datafeed, and I would like to display it with X number of items PER row. I've got a script that will put one item per row, and I can mod that to make it one item per column, but neither of those produce a clean output. Can one of you php gurus help a guy out?

Here is the basic feed:
PHP:
<?php
$login = "netshops";
$password = "feedme";
$table = "clockstyle";
$aid = "cd1";
$database = "netshopsaffiliates_com_-_datafeeds";
$connection = mysql_connect("207.234.208.224","$login","$password")
    or die ("Unable to connect.");
mysql_select_db($database)
    or die ("Unable to open database.");
$sql = "SELECT * FROM $table order by category limit 0,12";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
    {
        extract($row);
        echo "<table width='100%' border='0' cellspacing='0' cellpadding='3'> \n";
        echo "<tr>\n";
        echo "<td valign='top'>\n<a href='$link$aid'> <img src='$image' border=0> </a> \n <br> <b> Price: </b> $$price\n </td> \n";
        echo "<td valign='top'>\n<a href='$link$aid'> \n<b> $product </b> </a> \n<br> $description\n <br> <b> Item: </b> <i> $sku </i> \n <b> Category: </b> <i> $category </i> <br> </td>\n";
        echo "</tr>\n";
        echo "</table>\n";
        echo "<hr size='1'>\n";
    }
mysql_close($connection);
?>
So the output is just one long page of items. I changed the query to limit the results to 12. Ideally this should output a table with 3 colums and 4 rows.


I figure I need to define that somewhere, and then increment the rows so I added these variables:
PHP:
$row_items = 4;
$total_items = 12;
$total_rows = round(($total_items / $row_items), 0);
$current_row = 0;

Then I get stumped...
 


some of the ugliest code on the face of this earth...
Code:
<?php
$login = "netshops";
$password = "feedme";
$table = "clockstyle";
$aid = "cd1";
$database = "netshopsaffiliates_com_-_datafeeds";
$connection = mysql_connect("207.234.208.224","$login","$password") or die ("Unable to connect.");
mysql_select_db($database) or die ("Unable to open database.");
$sql = "SELECT * FROM $table ORDER BY category LIMIT 0,12";
$result = mysql_query($sql);
$cnt = 0;
$str = "<table border=3>\n\t<tr>";
while ($row = mysql_fetch_array($result)) {
		if($cnt==3) {
			$str .= "\n\t</tr>\n\t<tr>";
			$cnt = 0;
		}
			$str .= "\n\t\t<td valign=\"top\">";
			extract($row);
			$str .= "\n\t\t\t<table width='100%' border='0' cellspacing='0' cellpadding='3'>";
			$str .= "\n\t\t\t\t<tr>";
			$str .= "\n\t\t\t\t\t<td valign='top'><a href='$link$aid'><img src='$image' border=0></a><br><b>Price: </b> $$price</td>";
			$str .= "<td valign='top'><a href='$link$aid'><b> $product </b></a><br>$description<br><b> Item: </b><i>$sku</i><b>Category: </b><i>$category</i><br></td>";
			$str .= "\n\t\t\t\t</tr>";
			$str .= "\n\t\t\t</table>";
			$str .= "\n\t\t</td>";
			$cnt++;
}
mysql_close($connection);
$str .= "\n\t</tr>\n</table>";
echo $str;
?>
 
  • Like
Reactions: Mike
Status
Not open for further replies.