Practising how to add custom variable (not working)

Discussion forum for MOD Writers regarding MOD Development.
User avatar
AmigoJack
Registered User
Posts: 6127
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン

Re: Practising how to add custom variable (not working)

Post by AmigoJack »

  1. atoirtap wrote:

    Code: Select all

    	$row =& $rowset[$post_list[$i]];
    	$poster_id = $row['user_id'];
    	
    	// Create the SQL statement
    	$sql= 'SELECT post_id
    		FROM phpbb_posts 
    		WHERE topic_id= '. (int)$topic_id;
    	$result= $db-> sql_query( $sql );
    	while( $row= $db-> sql_fetchrow( $result ) ) {
    First of all you're doing the repeat your mistake of overwriting the $row variable. Stop that.
  2. Second: why would you want to assign your output globally, instead of per post? That's most likely the wrong decision and it proves you yet didn't understand how loops are working.
  3. Third: handing over data to the template must follow the hierarchy: you can't assign iterations of another loop while haven't assigned the current iteration of the parent loop. That's the reason why you won't see assign_block_vars() in the wild, but instead at the end of the big loop in /viewtopic.php:

    Code: Select all

        // Dump vars into template
        $template->assign_block_vars('postrow', $postrow);
    
        if (!empty($cp_row['blockrow']))
        {
            foreach ($cp_row['blockrow'] as $field_data)
            {
                $template->assign_block_vars('postrow.custom_fields', $field_data);
            }
        }
    
        // Display not already displayed Attachments for this post, we already parsed them. ;)
        if (!empty($attachments[$row['post_id']]))
        {
            foreach ($attachments[$row['post_id']] as $attachment)
            {
                $template->assign_block_vars('postrow.attachment', array(
                    'DISPLAY_ATTACHMENT'    => $attachment)
                );
            }
        }
  4. Fouth: loop variables can't have underscores.
  5. Last but not least: use proper HTML.

Let's start fresh. Undo all changes you made and open /viewtopic.php. Find:

Code: Select all

    $row =& $rowset[$post_list[$i]];
    $poster_id = $row['user_id'];
After, add:

Code: Select all

    // My block
    $sql= 'SELECT post_id
        FROM phpbb_posts
        WHERE topic_id= '. (int)$topic_id;
    $result= $db-> sql_query( $sql );
    $aMyblock= array();  // Store in variable, instead of feeding the template instantly
    while( $row2= $db-> sql_fetchrow( $result2 ) ) {  // You see that? Another variable than "$row"
        $aMyblock[]= array( 'TPL_VAR' => $row2['post_id'] );
    }
    $db-> sql_freeresult( $result );
Find:

Code: Select all

    // Dump vars into template
    $template->assign_block_vars('postrow', $postrow);

    if (!empty($cp_row['blockrow']))
    {
        foreach ($cp_row['blockrow'] as $field_data)
        {
            $template->assign_block_vars('postrow.custom_fields', $field_data);
        }
    }

    // Display not already displayed Attachments for this post, we already parsed them. ;)
    if (!empty($attachments[$row['post_id']]))
    {
        foreach ($attachments[$row['post_id']] as $attachment)
        {
            $template->assign_block_vars('postrow.attachment', array(
                'DISPLAY_ATTACHMENT'    => $attachment)
            );
        }
    }
After, add:

Code: Select all

    foreach( $aMyblock as $aBlock ) {  // Now is okay
        $template-> assign_block_vars( 'postrow.myblock', $aBlock );  // No underscore
    }
Open /styles/prosilver/template/viewtopic_body.html and find:

Code: Select all

			<h3 <!-- IF postrow.S_FIRST_ROW -->class="first"<!-- ENDIF -->><!-- IF postrow.POST_ICON_IMG --><img src="{T_ICONS_PATH}{postrow.POST_ICON_IMG}" width="{postrow.POST_ICON_IMG_WIDTH}" height="{postrow.POST_ICON_IMG_HEIGHT}" alt="" /> <!-- ENDIF --><a href="#p{postrow.POST_ID}">{postrow.POST_SUBJECT}</a></h3>
			<p class="author"><!-- IF S_IS_BOT -->{postrow.MINI_POST_IMG}<!-- ELSE --><a href="{postrow.U_MINI_POST}">{postrow.MINI_POST_IMG}</a><!-- ENDIF -->{L_POST_BY_AUTHOR} <strong>{postrow.POST_AUTHOR_FULL}</strong> &raquo; {postrow.POST_DATE} </p>
After, add:

Code: Select all

			<p>Output = <!-- BEGIN myblock --> {postrow.myblock.TPL_VAR} <!-- END myblock --></p>
Tested.
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

Hi amigo, thanks for answering again.
First of all you're doing the repeat your mistake of overwriting the $row variable. Stop that.
I stopped that some 'answers' ago. You saw a version of the file when I haven't been told to chose another variable name yet. Anyway, for a beginner is quite difficult to 'guess' that kind of thing.
Second: why would you want to assign your output globally, instead of per post?
Sorry, I don't understand this.
Third: handing over data to the template must follow the hierarchy: you can't assign iterations of another loop while haven't assigned the current iteration of the parent loop.
My bad. But once again, for a beginner is quite difficult to see this level of things, even more with such file as viewtopic.php
Fouth: loop variables can't have underscores.
No idea about that. A beginner probably can't 'guess' that.
Last but not least: use proper HTML.
Just using basic&quick html code, and giving priority to the viewtopic.php part.

I did the hack, step by step, but still not working properly. The output variable shows all the id's in all the posts, instead of each post's id

Image
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53567
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Practising how to add custom variable (not working)

Post by Brf »

atoirtap wrote: The output variable shows all the id's in all the posts, instead of each post's id
Indeed. That is what a loop does. If you do not want to show every id, you should not be using a loop. The current post's ID is already there without needing any special code. Just use {postrow.POST_ID}
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

Yes, buddy, I know that, I explained it in my first post: I'm using the post id as some kind of example to 'understand' the basics, and then implement a certain code with a similar behaviour.

From my first post:
Hi all. I'm practising how to add custom variables in phpbb, and I'm trying a simple example that consists in show the post id in a topic (just an example, I know there's already a variable for that)
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53567
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Practising how to add custom variable (not working)

Post by Brf »

Then we are not understanding what you are trying to accomplish. If you want to duplicate the {postrow.POST_ID} then you would use the exact same code to create your own{postrow.MY_POST_ID}, which would simply duplicate the code for the other.

find

Code: Select all

'POST_ID'			=> $row['post_id'],
after it put

Code: Select all

'MY_POST_ID'			=> $row['post_id'],
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

No, buddy, I know all that you explained. The important here is not post id variable itself. Remind the title of this topic: 'practising how to add a custom variable'. I'm using post id just as a way to learn the basics and how to add my own custom code. If the output variable example works, then it will work with my custom code and variables.
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

Anyone? amigoJack?
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53567
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Practising how to add custom variable (not working)

Post by Brf »

You still have not said what you are trying to accomplish.
If you put a loop inside a loop it will repeat all of its items on each item in the outer loop.
User avatar
AmigoJack
Registered User
Posts: 6127
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン

Re: Practising how to add custom variable (not working)

Post by AmigoJack »

atoirtap wrote:If the output variable example works
We've gone thru all possibilities, yet you still want another way. In your head it's surely logical, but we're here on the outside. Try to rephrase it - try to paint a picture...
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
  • "My reaction is not to everyone, especially to you." Raptiye, 2021-02-28
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

@brf
Buddy, I have explained it several times in several ways.

@amigoJack
Well, what I'm pretending is that the output variable to show the post id, like this:
Image

Yes, I repeat again, I know there's already a postrow.POST_ID variable for that; I'm just using this example as a base for a similar case with another variables, tables, etc. ;)
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53567
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Practising how to add custom variable (not working)

Post by Brf »

Brf wrote: find

Code: Select all

'POST_ID'			=> $row['post_id'],
after it put

Code: Select all

'MY_POST_ID'			=> $row['post_id'],
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

Brf, I don't want to be impolite, but I have explained several times that I know what you have posted, and that's not what I'm asking help for. Please, don't repeat again.
User avatar
Lumpy Burgertushie
Registered User
Posts: 69228
Joined: Mon May 02, 2005 3:11 am

Re: Practising how to add custom variable (not working)

Post by Lumpy Burgertushie »

the fact that people keep telling you the same thing seems to mean that you are not explaining what you DO want help with very well.
Maybe if you explained exactly what final outcome you are looking for, not generalities, but exactly what you are hoping to accomplish when you are done practicing, then you could get specific answers to specific questions.

robert
Premium phpBB 3.3 Styles by PlanetStyles.net

I am pleased to announce that I have completed the first item on my bucket list. I have the bucket.
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Re: Practising how to add custom variable (not working)

Post by atoirtap »

Hi Lumpy. The thing is that English is not my native language, and my final goal isn't easy for me to explain it. Besides, I know that this forum does not provide code customizations for each user, so, I decided to request help just for a 'basic procedure' (which is quite similar to my final goal and theoretically is easier to explain) and then 'extrapolate' by myself. That 'basic procedure' consist of showing an output variable with the post id of every post in a topic, as I stated several times and depicted in viewtopic.php?f=71&t=2316476&start=15#p14100581

Using simply {postrow.MY_POST_ID} as pointed by Brf, is not valid (as I repeated several times) because I need a 'extendable' and 'reusable' code for my final goal. I proposed by myself a block of code in the first post of this topic, which was improved by amigoJack, but still not getting the requested output as desired.

If anyway, you guys, think that is better to expose my actual final goal, instead of using a less complicate case, I can try.
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: Practising how to add custom variable (not working)

Post by david63 »

Show us the code you have so far and then we will be able to see what you are trying to do
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here

Return to “[3.0.x] MOD Writers Discussion”