luke777 Posted May 9, 2009 Share Posted May 9, 2009 Hey guys, i didn't want to have to hassle you since you've already helped me so much before but I really am stuck i've tryed a number of things and have got absolutely nowhere :-\ I have been working on a comments system for my site, it writes to a field in a mysql database in this format : %%newitem%%Anonymous user %%seper%% 4%%newitem%%Anonymous user %%seper%% 3 The %%seperator%% is to seperate the username from the comment they left. The script contains a form where they enter text input username and a textarea input for the message. I have got the whole thing working, its just sometimes things arn't outputing right. Anyway heres the script: <?php // the top stuff to recieve post info and add new comment if there is one $commentadd = $_POST["commentadd"]; $commentuser = $_POST["commentuser"]; $usernameurl = $_GET["username"]; include "connect.php"; $query = sprintf("SELECT * FROM `members` WHERE username='%s'", mysql_real_escape_string($usernameurl)); $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $username = $row['username']; $aboutmeta = $row['aboutme']; $comments = $row['comments']; $password = $row['password']; $usercheck = $row['username']; } if (($commentadd!="") && ($commentadd!="Type your message...") && ($commentuser!="")) { $comments = "%%newitem%%$commentuser %%seper%% ".$commentadd."".$comments; $sql = sprintf("UPDATE `members` SET `comments` = '%s' WHERE username = '%s' AND password = '%s'", mysql_real_escape_string($comments), mysql_real_escape_string($username), mysql_real_escape_string($password)); $result = mysql_query($sql); echo "<br><font color='green'><b>Comment added!</font>"; } // the form data and comments displayed echo <<<HERE <br> <div class='ocontentsection'> <div class='ocontenthead'> <center>$username's wall Comments</center> </div> <div class='membertext'> <br><br> <table width="97%" class="contenttable" cellspacing="0"> HERE; list($comment1, $comment2, $comment3, $comment4, $comment5, $comment6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12) = split("%%newitem%%", $comments, 102); $commentarray= array($comment1, $comment2, $comment3, $comment4, $comment5, $commen6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12); foreach($commentarray as $commentitem) { if ($commentitem!="") { list($user, $comment) = split("%%seper%%", $commentitem, 2); echo "<tr><td width='50%' style='padding-left: 100px; padding-top: 0px;'> <div class='membertext'><font color='purple'>$user says:</font></div><td><td width='50%' style='padding-right: 15px; padding-top: 0px;'>$comment<br></td></tr>"; } } echo <<<HERE </table> <center> <form method="post" name="myform"> <br><br> <input type="text" name="commentuser" onclick="document.myform.commentuser.value='';" class="membertext" value="Anonymous user" style="border: 2px #fbcae1 solid;"><br> <textarea cols="50" rows="2" name="commentadd" onclick="document.myform.commentadd.value='';" class="membertext" style="border: 2px #fbcae1 solid;">Type your message...</textarea> <br><input type='submit' name='submit' style='font: bold 14px Arial;width:150px;height:40px' value='Add comment!' /> </form> <br><br> </center> </div> </div> HERE; ?> I have left this page so you can see my problem - http://sweetness341.girldressupgames.net/ Its outputing: Anonymous user says: 5 Anonymous user says: 4 Anonymous user says: 3 Anonymous user says: 2 Where did the "1" message go? the contents of the mysql row is %%newitem%%Anonymous user %%seper%% 5%%newitem%%Anonymous user %%seper%% 4%%newitem%%Anonymous user %%seper%% 3%%newitem%%Anonymous user %%seper%% 2%%newitem%%Anonymous user %%seper%% 1 and it all displayed just fine before i added the "4" comment. Its doing other strange things too like when there are 10 comments missing out the middle one Any help would really be appreciated! Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 I haven't really looked at the problem yet, I'm just curious as to why you're using teh whole %%seperator%% thing. Why not just have 2 fields in the db, user and comment. You retrieve them from the db, and print out, simples Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 i already have a bunch of fields in the database like $username (not for the comment system) $password $avatarurl etc etc and then $comments to hold all the comment information from all users in 1 field, i admit %%sepator%% and %%newitem%% isn't that tidy, i did have line breaks for seperators instead before but i changed back to this while trying to figure out the problem. if anyone has any questions im right here waiting on notifications and will respond right away incase i havnt explained things clearly enough. anyone that can help me with this i'd be so greatful! Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 Why aren't you using a new table for comments, using a third tield to reference the user's id? Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hi gevan im reli not sure how to do that or what u mean ; / Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 I'm kinda going off topic, but it would have been a far better design if you used a different table for comments. So one of your database tables would be user taht hold password, usrname etc... A second table in the database would be comments, this would have 2 fields, comments and username (or and id reference to a user) Then you can get all the comments from one table. Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 this system is working it jus has a small error that im sure someone here could figure out, if you look at the code they don't require a user or pass to post a comment jus a username string which they can make up and the comment then the info is added to that usernames "comment" field on the table. don't worry gevan, im happy to see people are trying to help even if they are off topic , i know i cud hav had a better design but im not that experienced. lets jus get this error fixed :-\ Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 ok, can you change this bit of code list($comment1, $comment2, $comment3, $comment4, $comment5, $comment6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12) = split("%%newitem%%", $comments, 102); $commentarray= array($comment1, $comment2, $comment3, $comment4, $comment5, $commen6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12); foreach($commentarray as $commentitem) { to this list($comment1, $comment2, $comment3, $comment4, $comment5, $comment6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12) = split("%%newitem%%", $comments, 102); $commentarray= array($comment1, $comment2, $comment3, $comment4, $comment5, $commen6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12); var_dump($comments."\n".$commentarray); foreach($commentarray as $commentitem) { And post the text back! Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hi gevans, changed this and its still outputing the same... http://sweetness341.girldressupgames.net/ edt - actually that really messed things up.... i just added a comment " comment 6" and it outputed- string(236) "%%newitem%%Anonymous user %%seper%% comment 6%%newitem%%Anonymous user %%seper%% 5%%newitem%%Anonymous user %%seper%% 4%%newitem%%Anonymous user %%seper%% 3%%newitem%%Anonymous user %%seper%% 2%%newitem%%Anonymous user %%seper%% 1 Array" Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 And post the text back! This isn't a fix, this is investigation Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 It looks to me like it's printing all the comments. I'm not sure what the problem is... You can get rid of var_dump($comments."\n".$commentarray); Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hey gevan ah ok, i just added upto 8 and then suddnly comment 4 dissapeared. this is whats puzzling me http://sweetness341.girldressupgames.net/ ??? Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 change var_dump($comments."\n".$commentarray); to var_dump($commentarray); Let me know when you've done that Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hey gevans ive done that Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 I'm slowly getting lost so lets re'write it a little, try this.. <?php // the top stuff to recieve post info and add new comment if there is one $commentadd = $_POST["commentadd"]; $commentuser = $_POST["commentuser"]; $usernameurl = $_GET["username"]; include "connect.php"; $query = sprintf("SELECT * FROM `members` WHERE username='%s'", mysql_real_escape_string($usernameurl)); $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $username = $row['username']; $aboutmeta = $row['aboutme']; $comments = $row['comments']; $password = $row['password']; $usercheck = $row['username']; } if (($commentadd!="") && ($commentadd!="Type your message...") && ($commentuser!="")) { $comments = "%%newitem%%$commentuser %%seper%% ".$commentadd."".$comments; $sql = sprintf("UPDATE `members` SET `comments` = '%s' WHERE username = '%s' AND password = '%s'", mysql_real_escape_string($comments), mysql_real_escape_string($username), mysql_real_escape_string($password)); $result = mysql_query($sql); echo "<br><font color='green'><b>Comment added!</font>"; } // the form data and comments displayed echo <<<HERE <br> <div class='ocontentsection'> <div class='ocontenthead'> <center>$username's wall Comments</center> </div> <div class='membertext'> <br><br> <table width="97%" class="contenttable" cellspacing="0"> HERE; list($comment1, $comment2, $comment3, $comment4, $comment5, $comment6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12) = split("%%newitem%%", $comments, 102); $commentarray= array($comment1, $comment2, $comment3, $comment4, $comment5, $commen6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12); $commentarray = explode("%%newitem%%", $comments, 12); foreach($commentarray as $commentitem) { if ($commentitem!="") { list($user, $comment) = explode("%%seper%%", $commentitem, 2); echo "<tr><td width='50%' style='padding-left: 100px; padding-top: 0px;'> <div class='membertext'><font color='purple'>$user says:</font></div><td><td width='50%' style='padding-right: 15px; padding-top: 0px;'>$comment<br></td></tr>"; } } echo <<<HERE </table> <center> <form method="post" name="myform"> <br><br> <input type="text" name="commentuser" onclick="document.myform.commentuser.value='';" class="membertext" value="Anonymous user" style="border: 2px #fbcae1 solid;"><br> <textarea cols="50" rows="2" name="commentadd" onclick="document.myform.commentadd.value='';" class="membertext" style="border: 2px #fbcae1 solid;">Type your message...</textarea> <br><input type='submit' name='submit' style='font: bold 14px Arial;width:150px;height:40px' value='Add comment!' /> </form> <br><br> </center> </div> </div> HERE; ?> Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hi gevan, thank you very much, im still trying to figure out how you did that now i jus gotta work on removing the first item when theres 12 or more so that i dont end up with 100's on the page Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 It should work like that now, try it. It only adds 12 into the array Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 hey gevan, yea i thought so too but its grabbing it all into the last array is: Anonymous user says: 12 Anonymous user says: comment 11 Anonymous user says: comment 10 Anonymous user says: comment 9 Anonymous user says: comment8 Anonymous user says: comment7 Anonymous user says: comment 6 Anonymous user says: 5 Anonymous user says: 4 Anonymous user says: 3 Anonymous user says: 2%%newitem%%Anonymous user %%seper%% 1 Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 <?php // the top stuff to recieve post info and add new comment if there is one $commentadd = $_POST["commentadd"]; $commentuser = $_POST["commentuser"]; $usernameurl = $_GET["username"]; include "connect.php"; $query = sprintf("SELECT * FROM `members` WHERE username='%s'", mysql_real_escape_string($usernameurl)); $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $username = $row['username']; $aboutmeta = $row['aboutme']; $comments = $row['comments']; $password = $row['password']; $usercheck = $row['username']; } if (($commentadd!="") && ($commentadd!="Type your message...") && ($commentuser!="")) { $comments = "%%newitem%%$commentuser %%seper%% ".$commentadd."".$comments; $sql = sprintf("UPDATE `members` SET `comments` = '%s' WHERE username = '%s' AND password = '%s'", mysql_real_escape_string($comments), mysql_real_escape_string($username), mysql_real_escape_string($password)); $result = mysql_query($sql); echo "<br><font color='green'><b>Comment added!</font>"; } // the form data and comments displayed echo <<<HERE <br> <div class='ocontentsection'> <div class='ocontenthead'> <center>$username's wall Comments</center> </div> <div class='membertext'> <br><br> <table width="97%" class="contenttable" cellspacing="0"> HERE; list($comment1, $comment2, $comment3, $comment4, $comment5, $comment6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12) = split("%%newitem%%", $comments, 102); $commentarray= array($comment1, $comment2, $comment3, $comment4, $comment5, $commen6, $comment7, $comment8, $comment9, $comment10, $comment11, $comment12); $commentarray = explode("%%newitem%%", $comments); $i=1; foreach($commentarray as $commentitem) { if($i>12) continue; if ($commentitem!="") { list($user, $comment) = explode("%%seper%%", $commentitem, 2); echo "<tr><td width='50%' style='padding-left: 100px; padding-top: 0px;'> <div class='membertext'><font color='purple'>$user says:</font></div><td><td width='50%' style='padding-right: 15px; padding-top: 0px;'>$comment<br></td></tr>"; } $i++; } echo <<<HERE </table> <center> <form method="post" name="myform"> <br><br> <input type="text" name="commentuser" onclick="document.myform.commentuser.value='';" class="membertext" value="Anonymous user" style="border: 2px #fbcae1 solid;"><br> <textarea cols="50" rows="2" name="commentadd" onclick="document.myform.commentadd.value='';" class="membertext" style="border: 2px #fbcae1 solid;">Type your message...</textarea> <br><input type='submit' name='submit' style='font: bold 14px Arial;width:150px;height:40px' value='Add comment!' /> </form> <br><br> </center> </div> </div> HERE; ?> That should do it Quote Link to comment Share on other sites More sharing options...
luke777 Posted May 9, 2009 Author Share Posted May 9, 2009 thank you very much gevan, you have been great now thanks to you my script is working exactly hopw i wanted. much appreciated have a great day thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.