Hey guys, So I have a wordpress loop which currently outputs an unordered list: $terms = get_terms("brands"); $count = count($terms); if ( $count > 0 ){ echo "<ul>"; foreach ( $terms as $term ) { echo "<li><a href=\"$term->slug\"> " . $term->name. "</a><br />" . $term->description. "</li>"; } echo "</ul>"; } PHP: Is there a away I can get it to dynamically populate a table like this: <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="26"> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> HTML: With the number of rows increasing as the number of loop items increases. Is that possible? I seem to remember something similar a while back. Many thanks,
simply replace the <li>...</li> in the loop with a <tr>....</tr> and corresponding TDs.. and put the table opening and closing tag before and after the loop..
Yes, but that places each item in the list in a row! I want to have 3 per row in columns before setting up a new row my problem lies in placing the TD's in the correct places :S
here's a sample: $terms = get_terms("brands"); $count = count($terms); if ( $count > 0 ){ echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">'; foreach ( $terms as $term ) { echo ' <tr> <td><a href="'.$term->slug.'">'.$term->name.'</a></td> <td>'.$term->description.'</td> </tr>'; } echo "</table>"; } just tweak it as you need
I think you have the wrong idea... I am trying to do something like this: --------------------------------- | Brand 1 | Brand 2 | Brand 3 | | Logo | Logo | Logo | --------------------------------- | Brand 4 | Brand 5 | Brand 6 | | Logo | Logo | Logo | ---------------------------------
You may use the table column counter like $i in my example: $terms = get_terms("brands"); $count = count($terms); if ( $count > 0 ){ echo "<table>"; $i = 0; echo "<tr>"; foreach ( $terms as $term ) { echo "<td><a href=\"$term->slug\"> " . $term->name. "</a><br />" . $term->description. "</td>"; $i++; if ($i == 3) { // if it's the last column $i = 0; echo("</tr><tr>"); } } if ($i > 0) { // if we have empty cells left to display while ($i < 3) { echo("<td> </td>"); $i++; } } echo "</tr>"; echo "</table>"; } PHP:
Starts to sound like what you are asking for is NOT using tables for tabular data, but tables for layout... just as with it having a name and description that ALMOST sounds like what should be a heading and a paragraph -- meaning a DIV around them and floats -- NOT a list. Though it's REALLY hard to say what the proper markup should be without seeing some actual content... but it seems like you're not actually working with tabular data so why in blazes put it in a table? Oh, and @ahost, what's with the multiple echo's doing the job of singles, oddball parenthesis around echo outputs, double quotes just making the code harder to use, string additions for nothing, and screwball iteration that could result in <tr></tr> in the output? $terms = get_terms("brands"); $count = count($terms); if ($count > 0) { $i = 0; echo ' <table>'; foreach ($terms as $term) { if ($i == 0) { echo ' <tr>'; } echo ' <td> <a href="',$term->slug,'">',$term->name,'</a> <br /> ',$term->description,' </td>'; $i = ($i + 1) % 3; if ($i == 0) { echo ' </tr>'; } } if ($i > 0) { while ($i++ < 3) { echo ' <td></td>'; } echo ' </tr>'; } echo ' </table>'; } Code (markup): NOT that this looks to be tabular data.
I may have used the wrong terminology so I'd like to apologise for that. But that is exactly what I wanted! So thank you very much guys!
Well, what you wanted is kinda... wrong. If I was making a section like that NONE of it is tabular data... so I'd probably have an appropriate level heading (h2 or h3 most likely depending on how the parent section is declared), then handle it thus: PHP: $terms = get_terms("brands"); if (count($terms) > 0) { echo ' <div class="brandInfo">'; $count = 0; foreach ($terms as $term) { echo ' <div',( ($count++) % 3 == 0 ? ' class="zeroOfThree"' : '' ),'> <h2><a href="',$term->slug,'">',$term->name,'</a></h2> <p> ',$term->description,' </p> </div>'; } echo ' <!-- .brandInfo --></div>'; } Code (markup): CSS: .brandInfo { overflow:hidden; /* wrap floats */ width:100%; /* trip haslayout, wrap floats old IE, report width for sizing */ } .brandInfo div { float:left; width:33%; } .brandInfo .zeroOfThree { clear:both; } Code (markup): Makes the PHP a billion times simpler, gives semantic markup of the content, does pretty much the same appearance. Tables are for tabular data -- and you don't seem to have that.