<?php $data = mysql_query("SELECT * FROM forum_topics ORDER BY id DESC LIMIT 5"); while($row = mysql_fetch_array($data)) { echo " <div> ".$row[id]." ".$row[title]." </div> "; } ?> Code (markup): The code above displays 5 latest topics from my forum from a mysql database. But the problem is that I want to display them in divs horizontally, but the code above displays them vertically. I understand that I need to create a table and add <td> and </td> to the echo. But I would also need to make it so that on every 5h query, there is a </tr> to start a new row -- is this correct? If so, how the hell do I implement it within the code? Or any other suggestions?
First - the code presented above only displays 5 rows - so, there would be no need for making more <tr> elements... You can change the code as follows: <?php $data = mysql_query("SELECT * FROM forum_topics ORDER BY id DESC LIMIT 5"); ?> <table> <tr> <?php while($row = mysql_fetch_array($data)) { echo " <td> ".$row[id]." </td> <td> ".$row[title]." </td> "; } ?> </tr> </table> PHP: not tested, but should work just fine.
<?php include "conn.php"; $data = mysql_query("SELECT * FROM wp_posts"); $i=1; echo "<table>"; while($row = mysql_fetch_array($data)) { if($i == 1) { echo "<tr>"; } echo " <td> ".$row[ID]." ".$row[post_title]." </td> "; if($i == 5) { $i = 1; echo "</tr>"; } else { $i++; } } // catch if the last TR is missing if($i != 1) { echo "</tr>"; } echo "</table>"; ?> PHP: output : <table style='font-size:10px; width: 350px;'><tr> <td> 1 Hello world! </td> <td> 2 About </td> <td> 3 Lorum ipseum is unavailable. </td> <td> 4 Lorum ipseum is unavailable. </td> <td> 5 Image Azure, how blue does the star shine ? </td> </tr><tr> <td> 6 park4copy </td> <td> 7 Image Azure, how blue does the star shine ? </td> <td> 8 Image with text </td> <td> 9 Image with text </td> </table> Code (markup):
Not really a PHP question, but you just have to change your div tag to <div style='float: left;'> If all you want is to display them horizontally, that's all you have to do. No need to mess about with tables and other furniture.
Apart from the following facts: *floated divs might mess up the rest of the page layout, and are hell to position properly if the content differs too much *it's a presentation of table-data - ie. a table would be the correct semantic HTML to use
1. <br style='clear: both;' /> 2. It's a list of 5 items. If anything, it belongs in a <ul>. Tables are for two-dimensional grids.
if the output is 5 items, maybe 10, ul would work. somewhat easier too. It's really a list of 5 items with 2 sub items. Add anything else(image, excerpt) and additional markup would be required. the br clear both, I wouldn't dream of using. wrap it in a div. give it height and width, and overflow set to hidden. Is moot though, up to what the OP final use is.
No offense intended, but you clearly, as you've demonstrated twice now, have no clue as for correct markup of HTML. 1. The <br style="clear: both;" /> is ridiculous, and is definitely not the right way to do a clear-statement. Use a <div> for that, or a hidden <hr /> if you must. 2. No, it is NOT a list of 5 items - or, it COULD be a list, but he presents them with both ID and TITLE - that makes TWO, and that again, my friend, makes it exactly a two-dimensional grid. Now, how about reading the posts next time, and actually read what the W3C and several HTML & CSS professionals recommends for dealing with different type of data presentation?
Use a 2000-pixel embeded Flash animation of a dancing puppy if you want. The point is that keeping floating divs from causing trouble outside their milieu is a manageable task. Jesus Christ, put it back in your pants already. What did I ever do to you to turn you into a raving pit bull? It's not all that hard to disagree like an adult, is it? See shallowink's response for an example. From my viewing of the OP's initial markup, the ID and title are presented adjacently (like "15 - Tomatoes"). It's possible I understood him wrong but it seems ambiguous at best. That's a one-dimensional list of grouped tuples, unless you're going to take the position that, for instance, a list of words is two-dimensional because you can put the individual letters on a second axis.
-1 for flame bait. 1. I said use a DIV. 2. OP said, "start a new row" so the nice little list of 5 items is now a list of 10 items on two rows. So when they go, let's add a 3rd, it'll be 15 on 3. So at what point does the tabular data go into a table? actually, I think the ID will be used for creating a hyper link so it will be a single item, but I wouldn't limit it to that.
But you disagreed about the <br /> tag, and I thought you did it in a pleasant and mature manner, which is why I called attention to it - to contrast it with PoPSiCLe's hissy fit. Depends on your philosophy, I guess. The purist would argue that semantically, it's still a one-dimensional list of 15 items that happens to require a two-dimensional layout due to screen width constraints. Anyway, I'd rather argue with spammers, not with fellow programmers who are also trying to help out strangers on the forum.
Guys, please stay on topic So how do I make the mysql query count if there's 5 items and insert a <br />, a <ul>, a <tr> -- whatever -- after the fifth result?
Those comments weren't directed at you bud. I read the other post and it felt like he was attacking both of us. Other guy must have had a rough night. To the OP: the code I showed you at the start handles your requirement. Just have to replace the table lines with ul, etc.
Well, okay, I went a bit off. I'm sorry about that. I still think using the correct markup is important. I'm quite bad tempered when I see HTML code used semantically wrong, which is about on every single webpage out there. The point of the "list" remains - as long as you present something as two values (as the list in this instance does, by having an ID and a Title), it is practically speaking, a table. How you present that table, for instance by having colspan="0" and looking like a list, is kinda irrelevant to the data presented. And yes, it was a rough night Sorry for the blow-up.