1 (edited by idimmu 2005-08-12 01:28)

Topic: making a mysql query in a bb code

Basically I'm working on a piece for my forum that is like a dictionary

you post [dict]word[/dict] and when the bb code is processed the definition for that word is selected from the database and used to replace it.

So far my code is like this:

//
// Convert BBCodes to their HTML equivalent
//
function do_bbcode($text)
{

<<<<<<<<<<< SNIP >>>>>>>>>>>>>>>
    $replace = array('<strong>$1</strong>',
                     '<em>$1</em>',
                     '<span class="bbu">$1</span>',
                     'handle_url_tag(\'$1\')',
                     'handle_url_tag(\'$1\', \'$2\')',
                     '<a href="mailto:$1">$1</a>',
                     '<a href="mailto:$1">$2</a>',
                     '<span style="color: $1">$2</span>');

    // This thing takes a while! :)
    $text = preg_replace($pattern, $replace, $text);
    $matches = array ('/\[dict\](.*?)\\[\/dict\]/ise');
    $text = preg_replace($matches, "process_dict('\\1')", $text);

    return $text;
}

//
// Process any dict tags
//
function process_dict($dict)
{
    global $db;

    $t = $db->query('select 1+1 as b');
    $r = $db->fetch_assoc($t);
    $html =    'arse'.$hash.'arse'.$r['b'];

    return $html;
}

as you can see i have added a preg_replace at the bottom of do_bbcode which calls a function to handle the replace, the function does an sql query and returns the replace string

the thing is, when i do this, is all works fine, but then none of the other posts in the thread below the first post that uses [dict] tags are displayed. if i remove the query the posts all show, so its not the additional preg_replace, it is something in the mysql/db class that is interfering with the results that hold the post data

any ideas on how to fix this issue?

2 (edited by flip314 2005-08-15 05:21)

Re: making a mysql query in a bb code

use preg_replace_all instead of preg_replace.  preg_replace stops after the first match.

PS:  php.net is your friend smile


edit: i guess that won't fix your issue (assuming the function gets called once for each post), but it will make multiple [dict] tags in the same post work.

3

Re: making a mysql query in a bb code

I search EXACTLY the same thing smile