PDA

View Full Version : BBCode trouble


destrugter
07-07-2008, 10:26 PM
Ok, I am making my own forums, and I want to make it to where when the user puts something in, such as [user posts=destrugter] it connects to my database and grabs my posts, and displays them in the post...not the actual posts, the amount of posts...then if I post something after that post it will automatically update with the database. Here is the code I have so far (just ignore the basic bbcode tags).


<?php
function BBCodes($str) {
$str = htmlentities($str);

$simple_search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[smiley\=(.*?)\]/is',
'/\[user posts\=(.*?)\]/is'
);

$simple_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$1">$2</a>',
'<img src="../imgs/smilies/$1.gif"></img>',
Get_Posts("$1")
);

// Do simple BBCode's
$str = preg_replace($simple_search, $simple_replace, $str);

return $str;
}

function Get_Posts($Username) {
$result = mysql_query("SELECT * FROM `Users` WHERE `Username` = '". $Username ."'");
while($row = mysql_fetch_array($result))
{
return $row['Posts'];
}
}
?>



I am not going to actually allow users to post that...it's just a simple thing I started off with to make sure I know how it works for the bigger project. Can someone please tell me what I did wrong?

I have the post as:
Destrugter has: [user posts=destrugter] posts.

When it goes through and replaces for the BBCodes it does this:
Destrugter has: posts.

But when I remove the where section from the mysql_query, it grabs the first username to appear and that works...but I don't want the first user, I want the one specified by the poster.

UnlimitedMB
07-13-2008, 04:51 PM
you cannot loop the return function, though i doubt thats the reason for the failure.

The problem is your use of preg_replace.
You cannot replace with a function.

When you set $simple_replace the Get_Posts function is run at that moment, and as $1 does not yet have a value it is null.

So you are preg_replace'ing with the value of Get_Posts("")

destrugter
07-13-2008, 11:43 PM
ah, I get it. I just put the "return Get_Posts($str);" and it worked, thanks a ton!