News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Help with new custom BBC?

Started by JamzTheMan, September 16, 2011, 04:35:58 AM

Previous topic - Next topic

JamzTheMan

OK, I'm completely stumped.  And I'm new to php/smf mod so be gentle :)

So, in Subs.php I have added:

array(
'tag' => 'xp',
'type' => 'unparsed_equals',
'before' => build_xp_output($user_info['username'], '$1'),
),


I also added the build_xp_output function above the parse_bbc function.

It calls my function on parsing the tag [xp=Karen] just fine but my SQL returns null values.



function build_xp_output($characterUserName, $characterName)
{
$xpQuery = "SELECT SUM(xp_earned), SUM(xp_spent), SUM(xp_secondary_earned), SUM(xp_secondary_spent) FROM character_xp WHERE ";
.
.
.
//$characterName = 'Karen';  <--WORKS IF THIS LINE IS ADDED
$xpQueryFinal = $xpQuery . "character_name = '".$characterName."' and user_name = '".$characterUserName."'";

$xpResult = mysql_query($xpQueryFinal, $wodDbLink);
.
.
.


So I got far enough into the debugging to find that the SQL returns null values (always returns 1 row due to the SUM) if I used the $characterName variable passed in, but if I hard code that variable, everthing runs perfect!  I've output the variable every way I could find and don't see anything wrong with it. I've printed the SQL to the screen, copied, ran it manually, and it works. I've checked the encoding (ASCII) and still stumped.

It's like the $1 passed to the function has some hidden characters in it or something :/

Help?

JamzTheMan

Ok, so using a hexdump function, I now see this:

(first line is right after the function is called, second line is when I reassign the variable to 'Karen' for testing.
0000  24 31                                              $1
0000  4b 61 72 65 6e                                     Karen


So the parse_bbc function is calling my function and just passing a reference of $1? And then I'm passing a reference vs the string contents to my SQL? Is that the issue?

If so, how do I fix it?

Andre N

after this:

array(
'tag' => 'xp',
'type' => 'unparsed_equals',
'before' => build_xp_output($user_info['username'], '$1'),
),

try this:

var_dump($1);
exit();

and you'll probably find that your variable is null.

Also, FYI in Subs.php there is an integration hook for adding BBC codes you could use so you wouldn't have to mess with your Subs.php file :)

look for this line:

// Let mods add new BBC without hassle.
call_integration_hook('integrate_bbc_codes', array(&$codes));

and if you want to go that route you can use this integration hooks skeleton file I just uploaded 5 min ago :)
http://www.simplemachines.org/community/index.php?topic=452678.0
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

JamzTheMan

#3
that just gave me an error:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /.../forums/Sources/Subs.php on line 1853

I get a value in my function, ie on the screen is prints 'Karen' but to the database, it sends '$1'.

It seems $1 is passed by reference to the function? Is there some other/better way to call my function and pass some value from the bbc tag?

Andre N

Ok, let's do it your way. You have your variable ($1) in quotes, which makes it a string.
'before' => build_xp_output($user_info['username'], '$1'),
should be
'before' => build_xp_output($user_info['username'], $1),
notice the no quotes around your variable. You need to set that variable with a value before you call the function.
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

JamzTheMan

OK, well that gives an error to:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/content/.../forums/Sources/Subs.php on line 1850

And it's not really "my" way, I'm just copying existing code bits.  I'd love to use the integration piece but don't even know how to start... I figured if I can just get it working here, I could clean it up later.

I'm assuming the parse_bbc function later replaces/injects this $1 with parsed input from the BBC tag and calls my defined function later?

JamzTheMan

OK never mind, I gave up on this and used the Custom BBC mod to call a JavaScript AJAX call to a php script to give me what I needed.

In the end, much cleaner, all code is external to SMF now.

Advertisement: