Help - Search - Members - Calendar
Full Version: Edit Comments
Codegrrl.com Forums > Script Help > Tutorial Help
Steven
I followed the tutorial to add edit/delete comment features to my administrative panel, and I have run into a problem.

Most of the time the comments are long and sometime when the "while" loop does its job the comments run together and make a mess. So I am trying to sort them using tables. Only the first comment displays properly (In a table) then the rest are printed in plain text and my footer gets messed up. What is wrong?

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Phreak Your Geek - Blog Admin - Edit/Delete Comments</title>
<style type="text/css">
@import url("http://www.pyg.tgohome.com/includes/pyg.css");
</style>
</head>
<body>
  <div class="center">
    <table id="layout">
      <tr>
        <td id="header" colspan="2">
          <h2><a href="index.php">Phreak Your Geek</a></h2>
          <h3>the ramblings of a labeled geek.</h3>
        </td>
      </tr>
      <tr>
        <td id="contents">
        <h1>Blog Admin - Edit/Delete Comments</h1><hr />
<?php
mysql_connect ('````', '````', '````');
mysql_select_db ('````');

if (isset($_POST['edit'])) {
    $name = htmlspecialchars(strip_tags($_POST['name']));
    $email = htmlspecialchars(strip_tags($_POST['email']));
    $url = htmlspecialchars(strip_tags($_POST['url']));
    $comment = htmlspecialchars(strip_tags($_POST['comment']));
    $comment = nl2br($comment);
    $id = (int)$_POST['id'];

    if (!get_magic_quotes_gpc()) {
        $name = addslashes($name);
        $url = addslashes($url);
        $comment = addslashes($comment);
    }

    $result = mysql_query("UPDATE php_blog_comments SET name='$name', email='$email', url='$url', comment='$comment' WHERE id='$id' LIMIT 1") or print ("Can't update comment.<br />" . $result . "<br />" . mysql_error());
    if ($result != false) {
        print "<p>The comment has successfully been edited.</p>";
    }
}

if(isset($_POST['delete'])) {
$id = (int)$_POST['id'];
     $result = mysql_query("DELETE FROM php_blog_comments WHERE id='$id' LIMIT 1") or print ("Can't delete comment.<br />" . $result . "<br />" . mysql_error());
     if ($result != false) {
         print "<p>The comment has successfully been deleted.</p>";
     }
}

if (isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {

$result = mysql_query ("SELECT * FROM php_blog_comments WHERE id='$_GET[id]'") or print ("Can't select comment.<br />" . mysql_error());

while ($row = mysql_fetch_array($result)) {
      $old_name = stripslashes($row['name']);
      $old_email = $row['email'];
      $old_url = stripslashes($row['url']);
      $old_comment = stripslashes($row['comment']);
      $old_comment = str_replace('<br />', '', $old_comment);
}

?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><input type="hidden" name="id" id="id" value="<?php echo $_GET['id']; ?>">
    <strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="40" value="<?php echo $old_name; ?>" /></p>
    <p><strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="40" value="<?php echo $old_email; ?>" /></p>
    <p><strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="40" value="<?php echo $old_url; ?>" /></p>
    <p><strong><label for="comment">Comment:<label></strong><br />
    <textarea cols="80" rows="20" name="comment" id="comment"><?php echo $old_comment; ?></textarea></p>
    <p><input type="submit" name="edit" id="edit" value="Save Changes"> <input type="submit" name="delete" id="delete" value="Delete Comment"> <input type="submit" value="Never Mind"></p>
</form>
<?php

}
else {

$result = mysql_query("SELECT entry AS get_group FROM php_blog_comments GROUP BY get_group DESC LIMIT 10") or print ("Can't select comments.<br />" . $result . "<br />" . mysql_error());

while($row = mysql_fetch_array($result)) {
     $get_group = $row['get_group'];

     print "<table border=\"1\">";

    $result2 = mysql_query("SELECT timestamp, title FROM php_blog WHERE id='$get_group'");
    while($row2 = mysql_fetch_array($result2)) {
        $date = date("l F d Y",$row2['timestamp']);
        $title = stripslashes($row2['title']);
        print "<tr><td><strong>" . $date . " - " . $title . "</strong></td></tr>";
    }

    $result3 = mysql_query("SELECT * FROM php_blog_comments WHERE entry='$get_group' ORDER BY timestamp DESC");
    while($row3 = mysql_fetch_array($result3)) {
        $id = $row3['id'];
        $name = stripslashes($row3['name']);
        $comment = stripslashes($row3['comment']);
        $date = date("l F d Y",$row3['timestamp']);

        if (strlen($comment) > 75 || strstr($comment, "<br />") || strstr($comment, "\n")) {
            $comment = substr($comment,0,75) . "...";
            $comment = str_replace("<br />", "", $comment);
            $comment = str_replace("\n", " ", $comment);
        }

        print "<tr><td><a href=\"editcomments.php?id=" . $id . "\">" . $comment . "</a></td></tr><tr><td>Comment by " . $name . " @ " . $date;
        print "</td></tr></table>";

    }
}
}
mysql_close();

?>
        </td>
      </tr>
      <tr>
        <td id="footer" colspan="2">
            No copyright in these parts!
        </td>
      </tr>
    </table>
  </div>
</body>
</html>
Amelie
I can't see anything wrong with that...

Can you post a copy of your source code (when you are at the troublesome page in your browser, go to View > Source and paste what it says here)? Maybe if we can see what the code is outputting we will be able to find out what the problem is. smile.gif
loadx
off topic well sort of. Someone should re-write the blog tutorials to separate the database functions from the underlying design.

Smarty would be a great idea, it's easy and the syntax is well documented.
Steven
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Phreak Your Geek - Blog Admin - Edit/Delete Comments</title>
<style type="text/css">
@import url("http://www.pyg.tgohome.com/includes/pyg.css");
</style>
</head>
<body>
  <div class="center">
    <table id="layout">
      <tr>

        <td id="header" colspan="2">
          <h2><a href="index.php">Phreak Your Geek</a></h2>
          <h3>the ramblings of a labeled geek.</h3>
        </td>
      </tr>
      <tr>
        <td id="contents">
        <h1>Blog Admin - Edit/Delete Comments</h1><hr />

<table border="1"><tr><td><strong>Thursday February 15 2007 - Like it?</strong></td></tr><tr><td><a href="editcomments.php?id=4">Ryan, why not follow the tutorial again? You'll have a better understanding...</a></td></tr><tr><td>Comment by Steven @ Sunday February 18 2007</td></tr></table><tr><td><a href="editcomments.php?id=3">Nice! Steven, can you please send me the blog files via email? my e-mail ad...</a></td></tr><tr><td>Comment by RP07 @ Saturday February 17 2007</td></tr></table><tr><td><a href="editcomments.php?id=2">Nice!  I actually have an rss feed tutorial for the build-a-blog tutorial s...</a></td></tr><tr><td>Comment by Nick (hdshngout) @ Friday February 16 2007</td></tr></table><tr><td><a href="editcomments.php?id=1">Testing the comment engine!</a></td></tr><tr><td>Comment by Steven @ Friday February 16 2007</td></tr></table>        </td>
      </tr>
      <tr>
        <td id="footer" colspan="2">

            No copyright in these parts!
        </td>
      </tr>
    </table>
  </div>
</body>
</html>


This help? It only prints out one <table border="1">...
Alexandra_K
I don't know if this is still relevant, but I had the same problem and fixed it without tables. All that was needed was a bit of fiddling with the <p> tags.

Instead of this:
CODE

while($row = mysql_fetch_array($result)) {
     $get_group = $row['get_group'];

     print "<p>";

    $result2 = mysql_query("SELECT timestamp, title FROM php_blog WHERE id='$get_group'");
    while($row2 = mysql_fetch_array($result2)) {
        $date = date("l F d Y",$row2['timestamp']);
        $title = stripslashes($row2['title']);
        print "<strong>" . $date . " - " . $title . "</strong><br />";
    }

    $result3 = mysql_query("SELECT * FROM php_blog_comments WHERE entry='$get_group' ORDER BY timestamp DESC");
    while($row3 = mysql_fetch_array($result3)) {
        $id = $row3['id'];
        $name = stripslashes($row3['name']);
        $comment = stripslashes($row3['comment']);
        $date = date("l F d Y",$row3['timestamp']);

        if (strlen($comment) > 75 || strstr($comment, "<br />") || strstr($comment, "\n")) {
            $comment = substr($comment,0,75) . "...";
            $comment = str_replace("<br />", "", $comment);
            $comment = str_replace("\n", " ", $comment);
        }

        print "<a href=\"editcomments.php?id=" . $id . "\">" . $comment . "</a><br />Comment by " . $name . " @ " . $date;
        print "</p>";

    }


I used this:
CODE
while($row = mysql_fetch_array($result)) {
     $get_group = $row['get_group'];

     print "<p>";

    $result2 = mysql_query("SELECT timestamp, title FROM php_blog WHERE id='$get_group'");
    while($row2 = mysql_fetch_array($result2)) {
        $date = date("l F d Y",$row2['timestamp']);
        $title = stripslashes($row2['title']);
        print "<strong>" . $date . " - " . $title . "</strong></p>";
    }

    $result3 = mysql_query("SELECT * FROM php_blog_comments WHERE entry='$get_group' ORDER BY timestamp DESC");
    while($row3 = mysql_fetch_array($result3)) {
        $id = $row3['id'];
        $name = stripslashes($row3['name']);
        $comment = stripslashes($row3['comment']);
        $date = date("l F d Y",$row3['timestamp']);

        if (strlen($comment) > 75 || strstr($comment, "<br />") || strstr($comment, "\n")) {
            $comment = substr($comment,0,75) . "...";
            $comment = str_replace("<br />", "", $comment);
            $comment = str_replace("\n", " ", $comment);
        }

        print "<p><a href=\"editcomments.php?id=" . $id . "\">" . $comment . "</a><br />-- Comment by " . $name . " on " . $date;
        print "</p>";
        }
    }


Hope this helped!
Steven
Actually, I figured it out a while ago. Thanks, though!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.