Jump to content

Styling Comment Section from Database


Go to solution Solved by printf,

Recommended Posts

Trying to style the comments section here but it's a little challenging to style the response from a database.  I would like for starters a simple HR tag on the top but when I do that it duplicates with the one on the bottom.

 

This is the live page:

http://www.golden-wand.com/comments/index.php

 

This is the PHP code from the section I need help styling:

<?php	
include '../Scripts/connection.php';

$sql = "SELECT id, name, email, subject, comment FROM comment";
$result = $connect->query($sql);

if ($result->num_rows > 0) {
	// output data of each row
	while($row = $result->fetch_assoc()) {
		echo "
		<table class='commenttable'>
			<tr>
				<td class='id'>
					<u>Loyal Client ". $row["id"] . "</u>: 
				</td>
				<td class='subject'>
					" . $row["subject"]. "
				</td>
			</tr>
			<tr>
				<td></td>
				<td class='comment' colspan='2'>
					" . $row["comment"]. "
				</td>
			</tr>
		</table>
			<hr class='commenthr'>";
	}
} else {
	echo "No Comments";
}
?>

I've also uploaded the index.php page just in case I've edited the live version, thanks for any help :)

index.php

index.php

Link to comment
https://forums.phpfreaks.com/topic/298809-styling-comment-section-from-database/
Share on other sites

Your first problem is you need to take the table start and end tags out of the while loop. If you have the <hr> in the loop it is going to keep repeating. Your better off putting the DB variables in parenthesis instead of doing all that escaping.

 

{$row["subject"]}

Edited by benanamen
  • Solution

This looks like a PHP question, not a SQL question! Anyway, move the <table> out of the loop, and then just style your <tr> tag with a border-top or border-bottom.

 

        <td>
        <table class='commenttable'>
            <?php    
            include '../Scripts/connection.php';
            
            $sql = "SELECT id, name, email, subject, comment FROM comment";
            $result = $connect->query($sql);
            
            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "
            <tr style=\"border-bottom: 1px solid #000;\">
                            <td class='id'>
                                <u>Loyal Client ". $row["id"] . "</u>:
                            </td>
                            <td class='subject'>
                                " . $row["subject"]. "
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td class='comment' colspan='2'>
                                " . $row["comment"]. "
                            </td>
                        </tr>
";
                }
            } else {
                echo "            <tr>
                <td>
                    No Comments
                </td>
            <tr>
";
            }
            ?>
        </table>
        </td>

By double post do you mean in my code or I actually posted to this site twice? I can't for the life of me find out how to find my second post it says I have 2 posts but no link on how to get there. I only had this one post in my favorites, also I though it would have emailed me when anyone replied guess I just needed to check myself hah.  Thanks for the quick and helpful responses I've since made a somewhat decent login/signup section.  I'm still working on my login process but I'll be sure to hop into the correct section if I need more help :)

 

I ended up using the border-top and border-bottom with the tr and td.  I also really liked the first post because all that escaping was super annoying  :tease-01: but if I had to chose one the second person nailed it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.