Jump to content

[SOLVED] newbies needs help with list() after 3 hours of getting nowhere (no lie)


luke777

Recommended Posts

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!

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 :)

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!

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.

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  :-\

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!

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"

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;


?>

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.