Skip to content
Advertisement

New lines HTML if value changed in MySql Output via PHP

This is the HTML/PHP , I am using to display some data.

 <body>
        <section>
            <h1>Facebook Search</h1>
            <!-- TABLE CONSTRUCTION-->
            <table>
                <tr>
                    <th>Comment</th>
                    <th>Comment Made By</th>
                    <th>Commentor's Profile Link</th>
                    <th>Comment ID</th>
                    <th>Post ID</th>
                    
                </tr>
                <!-- PHP CODE TO FETCH DATA FROM ROWS-->
                <?php   // LOOP TILL END OF DATA 
                    while($rows=$result->fetch_assoc())
                    {
                 ?>
                <tr>
                    <!--FETCHING DATA FROM EACH 
                        ROW OF EVERY COLUMN-->
                    <td><?php echo $rows['comtext'];?></td>
                    <td><?php echo $rows['comby'];?></td>
                    <td><?php echo $rows['compro'];?></td>
                    <td><?php echo $rows['commentid'];?></td>
                    <td><?php echo $rows['postid'];?></td>
                </tr>
                <?php
                    }
                 ?>
            </table>
        </section>
    </body>
      
    </html>

and I am getting the data like this from the db.

$sql = "SELECT comments.*, posts.postid FROM comments JOIN posts USING(postid)";
$result = $mysqli->query($sql);
$mysqli->close(); 

This outputs a single table with all the data. My question is , is there a way to break the single table into tables when the value of postid changes? It goes like..is 1 for x number of rows, then 2 for x number of rows and so on. I wanted to have a little break between those tables.

Advertisement

Answer

is there a way to break the single table into tables when the value of postid changes

You should stop using “SELECT *”, it’s inefficient and makes code difficult to maintain. While we’re talking about style here, you swap back and forth between PHP and inline HTML which also impacts the readability of your code.

As to your question…

You need to ensure that the output of your query is sorted by postid – or you’re going to get a lot of tables.

Use a state variable to track the postid from the previous iteration of the loop.

$prev=0;
while($rows=$result->fetch_assoc()) {
   if ($rows['postid']!=$prev) {
      close_table();
      open_table();
      $prev=$rows['postid'];
   }
   output_row($rows);
}
close_table();

The problem with this is that each iteration of the expects that you’ve already written <table> to the output stream – including the first iteration. You could do this before the loop – but you’ll end up with an empty table at the top of the page. You could add another state variable to track whether you have opened a table tag yet. Personally, I’d go with making inferences from the value of the existing state variable:

$prev=0;
while($rows=$result->fetch_assoc()) {
   if ($rows['postid']!=$prev) {
      $prev || close_table();
      open_table();
      $prev=$rows['postid'];
   }
   output_row($rows);
}
close_table();

(the || is a quick way of writing if (not(condition)) do something() )

Note that unless you fix the size of the columns, then each table will be independently sized to fit the data that resides within it.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement