News:

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

Main Menu

SQL query inside bbcode creation

Started by Jotade29, April 07, 2024, 11:32:36 PM

Previous topic - Next topic

Jotade29

V: 2.0.19
Hello, i would like to know if when creating a bbcode, i can enter an SQL query to modify the styles. For example:

function test_bbc(array &$bbctest_tags)
{
global $scripturl;

$bbctest_tags[] = array(
'tag' => 'test',
'type' => 'unparsed_equals',
'before' => '<a href="' . $scripturl . '?action=profile;u=$1">user',
'after' => '</a>',
);
}

I would like to add color to the link. For this i have $1 which is equal to the user's id, therefore, it would be enough to do an SQL query to know the color of that member, but if i do the query outside of that array bbcode that i am creating, i get an error in '$1' , that is, it does not bind its ID. When creating it, the href of <a> with $1 correctly shows me the user's id. How can i export the value of $1 within that bccode array to a variable? Thank you!!
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Kindred

$1 is not how you would call the userid...  there are several global variables you can use that are clearly defined... why would you randomly use  $1??
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Jotade29

Quote from: Kindred on April 07, 2024, 11:45:50 PM$1 is not how you would call the userid...  there are several global variables you can use that are clearly defined... why would you randomly use  $1??

Thanks for responding, Kind. For bbcodes, i have seen many codes, and they all use $1 to bind the value being searched. What i want is that if i enter $1 = 2 in the href, and user 2 has the color blue, he should set the color blue, which will be obtained by SQL query.

Please tell me how i could get the color for a query, knowing that $1 gives me the id i want. In other words, how can i make the SQL query within that array?
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Diego Andrés

You can use the 'validate' key to run a query inside an anonymous function.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Jotade29

Quote from: Diego Andrés on April 08, 2024, 01:26:38 AMYou can use the 'validate' key to run a query inside an anonymous function.

Thanks for the support, @Diego Andrés

Could you tell me how could do it?

$bbctest_tags[] = array(
'tag' => 'test',
'type' => 'unparsed_equals',
                'validate' => create_function('', '
                              $prueba = 10;
return $prueba;
'),
'before' => '<a href="' . $scripturl . '?action=profile;u='.$prueba.'">user',
'after' => '</a>',
);

The following code is not working, but i think it is something similar, could you guide me a little more? Thank you!!
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Diego Andrés

create_function is deprecated in PHP 7.2
'validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { query }

SMF Tricks - Free & Premium Responsive Themes for SMF.

Jotade29

Quote from: Diego Andrés on April 09, 2024, 08:44:10 PMcreate_function is deprecated in PHP 7.2
'validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { query }

Thnx for your answeer @Diego Andrés

create_function works for me, because i use a lower version of php. My version of smf is 2.0.19.

I don't know and i don't understand how create_function works or what you told me ('validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { query }). Once i understand it, i will know how to get the query right. So that i understand, could you adapt that code to, for example, return the value '8' in a variable?

That is, in that validate return a variable called, for example, $test, and that has the value of 8. Finally that 8 would have to be passed to ('before' => '<a href="' . $scripturl . '?action=profile;u=$test'">user',)

Thank you so much!!
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Diego Andrés

You don't need to use create_function, a regular anonymous function is already used for the other BBC.

Quote from: Jotade29 on April 09, 2024, 08:52:26 PMI don't know and i don't understand how create_function works or what you told me ('validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { query }). Once i understand it, i will know how to get the query right. So that i understand, could you adapt that code to, for example, return the value '8' in a variable?
https://www.php.net/manual/en/functions.anonymous.php

Just give $data the new value from the query.
$data = 8;

SMF Tricks - Free & Premium Responsive Themes for SMF.

Jotade29

Quote from: Diego Andrés on April 09, 2024, 09:31:39 PMYou don't need to use create_function, a regular anonymous function is already used for the other BBC.

Quote from: Jotade29 on April 09, 2024, 08:52:26 PMI don't know and i don't understand how create_function works or what you told me ('validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { query }). Once i understand it, i will know how to get the query right. So that i understand, could you adapt that code to, for example, return the value '8' in a variable?
https://www.php.net/manual/en/functions.anonymous.php

Just give $data the new value from the query.
$data = 8;

Thns for your answer @Diego Andrés  :)

'validate' => function(&$tag, &$data, $disabled, $params) use ($smcFunc) { $data = 8; },
Give me a error:

Too few arguments to function {closure}(), 3 passed in /public_html/foro/Sources/Subs.php on line 2499 and exactly 4 expected
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Diego Andrés

Check your syntax, use the email bbc as example:
            array(
                'tag' => 'email',
                'type' => 'unparsed_content',
                'content' => '<a href="mailto:$1" class="bbc_email">$1</a>',
                'validate' => function(&$tag, &$data, $disabled)
                {
                    $data = strtr($data, array('<br>' => ''));
                },
            ),

SMF Tricks - Free & Premium Responsive Themes for SMF.

Jotade29

Quote from: Diego Andrés on April 09, 2024, 11:13:25 PMCheck your syntax, use the email bbc as example:
            array(
                'tag' => 'email',
                'type' => 'unparsed_content',
                'content' => '<a href="mailto:$1" class="bbc_email">$1</a>',
                'validate' => function(&$tag, &$data, $disabled)
                {
                    $data = strtr($data, array('<br>' => ''));
                },
            ),


In the other, you say me

"function(&$tag, &$data, $disabled, $params)"

But, $params in 0.19 not exist.

Gracias por tu soporte, ya está arreglado  ;)
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Arantor

In 2.0, it's just function(&$tag, &$data, $disabled)

Advertisement: