1

On my "popular tags" page, I am fetching all of the tags from the database with this PHP & SQL in order from most popular to least popular:

$sql = "SELECT t.tagid, t.name, t.desc, COUNT(qt.id) AS total
        FROM tags t
        LEFT JOIN question_tags qt ON t.tagid=qt.tag_id
        GROUP BY t.tagid, t.name
        ORDER BY total DESC";

$result = mysql_query($sql) or die(mysql_error());

while($row=mysql_fetch_assoc($result)) {
  echo '<div class="tag-list-box" id="tag-'.$row['tagid'].'">
        <a href="/tags/'.strtolower($row['name']).'" class="tag">
        <span class="label label-inverse label-tag">'.strtolower($row['name']).'</span>
        </a> <strong>&times; '.$row['total'].'</strong><br />
        <p>'.($row['desc']!==null ? $row['desc'] : '<em>This tag has no description.</em>').'</p>
        </div>';
}

I don't have it in an HTML table right now because I thought I could do it the more "modern way" with divs and CSS, but as you can see in this screenshot below, the rows get messed up when a tag has a description because the height of the box is greater than the others:

enter image description here

My CSS for the boxes is:

.tag-list-box {
  width: 20%;
  padding: 5px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  float: left;
  margin-right: 2%;
  margin-bottom: 10px;
}

I am thinking I should put it in a table which should fix the problem but my question is how do I make every 4 rows from the query have their own <tr> so there can be 4 in each table row?

I can't figure it out — I tried doing a count and incrementing it in the while loop but then I just don't know where to go from there.

1
  • Looks like somebody is trying to create a mini-SO, no? Commented Jun 15, 2012 at 18:19

2 Answers 2

2

Use the modulo operator to see if your count is divisible by 4:

if($count % 4 == 0) 

If you want to keep your css solution, you can also set a height for your divs and then set overflow to hidden to handle descriptions that are too long.

Sign up to request clarification or add additional context in comments.

3 Comments

If JS is an option, he could also add an equal height mechanism, it would be a nice addition (and to me, better than having text cut by the hidden overflow)
@DamienPirsy I could do that (and yes JS is an option), but I don't think the descriptions will be that long. Thanks for the idea though :)
I'm having a little problem (I'm probably just not doing it right), it is doing this: dl.dropbox.com/u/25819920/… Here's the code: dl.dropbox.com/u/25819920/…
1

Yeah I would check to see if you have 4 in a row like Scott said, however. You can use divs. After every 4 just add a div that clears the left float. I would NOT use a set height because you never know how tall the items will get. Example of what I would do:

$count = 0;
while($row=mysql_fetch_assoc($result)) {
  echo '...'; //YOUR CURRENT ECHO STATEMENT HERE
  $count++;
  if($count % 4 == 0) echo "<div style='clear: left;'></div>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.