Output A Mysql Loop To A Table

Discussion in 'PHP' started by asgsoft, Feb 11, 2013.

  1. #1
    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">&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</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,
     
    asgsoft, Feb 11, 2013 IP
  2. innozemec

    innozemec Active Member

    Messages:
    84
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    68
    #2
    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..
     
    innozemec, Feb 11, 2013 IP
  3. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #3
    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
     
    asgsoft, Feb 11, 2013 IP
  4. innozemec

    innozemec Active Member

    Messages:
    84
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    68
    #4
    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
     
    innozemec, Feb 11, 2013 IP
  5. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #5
    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 |
    ---------------------------------
     
    asgsoft, Feb 11, 2013 IP
  6. ahost

    ahost Active Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    88
    #6
    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>&nbsp;</td>");
            $i++;
          }
        }
        echo "</tr>";
        echo "</table>";
    }
    PHP:
     
    ahost, Feb 11, 2013 IP
    asgsoft likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    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.
     
    Last edited: Feb 11, 2013
    deathshadow, Feb 11, 2013 IP
    asgsoft likes this.
  8. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #8
    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!
     
    asgsoft, Feb 12, 2013 IP
  9. ahost

    ahost Active Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    88
    #9
    Not pretending for a clean and professional code. It was just an idea how to do the thing.
     
    ahost, Feb 12, 2013 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    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.
     
    deathshadow, Feb 12, 2013 IP
    asgsoft likes this.