Forum Moderators: coopster

Message Too Old, No Replies

Help needed with DB output into columns

         

stever_nl

2:38 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Hi, would appreciate some help re-coding the following so it will display the results not on one line (seperated by "¦") but in a table of 3 columns.

It queries the DB for subcategories and if present list them seperated by "¦"


$pol = mysql_query("select * from categories where parent = '$catid' order by name");
if (mysql_num_rows($pol) > 0) {
while ($scat = mysql_fetch_object($pol)) {
$subcatimg = rand(1,5);

$subcathtml .= "<img src=\"".$_SERVER["HOST_NAME"]."/images/cat-".$subcatimg.".gif\" border=\"0\" align=\"absmiddle\"><a href=\"".str_replace(" ", "_", $scat->name)."/".getindex($scat->name).".$pext\">".$scat->name."</a> ¦ ";

}

$subcathtml = substr($subcathtml, 0, -2);
}
else $subcathtml = "Geen";

I've tried several web suggestions, but cannot seem to assimilate it with the exsisting code above.

Thanks for any support!

Steve

ironik

3:05 am on Mar 15, 2005 (gmt 0)

10+ Year Member



I'm not sure what data you want in each column, but here's my guess at it:

// Start the table
$subcathtml = "<table>";

// Start the loop
while ($scat = mysql_fetch_object($pol)) {
$subcatimg = rand(1,5);

// Start the row
$subcathtml .= "<tr>";

// The first column
$subcathtml .= "<td><img src=\"".$_SERVER["HOST_NAME"]."/images/cat-".$subcatimg.".gif\" border=\"0\" align=\"absmiddle\"></td>";

// The second column
$subcathtml .= "<td><a href=\"".str_replace(" ", "_", $scat->name)."/".getindex($scat->name)."$pext\">".$scat->name."</a></td>";

// The third column... not sure what goes in here
$subcathtml .= "<td>&nbsp;</td>";

// End the row
$subcathtml .= "</tr>";
}
// End the table
$subcathtml .= "</table>";

Hopefully I'm not too far off the mark and this is what you were after :)

stever_nl

3:18 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Hi Ironik, thanks for the effort! But it was not what I wanted.

The image before the link is just a design thingie, it should reside into the same table cell like this:

<table>
<tr>
<td>[image][subcategorie] </td>
<td>[image][subcategorie] </td>
<td>[image][subcategorie] </td>
</tr>
<tr>
<td>[image][subcategorie] </td>
<td>[image][subcategorie] </td>
<td>[image][subcategorie] </td>
</tr>
</table>

Thanks!

ironik

3:39 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Ah, I see... a little different then:


// Start the table
$subcathtml = "<table>";

// Store an internal counter for the loop
$counter = 1;

// Store a number of rows
$numRows = mysql_num_rows($pol);

// Start the loop
while ($scat = mysql_fetch_object($pol)) {
$subcatimg = rand(1,5);

// Start the first row
if ($counter == 1)
{
$subcathtml .= "<tr>";
}

// column
$subcathtml .= "<td><img src=\"".$_SERVER["HOST_NAME"]."/images/cat-".$subcatimg.".gif\" border=\"0\" align=\"absmiddle\">a href=\"".str_replace(" ", "_", $scat->name)."/".getindex($scat->name)."$pext\">".$scat->name."</a></td>";

// End the row
if (($counter % 3) == 0 && $counter!= $numRows)
{
$subcathtml .= "</tr><tr>";
} else if ($counter == $numRows) {
for ($i = 1; $i <= ($counter % 3); $i++)
{
$subcathtml .= "<td>&nbsp;</td>";
}
$subcathtml .= "</tr>";
}
$counter ++;
}
// End the table
$subcathtml .= "</table>";

I haven't been able to test this, but it should give you a starting point.

The internal counter is tested against the number of columns that you want and the total number of rows, so it should give you a well formed table with empty columns if it doesn't fit within your 3 column structure.

stever_nl

4:05 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Ironik, you're the best!

This worked exactly as I wanted it :)

Learned more about loops, but will need more tutoring in the future to fully understand it.

Thanks again,

Steve

ironik

4:08 am on Mar 15, 2005 (gmt 0)

10+ Year Member



hehe, that's probably the first piece of untested code that has worked for me on these forums. Glad it served it's purpose! ;)