PDA

View Full Version : Help with if condition


ragarcia87
01-07-2010, 01:12 AM
Hello I have this array

if ( empty($array['thread_link']) ){
$array['thread_link'] = 0;
}

and it is defined by

'thread' => $array['thread_link'],

I am trying to create the condition

<vb:if condition="$rep.thread != 0">
<td style="padding : 3px;"><a href="{$rep.thread}">View</a></td>
<vb:else />
<td style="padding : 3px;">n/a</td>
</vb:if>

but it doesnt work and only shows "n/a" on everyone where it should show a thread link for some.

is there another way i can say if $rep.thread doenst = 0 then show the thread link?

BBR-APBT
01-07-2010, 02:32 AM
Where do you have $rep.thread defined in your code?

If your not sure if your variable is working try to use var_dump($var);
if it returns NULL you have a problem unless it is supposed to return NULL.

Lynne
01-07-2010, 02:37 AM
You condition should be:
<vb:if condition="$rep['thread'] != 0">

(Or maybe $thread['rep']?) But like BBR, I have no idea what that condition has to do with the other information you posted.

consolegaming
01-07-2010, 07:16 AM
I think you need to provide more info than you have already. Is those first 2 sets of codes from plugins ye? And if so I presume you're trying to get that variable into the if statement ye?

I think it all depends on how this $rep['thread'] or $rep.thread is defined. If you're trying to define it inside a plugin then it needs to be registered or pre-registered (if used in an existing template). And then the format for using the variable (outside of the if statements as if statements are different) would be more like {vb:raw $rep.thread} (for your a href link.

But yeah for the if statement you cant use the same format. You do it the same way as you would use to. So yeah what Lynne suggested should be what you need for the if statement. Though it would still need to be registered inside the plugin. There's a few good vb4 articles which shoudl help:

https://vborg.vbsupport.ru/showthread.php?t=228078
https://vborg.vbsupport.ru/showthread.php?t=231525

ragarcia87
01-07-2010, 12:35 PM
I am sorry. Thank you consolegaming for the links I am reading them now. Oh yes I have seen these. Thanks though. :D

Here is the page, view_rep.php

<?php
/**
*@todo clean up
*/

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT', 'view_rep');
define('CSRF_PROTECTION', true);
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('view_userrep',
);

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################


// ###### YOUR CUSTOM CODE GOES HERE #####
$pagetitle = 'Viewing rep';
global $vbulletin;
$vbulletin->input->clean_array_gpc('g', array('userid' => TYPE_INT));
$db = $vbulletin->db;

$UserID = $vbulletin->GPC['userid'];
// Get username
$UserResult = $db->query_read("SELECT username FROM user WHERE userid = {$UserID} LIMIT 1");
$Username = current($db->fetch_array($UserResult)); // No need for array to cached in php since it only has one value.


$result = $db->query_read("SELECT repping_userid,rep_value,message,reason,thread_lin k FROM userrep WHERE repped_userid = {$UserID} ORDER BY timestamp DESC");

$arRep = array();

while ($array = $db->fetch_array($result))
{
// this code sucks!
if ($array['rep_value'] == 1){
// Postive show + img
$RatedIMG = "/images/pos-rep.png";
$RatedText = "Postive";
}
elseif ($array['rep_value'] == -1) {
// Negative show - img
$RatedIMG = "/images/neg-rep.png";
$RatedText = "Negative";
}

if ( empty($array['thread_link']) ){
$array['thread_link'] = 0;
}


$UserLinkResult = $db->query_read("SELECT username FROM user WHERE userid = {$array['repping_userid']} LIMIT 1"); // get a link to user who did the rating.
$arUsername = $db->fetch_array($UserLinkResult);
if ( empty($arUsername) ){
$strUsername = "System";
$UserLink = "/";
}
else{
$strUsername = current($arUsername); // No need for array to cached in php since it only has one value.
$UserLink = "/member.php?{$array['repping_userid']}-{$strUsername}";
}

$arRep[] = array('ratedimg' => $RatedIMG,
'ratedtext' => $RatedText,
'message' => $array['message'],
'reason' => $array['reason'],
'thread' => $array['thread_link'],
'userlink' => $UserLink,
'username' => $strUsername);


}

// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######
$navbits = construct_navbits(array("view_rep.php?$session[sessionurl]u={$UserID}" => $vbphrase['irep'],'' => $Username));
$navbar = render_navbar_template($navbits);
$templater = vB_Template::create('view_userrep');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('reparray', $arRep);
$templater->register('username', $Username);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());

?>



And here is the whole template page: view_userrep

{vb:stylevar htmldoctype}
<html xmlns="https://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}">
<head>
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<!-- end no cache headers -->
{vb:raw headinclude}
<title>{vb:raw pagetitle}</title>
</head>
<body>

{vb:raw header}
{vb:raw navbar}

<div class="blockhead">{$username}'s iRep Profile</div>
<div class="blockbody">
<div class="blockrow">
<table cellpadding="5" cellspacing="1" border="1" width="100%" style="border: 1px solid #333333;">
<tr>
<th width="10%" style="text-align: center; font-weight: bold;">From</th>
<th width="20" style="text-align: center; font-weight: bold;">Rating</th>
<th width="10%" style="text-align: center; font-weight: bold;">Reason</th>
<th width="10%" style="text-align: center; font-weight: bold;">Thread URL</th>
<th width="70%" style="text-align: center; font-weight: bold;">Comment</th>
</tr>
<vb:each from="reparray" value="rep">
<tr>

<vb:if condition="$rep.username">
<td style="padding : 3px;"><a href="{vb:var rep.userlink}" title="{$rep.username} user profile">{$rep.username}</a></td>
<vb:else />
<td style="padding : 3px;font-weight:bold;text-align:center;">System</td>
</vb:if>


<td style="text-align: center;"><img src="{vb:var rep.ratedimg}" alt="{vb:var rep.ratedtext}" /></td>
<td style="padding : 3px;">{vb:var rep.reason}</td>

<vb:if condition="$rep.thread != 0">
<td style="padding : 3px;"><a href="{$rep.thread}">View</a></td>
<vb:else />
<td style="padding : 3px;">n/a</td>
</vb:if>

<td>{vb:var rep.message}</td>

</tr>
</vb:each>
</table>

</div>
</div>

{vb:raw footer}
</body>
</html>


If I change it to:
<vb:if condition="$rep.thread">
<td style="padding : 3px;"><a href="{$rep.thread}">View</a></td>
</vb:if>

then it works and shows the thread link but some of them do not have values and need to show "n/a"