Forum Moderators: coopster
Any help would be great.
<?php
include 'includes/dbconnect.php';
$result = mysql_query("SELECT category FROM CATEGORIES");
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\"><tr>";
$i = 0;
while($array = mysql_fetch_array($result)){//loop through
$i++;
echo "<td>". $array['category']. "</td>";
if(!$i%3){echo "</tr><tr>";$i=0;}
}
$num_rows = mysql_num_rows($result);
$find_colspan = 3 - ($num_rows%3);//if there are 6 rows, this is 0, if there are 7, this is 1.
for($i=0;$i<$find_colspan;$i++){
echo "</td>";
}
echo "</tr></table>";
?>
Here is view source:
<table border="1" cellpadding="2" cellspacing="2"><tr><td>Accommodations - Hotels & Resorts</td><td>Arts & Entertainment</td><td>Automotive</td><td>Business & Professional Services</td><td>Clothing & Accessories</td><td>Community & Government</td><td>Computers & Electronics</td><td>Construction & Contractors</td><td>Education</td><td>Food & Dining</td><td>Government, Community, Schools, Churches</td><td>Health & Medicine</td><td>Home & Garden</td><td>Industry & Agriculture</td><td>Internet</td><td>Legal & Financial</td><td>Media & Communications</td><td>Personal Care & Services</td><td>Real Estate</td><td>Shopping</td><td>Sports & Recreation</td><td>Travel & Transportation</td><td>Wedding Services</td><td>Non-Profits</td></td></td></td></tr></table>
Got it working with the code below.
Now can anyone help me toward getting each category to be a link to it's corresponding page?
Example..."Home & Garden" would be a link to the home & garden page...etc..
Thanks again.
Working code below:
<?php
include 'includes/dbconnect.php';
$result = mysql_query("SELECT category FROM CATEGORIES");
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\"><tr>";
$i = 0;
while($array = mysql_fetch_array($result)){//loop through
$i++;
echo "<td>". $array['category']. "</td>";
//if(!$i%3){echo "</tr><tr>";$i=0;}
if($i == 3){echo "</tr><tr>";$i=0;}
}
$num_rows = mysql_num_rows($result);
$find_colspan = 3 - ($num_rows%3);//if there are 6 rows, this is 0, if there are 7, this is 1.
for($i=0;$i<$find_colspan;$i++){
echo "</td>";
}
echo "</tr></table>";
?>
<?php
include 'includes/dbconnect.php';
// Set the number of columns you want
$num_cols = 3;
$column_count = 0;
$select = 'select category from categories order by category'; //alphabetical if text
//------------------------------------------//
$result = mysql_query("$select");
echo '<table border="1" cellpadding="2" cellspacing="2">';
while($array = mysql_fetch_array($result)) {
if ($column_count==0) { echo '<tr>'; }
echo '<td><a href="yourscript.php?category=' . $array['category'] . '">' . $array['category']. '</a></td>';
$column_count++;
if ($column_count >= $num_cols) {
// end the row, reset the colum counter.
// Next loop it will create a new row.
echo '</tr>';
$column_count=0;
}
}
// On the last row of data, it may not give you three full columns
// If $column_count is 0, it's good. Otherwise,
if (($column_count > 0) and ($column_count < $num_cols)) {
for ($i=$column_count;$i<$num_cols;$i++) {
echo '<td> </td>';
}
echo '</tr>';
}
echo '</table>';
?>
Edit: added to add the link per second post, swear it wasn't there . . . . If your categories are by some unique record ID, which they should be, you'd use $array['cat_id'] instead of $array['category'] in the link.
Thank for your help.
Final code below:
$result = mysql_query("SELECT category, cat_id FROM CATEGORIES order by category");
echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"5\"><tr>";
$i = 0;
while($array = mysql_fetch_array($result)){//loop through
$i++;
echo "<td width=\"303\">". "<img src='images/test-bullet.jpg' width=\"6\" height=\"6\" />".' ' . "<a href='view-category.php?category=" .$array['cat_id']. "'/>" .$array['category']. "</td>";
//if(!$i%3){echo "</tr><tr>";$i=0;}
if($i == 3){echo "</tr><tr>";$i=0;}
}
$num_rows = mysql_num_rows($result);
$find_colspan = 3 - ($num_rows%3);//if there are 6 rows, this is 0, if there are 7, this is 1.
for($i=0;$i<$find_colspan;$i++){
echo "</td>";
}
echo "</tr></table>";