If/then statement on field value in drupal 7 - php

I'm trying to write a statment that looks at the value in a boolean field (field_solo) and returns one of two template files that I have created in Drupal 7.
My field "field_solo" is correctly outputting a value of 0 or 1 and I have cleared the cache.
Can someone tell me if I am doing this correctly? Right now I am not getting it to display when the statement is TRUE.
function motg_preprocess_node(&$vars) {
$node = $vars['node'];
if($node->field_solo[0]['value'] == 1)
{
$vars['theme_hook_suggestion'] = 'node__solo';
} else
{
$vars['theme_hook_suggestion'] = 'node__video';
}
}

Instead of
if($node->field_solo[0]['value'] == 1)
Make it
if($node->field_solo['und'][0]['value'] == 1)
// OR
if($node->field_solo[LANGUAGE_NONE][0]['value'] == 1)

Have a look at this it could be helpful:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

This worked for me
<?php if ($content["field_hide_title"]["#items"][0]["value"] == 0) { ?>
<?php echo $node->title; ?></h2>
<?php } ?>

Related

PHP array selection not working correctly

I am working on a website that alternates section depending on the ID, I created this code but noticed one of my parameters is not working correctly
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
if (get_the_ID() != ('34565' or '34565' or '1212' or '1192' or '1219' or '1180' or '1234' or '1221')) {
include 'facebook.html';}
else {
if (get_the_ID() == '1186') {
include 'facebook-1186.html';}else{
echo do_shortcode('[facebook_card]');}}
The ID pointing to 1186 is ok and the else echoing the shot code is ok BUT the array wit the IDs pointing to facebook.html do not load. Is there something wrong with my syntax? Any help is appreciated
Did you try to use the in_array function?
if (in_array(get_the_ID(), $fb_prac)) {
include 'facebook.html';
}
https://www.php.net/manual/en/function.in-array.php
I'm reading the logic like this, if the id matches 1186 include facebook-1186.html, if the id is NOT in your array $fb_prac, then include facebook.html. Else do the shortcode.
<?php
$fb_prac = array('34565', '34565', '1212', '1192', '1219', '1180', '1234','1186', '1221');
$id = get_the_ID();
if ($id == '1186') {
include 'facebook-1186.html';
}
elseif (!in_array($id, $fb_prac)) {
include 'facebook.html';
}
else {
echo do_shortcode('[facebook_card]');
}

PHP Display Magento Yes/No Attribut if Value is Yes and display an jpg instead of "yes"

I would like to display Yes/No Attributes in my view.phtml. I tried some codes but none is working.
Firstly I tried this syntax, which I found only and which is working for Attributes with Text-Values:
<?php
if ($_product->getAn_342() != null && $_product->getAn_342() != "") {
echo $this->__('Deliveryinformation');
echo $_product->getAn_342();
}
?>
This does not work for Attributes with yes/no Values. With this syntax there is displayed the Magento-ID of the Attribut but not the Value.
Next I tried this Syntax, which I found here in this forum:
<?php
if ($_product->getAttributeText($_data['An_56']) == "Yes"):
?>
But this does not work as well.
For explanation:
One Attribut i want to display would be "Bestseller". The Attribut has Label "an_bestsller" and code "an_56", which will be the magento-Id.
So what exactly is wrong with that yes/no syntax above? Some help would be great.
You can try to do as follows :
$attr = $product->getResource()->getAttribute("oversize_type");
if ($attr->usesSource()) {
$oversizeLabel = $attr->getSource()->getOptionText($oversizeId);
}
Please change oversize_type to your attribute code
Above is the way of getting the option text of dropdown type.
hope this helps.
You've to check different translatable values: 'N/A' or 'NO':
if ($_product->getAttributeText($_data['An_56']) =! $this->__("N/A") && $_product->getAttributeText($_data['An_56'] != $this->__('NO')):
It is better to make an extension which extends Mage_Catalog_Block_Product_View_Attributes and overwrites the function getAdditionalData:
public function getAdditionalData(array $excludeAttr = array())
{
$data = parent::getAdditionalData($excludeAttr);
foreach ($data as $code => $item) {
if ($item['value'] == Mage::helper('catalog')->__('N/A')
|| $item['value'] == Mage::helper('catalog')->__('No')
//You can add more values to hide
//|| $item['value'] == Mage::helper('catalog')->__('--')
) {
unset($data[$code]);
}
}
return $data;
}

php code only work if files has been changed

The variable in the session only changes if i update the file meaning:
<?php
if(isset($_GET['row']) && isset($_GET['quantity'])) {
if($_GET['quantity'] != 0) {
$_SESSION['basket'][$_GET['row']]['barcode']['quantity'] = $_GET['quantity'];
} else {
unset($_SESSION['basket'][$_GET['row']]);
}
}
?>
Changes the quantity first time and then in other tries doesn't.
If I change it to something like:
<?php
if(isset($_GET['row']) && isset($_GET['quantity'])) {
if($_GET['quantity'] != 0) {
$num = $_GET['quantity'];
$_SESSION['basket'][$_GET['row']]['barcode']['quantity'] = $num;
} else {
unset($_SESSION['basket'][$_GET['row']]);
}
}
?>
I save the file in works once and then it stops changing the quantity.
When echoing the variable in the code in shows that it's changed but in the rest of the site it doesn't. the part where it shows doesn't manipulate the variables only prints them.
It seems as if the code works only if the file's been saved and only for the first time.
Thanks in advance.

PHP get parameter pulls in wrong values

I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}

php and $_GET array

I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>

Categories