vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How do i Check a post ID is odd or even? (https://vborg.vbsupport.ru/showthread.php?t=284736)

Neo_Angelo 06-21-2012 05:14 AM

How do i Check a post ID is odd or even?
 
Ok i'm pretty new to the whole coding business and i'm trying to figure out what i'm doing wrong, basically what i'd like to do is create a check for the post ID that determines whether the post ID is odd or even and output two different things depending on the outcome, for example i want the Post layout to have the postbit information on the left if the post ID is odd and the postbit info on the right if its even.

Heres what i mean:

If post ID = even

User info | Post

If post = odd

Post | Userinfo

heres the code i've been working on so far and i do appologise for the exceedingly appalling use of code, can someone walk me through what i've done wrong and explain to me how i should go about it? i'd really appreciate it as i want to learn. I've been using W3 schools as a reference.

Code:

// Function to determine odd or even
function is_odd( $int )
{
  return( $int & 1 );
}


<vb:if( is_odd($postid) )>

  <div class="postdetails">
                <div class="userinfo">
                        <div class="username_container">
                        <vb:if condition="$post['userid']">
                                {vb:raw memberaction_dropdown}
                                {vb:raw post.onlinestatus}
                        <vb:else />
                                <span class="username guest">{vb:raw post.musername}</span>
                        </vb:if>
                        </div>
                        <span class="usertitle">
                                {vb:raw post.usertitle}
                        </span>
                        <vb:if condition="$post['rank']">
                                <span class="rank">{vb:raw post.rank}</span>
                        </vb:if>
                        {vb:raw template_hook.postbit_userinfo_left}
                        <vb:if condition="$show['reputation']">
                                <span class="postbit_reputation" id="repdisplay_{vb:raw post.postid}_{vb:raw post.userid}" title="{vb:raw post.username} {vb:raw post.level}">
                                        <vb:each from="reputationdisplay" value="row">
                                                <img class="{vb:raw row.class}" src="{vb:stylevar imgdir_reputation}/reputation_{vb:raw row.posneg}{vb:raw row.imgext}" alt="" />{vb:raw row.rowend}
                                        </vb:each>
                                </span>
                        </vb:if>
                        <vb:if condition="$show['avatar']">
                        <a class="postuseravatar" href="{vb:link member, {vb:raw post}}" title="{vb:rawphrase {vb:raw post['onlinestatusphrase']}, {vb:raw post.username}}">
                                <img src="{vb:raw post.avatarurl}" alt="{vb:rawphrase xs_avatar, {vb:raw post.username}}" title="{vb:rawphrase xs_avatar, {vb:raw post.username}}" />
                        </a>
                        </vb:if>
                        <vb:if condition="$post['userid']">
                                <hr />
                                <dl class="userinfo_extra">
                                        <vb:if condition="$post['joindate']"><dt>{vb:rawphrase join_date}</dt> <dd>{vb:raw post.joindate}</dd></vb:if>
                                        <vb:if condition="$post['field2']"><dt>{vb:rawphrase location_perm}</dt> <dd>{vb:raw post.field2}</dd></vb:if>
                                        <vb:if condition="$post['age']"><dt>{vb:rawphrase age}</dt> <dd>{vb:raw post.age}</dd></vb:if>
                                        <dt>{vb:rawphrase posts}</dt> <dd>{vb:raw post.posts}</dd>       
                                        {vb:raw template_hook.postbit_userinfo_right_after_posts}
                                </dl>
                                <vb:if condition="$show['infraction'] OR $show['reppower']">
                                <dl class="user_rep">
                                        <vb:if condition="$show['infraction']">
                                        <dt>{vb:rawphrase infractions}</dt>
                                        <dd>{vb:raw post.warnings}/{vb:raw post.infractions} ({vb:raw post.ipoints})</dd>
                                        </vb:if>
                                        <vb:if condition="$show['reputation']">
                                                <vb:if condition="$show['reppower']">
                                        <dt>{vb:rawphrase reppower}</dt>
                                        <dd id="reppower_{vb:raw post.postid}_{vb:raw post.userid}">{vb:raw post.reppower}</dd>
                                                </vb:if>
                                        </vb:if>
                                </dl>
                                </vb:if>
                                {vb:raw template_hook.postbit_userinfo_right}
                                <div class="imlinks">
                                        {vb:raw post.icqicon} {vb:raw post.aimicon} {vb:raw post.msnicon} {vb:raw post.yahooicon} {vb:raw post.skypeicon}
                                </div>
                        </vb:if>
                </div>

<vb:else />

<div align="right">
                <div class="userinfo">
                        <div class="username_container">
                        <vb:if condition="$post['userid']">
                                {vb:raw memberaction_dropdown}
                                {vb:raw post.onlinestatus}
                        <vb:else />
                                <span class="username guest">{vb:raw post.musername}</span>
                        </vb:if>
                        </div>
                        <span class="usertitle">
                                {vb:raw post.usertitle}
                        </span>
                        <vb:if condition="$post['rank']">
                                <span class="rank">{vb:raw post.rank}</span>
                        </vb:if>
                        {vb:raw template_hook.postbit_userinfo_left}
                        <vb:if condition="$show['reputation']">
                                <span class="postbit_reputation" id="repdisplay_{vb:raw post.postid}_{vb:raw post.userid}" title="{vb:raw post.username} {vb:raw post.level}">
                                        <vb:each from="reputationdisplay" value="row">
                                                <img class="{vb:raw row.class}" src="{vb:stylevar imgdir_reputation}/reputation_{vb:raw row.posneg}{vb:raw row.imgext}" alt="" />{vb:raw row.rowend}
                                        </vb:each>
                                </span>
                        </vb:if>
                        <vb:if condition="$show['avatar']">
                        <a class="postuseravatar" href="{vb:link member, {vb:raw post}}" title="{vb:rawphrase {vb:raw post['onlinestatusphrase']}, {vb:raw post.username}}">
                                <img src="{vb:raw post.avatarurl}" alt="{vb:rawphrase xs_avatar, {vb:raw post.username}}" title="{vb:rawphrase xs_avatar, {vb:raw post.username}}" />
                        </a>
                        </vb:if>
                        <vb:if condition="$post['userid']">
                                <hr />
                                <dl class="userinfo_extra">
                                        <vb:if condition="$post['joindate']"><dt>{vb:rawphrase join_date}</dt> <dd>{vb:raw post.joindate}</dd></vb:if>
                                        <vb:if condition="$post['field2']"><dt>{vb:rawphrase location_perm}</dt> <dd>{vb:raw post.field2}</dd></vb:if>
                                        <vb:if condition="$post['age']"><dt>{vb:rawphrase age}</dt> <dd>{vb:raw post.age}</dd></vb:if>
                                        <dt>{vb:rawphrase posts}</dt> <dd>{vb:raw post.posts}</dd>       
                                        {vb:raw template_hook.postbit_userinfo_right_after_posts}
                                </dl>
                                <vb:if condition="$show['infraction'] OR $show['reppower']">
                                <dl class="user_rep">
                                        <vb:if condition="$show['infraction']">
                                        <dt>{vb:rawphrase infractions}</dt>
                                        <dd>{vb:raw post.warnings}/{vb:raw post.infractions} ({vb:raw post.ipoints})</dd>
                                        </vb:if>
                                        <vb:if condition="$show['reputation']">
                                                <vb:if condition="$show['reppower']">
                                        <dt>{vb:rawphrase reppower}</dt>
                                        <dd id="reppower_{vb:raw post.postid}_{vb:raw post.userid}">{vb:raw post.reppower}</dd>
                                                </vb:if>
                                        </vb:if>
                                </dl>
                                </vb:if>
                                {vb:raw template_hook.postbit_userinfo_right}
                                <div class="imlinks">
                                        {vb:raw post.icqicon} {vb:raw post.aimicon} {vb:raw post.msnicon} {vb:raw post.yahooicon} {vb:raw post.skypeicon}
                                </div>
                        </vb:if>
                </div>

</vb:if>


cellarius 06-21-2012 06:09 AM

You can't use PHP in a template. You need to put your PHP code in a plugin.

Anyway, what are you trying to achieve? You do realize that the post-IDs in a thread are not consecutive - just in case you want to have userinfo alternating from left to right.

Neo_Angelo 06-21-2012 06:15 AM

Quote:

Originally Posted by cellarius (Post 2341651)
You can't use PHP in a template. You need to put your PHP code in a plugin.

Anyway, what are you trying to achieve? You do realize that the post-IDs in a thread are not consecutive - just in case you want to have userinfo alternating from left to right.

thats exactly what i want to do, i want to have my userinfo alternating from left to right with each post, i figured an If statement might of been the answer. whats the best way to go about it?

cellarius 06-21-2012 06:22 AM

Well, an if statement is the way to go, but you can't use the postid. Use postcount instead. Try this:

Code:

<vb:if condition="$post['postcount'] % 2">
    odd
<vb:else />
    even
</vb:if>


Neo_Angelo 06-21-2012 06:24 AM

Quote:

Originally Posted by cellarius (Post 2341655)
Well, an if statement is the way to go, but you can't use the postid. Use postcount instead. Try this:

Code:

<if condition="$post['postcount'] % 2">
    odd
<else />
    even
</if>


i'll give it a try thank you very much for the help i'll post back if it worked or not :)

Code:

<if condition="$post['postcount'] % 2">

        <div class="postdetails">
                <div class="userinfo">
                        <div class="username_container">
                        <vb:if condition="$post['userid']">
                                {vb:raw memberaction_dropdown}
                                {vb:raw post.onlinestatus}
                        <vb:else />
                                <span class="username guest">{vb:raw post.musername}</span>
                        </vb:if>
                        </div>
                        <span class="usertitle">
                                {vb:raw post.usertitle}
                        </span>
                        <vb:if condition="$post['rank']">
                                <span class="rank">{vb:raw post.rank}</span>
                        </vb:if>
                        {vb:raw template_hook.postbit_userinfo_left}
                        <vb:if condition="$show['reputation']">
                                <span class="postbit_reputation" id="repdisplay_{vb:raw post.postid}_{vb:raw post.userid}" title="{vb:raw post.username} {vb:raw post.level}">
                                        <vb:each from="reputationdisplay" value="row">
                                                <img class="{vb:raw row.class}" src="{vb:stylevar imgdir_reputation}/reputation_{vb:raw row.posneg}{vb:raw row.imgext}" alt="" />{vb:raw row.rowend}
                                        </vb:each>
                                </span>
                        </vb:if>
                        <vb:if condition="$show['avatar']">
                        <a class="postuseravatar" href="{vb:link member, {vb:raw post}}" title="{vb:rawphrase {vb:raw post['onlinestatusphrase']}, {vb:raw post.username}}">
                                <img src="{vb:raw post.avatarurl}" alt="{vb:rawphrase xs_avatar, {vb:raw post.username}}" title="{vb:rawphrase xs_avatar, {vb:raw post.username}}" />
                        </a>
                        </vb:if>
                        <vb:if condition="$post['userid']">
                                <hr />
                                <dl class="userinfo_extra">
                                        <vb:if condition="$post['joindate']"><dt>{vb:rawphrase join_date}</dt> <dd>{vb:raw post.joindate}</dd></vb:if>
                                        <vb:if condition="$post['field2']"><dt>{vb:rawphrase location_perm}</dt> <dd>{vb:raw post.field2}</dd></vb:if>
                                        <vb:if condition="$post['age']"><dt>{vb:rawphrase age}</dt> <dd>{vb:raw post.age}</dd></vb:if>
                                        <dt>{vb:rawphrase posts}</dt> <dd>{vb:raw post.posts}</dd>       
                                        {vb:raw template_hook.postbit_userinfo_right_after_posts}
                                </dl>
                                <vb:if condition="$show['infraction'] OR $show['reppower']">
                                <dl class="user_rep">
                                        <vb:if condition="$show['infraction']">
                                        <dt>{vb:rawphrase infractions}</dt>
                                        <dd>{vb:raw post.warnings}/{vb:raw post.infractions} ({vb:raw post.ipoints})</dd>
                                        </vb:if>
                                        <vb:if condition="$show['reputation']">
                                                <vb:if condition="$show['reppower']">
                                        <dt>{vb:rawphrase reppower}</dt>
                                        <dd id="reppower_{vb:raw post.postid}_{vb:raw post.userid}">{vb:raw post.reppower}</dd>
                                                </vb:if>
                                        </vb:if>
                                </dl>
                                </vb:if>
                                {vb:raw template_hook.postbit_userinfo_right}
                                <div class="imlinks">
                                        {vb:raw post.icqicon} {vb:raw post.aimicon} {vb:raw post.msnicon} {vb:raw post.yahooicon} {vb:raw post.skypeicon}
                                </div>
                        </vb:if>
                </div>

<else />

<div align="right">
                <div class="userinfo">
                        <div class="username_container">
                        <vb:if condition="$post['userid']">
                                {vb:raw memberaction_dropdown}
                                {vb:raw post.onlinestatus}
                        <vb:else />
                                <span class="username guest">{vb:raw post.musername}</span>
                        </vb:if>
                        </div>
                        <span class="usertitle">
                                {vb:raw post.usertitle}
                        </span>
                        <vb:if condition="$post['rank']">
                                <span class="rank">{vb:raw post.rank}</span>
                        </vb:if>
                        {vb:raw template_hook.postbit_userinfo_left}
                        <vb:if condition="$show['reputation']">
                                <span class="postbit_reputation" id="repdisplay_{vb:raw post.postid}_{vb:raw post.userid}" title="{vb:raw post.username} {vb:raw post.level}">
                                        <vb:each from="reputationdisplay" value="row">
                                                <img class="{vb:raw row.class}" src="{vb:stylevar imgdir_reputation}/reputation_{vb:raw row.posneg}{vb:raw row.imgext}" alt="" />{vb:raw row.rowend}
                                        </vb:each>
                                </span>
                        </vb:if>
                        <vb:if condition="$show['avatar']">
                        <a class="postuseravatar" href="{vb:link member, {vb:raw post}}" title="{vb:rawphrase {vb:raw post['onlinestatusphrase']}, {vb:raw post.username}}">
                                <img src="{vb:raw post.avatarurl}" alt="{vb:rawphrase xs_avatar, {vb:raw post.username}}" title="{vb:rawphrase xs_avatar, {vb:raw post.username}}" />
                        </a>
                        </vb:if>
                        <vb:if condition="$post['userid']">
                                <hr />
                                <dl class="userinfo_extra">
                                        <vb:if condition="$post['joindate']"><dt>{vb:rawphrase join_date}</dt> <dd>{vb:raw post.joindate}</dd></vb:if>
                                        <vb:if condition="$post['field2']"><dt>{vb:rawphrase location_perm}</dt> <dd>{vb:raw post.field2}</dd></vb:if>
                                        <vb:if condition="$post['age']"><dt>{vb:rawphrase age}</dt> <dd>{vb:raw post.age}</dd></vb:if>
                                        <dt>{vb:rawphrase posts}</dt> <dd>{vb:raw post.posts}</dd>       
                                        {vb:raw template_hook.postbit_userinfo_right_after_posts}
                                </dl>
                                <vb:if condition="$show['infraction'] OR $show['reppower']">
                                <dl class="user_rep">
                                        <vb:if condition="$show['infraction']">
                                        <dt>{vb:rawphrase infractions}</dt>
                                        <dd>{vb:raw post.warnings}/{vb:raw post.infractions} ({vb:raw post.ipoints})</dd>
                                        </vb:if>
                                        <vb:if condition="$show['reputation']">
                                                <vb:if condition="$show['reppower']">
                                        <dt>{vb:rawphrase reppower}</dt>
                                        <dd id="reppower_{vb:raw post.postid}_{vb:raw post.userid}">{vb:raw post.reppower}</dd>
                                                </vb:if>
                                        </vb:if>
                                </dl>
                                </vb:if>
                                {vb:raw template_hook.postbit_userinfo_right}
                                <div class="imlinks">
                                        {vb:raw post.icqicon} {vb:raw post.aimicon} {vb:raw post.msnicon} {vb:raw post.yahooicon} {vb:raw post.skypeicon}
                                </div>
                        </vb:if>
                </div>

</if>

i added your code to the template and i seem to be getting the following error:


Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3965

The following error occurred when attempting to evaluate this template:
%1$s

any idea's?

cellarius 06-21-2012 06:26 AM

Please note that I edited my code (added vB4-namespace).

Neo_Angelo 06-21-2012 06:40 AM

Quote:

Originally Posted by cellarius (Post 2341657)
Please note that I edited my code (added vB4-namespace).

ahh yes that old fruitcake of a thing, i re-edited my code to work using the vb4 coding and it parsed fine, it seems to have worked but hasn't moved my userinfo to the right possibly because i've done something wrong in my aligning. every second post has a massive white gap in the userbit ahahaha thus is the joys of coding.

Thank you so much for the help so far i knew i was on the right lines as such, i'm just not that experienced yet (as you can tell).


All times are GMT. The time now is 06:48 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01176 seconds
  • Memory Usage 1,836KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_code_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete