Adding custom variable to viewtopic

Discussion forum for MOD Writers regarding MOD Development.
atoirtap
Registered User
Posts: 63
Joined: Thu Aug 01, 2013 8:34 pm

Adding custom variable to viewtopic

Post by atoirtap »

Hi buddies. I'm trying to resolve this question which I'm going to expose.

Let's assume that there's some kind of posts' like/dislike mod installed in a phpbb 3.0.x forum. In any existing topic, the posts would look something like this (with a pair of positive/negative buttons per post):

Image

In this example, let's assume that depicted topic contains 3 posts which ids are 13, 14 and 15.

When any user likes (or dislikes) a certain post, that user can click on the respective button, so the author of that post will gain a positive (or negative) point (this part is not interesting for my question).

The mod has this associated table named phpbb_likes:

Image

The table shows what post (post id) in the whole forum have been liked or disliked, what kind of action (like or dislike) was made and who (what username) made the action.

Now, I want to improve this like/dislike mod, adding a new feature (non existing previously). That feature is to show who liked/disliked the posts. Following the example, and according to the table, I need to get something like this:

Image

My code to get this goal is the next:

In viewtopic.php, before the line // End signature parsing, only if needed I'm adding:

Code: Select all

    // My block
    $sql= 'SELECT user
        FROM phpbb_likes
        WHERE action="like"
        AND post= '. (int) $post_id;
    $result= $db-> sql_query( $sql );
    $aMyblock= array();  
    while( $row2= $db-> sql_fetchrow( $result ) ) {  
        $aMyblock[]= array( 'TPL_VAR' => $row2['user'] );
    }
    $db-> sql_freeresult( $result );
    
Before the line $prev_post_id = $row['post_id']; I'm adding:

Code: Select all

foreach( $aMyblock as $aBlock ) { 
        $template-> assign_block_vars( 'postrow.myblock', $aBlock );
    }
 
In viewtopic_body.html, before the line <!-- IF postrow.S_POST_UNAPPROVED or postrow.S_POST_REPORTED --> I'm adding:

Code: Select all

<p>Liked by: <!-- BEGIN myblock --> {postrow.myblock.TPL_VAR} <!-- END myblock --></p>
But, I'm getting the desired result repeated in all posts of the topic, not just in #14
Image

Any suggestion?
User avatar
AmigoJack
Registered User
Posts: 6127
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン

Re: Adding custom variable to viewtopic

Post by AmigoJack »

atoirtap wrote:In viewtopic.php, before the line // End signature parsing, only if needed I'm adding:

Code: Select all

    // My block
    $sql= 'SELECT user
        FROM phpbb_likes
        WHERE action="like"
        AND post= '. (int) $post_id;
$post_id never changes in that loop, it is a global one - hence your output is always the same for each post. Use $row['post_id'] instead, just like all the code in the loop does.
  • "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: Adding custom variable to viewtopic

Post by atoirtap »

Nice, it (almost) works!! Now, I'm getting this:

Image

Now, to avoid show the string 'Liked by:' when there's no likes in a post, I've thinking about adding a 'control variable' (CVAR), like this:

In viewtopic.php

Code: Select all

    // My block
    $sql= 'SELECT user
        FROM phpbb_likes
        WHERE action="like"
        AND post= '. (int) $row['post_id'];
    $result= $db-> sql_query( $sql );
    $aMyblock= array(); 
    $iRow= 0; 
    while( $row2= $db-> sql_fetchrow( $result ) ) {  
        $aMyblock[]= array( 'TPL_VAR' => $row2['user'] );
    $iRow++;
    }
    
    $template-> assign_vars 
	( array
		( 'CVAR'=> $iRow
		)
	);
	
    $db-> sql_freeresult( $result );
   
In viewtopic_body.html

Code: Select all

<p><!-- IF CVAR !== '' -->Liked by:<!-- ENDIF --><!-- BEGIN myblock --> {postrow.myblock.TPL_VAR} <!-- END myblock --></p>
But it's not working. Does it make sense?
User avatar
AmigoJack
Registered User
Posts: 6127
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン

Re: Adding custom variable to viewtopic

Post by AmigoJack »

No, it doesn't make sense - it looks like a wild guess instead of a decision based on something you learned. Consider reading /docs/coding-guidelines.html

Code: Select all

<-- IF .postrow.myblock -->
  <p>Liked by: <!-- BEGIN myblock -->
    {postrow.myblock.TPL_VAR}<!-- IF not postrow.myblock.S_LAST_ROW -->, <!-- ENDIF -->
  <!-- END myblock --></p>
<!-- ENDIF -->
  • "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: Adding custom variable to viewtopic

Post by atoirtap »

Oh, I see, very elegant solution.

Thanks buddy for answering and for your help. Now it works as intended. :)

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