PHP 7.4.21 Strange Behaviour inside Wordpress - php

I'm puzzled by the behaviour of PHP 7.4.21. Inside a Wordpress page I have this piece of code:
<span class="team-member-social">
<?php
if(the_field('facebook_link')) { ?>
<i class="<?php the_field('facebook_icon'); ?>"></i>
<?php }
if(the_field('github_link')) { ?>
<i class="<?php the_field('github_icon'); ?>"></i>
<?php }
if(the_field('dribble_link')) { ?>
<i class="<?php the_field('dribble_icon'); ?>"></i>
<?php }
if(the_field('tumblr_link')) { ?>
<i class="<?php the_field('tumblr_icon'); ?>"></i>
<?php } ?>
</span>
the function the_field() is a function of ACF (Advanced Custom Fields) plugin and returns a string with the content the user has set inside the custom field.
The strange behviour I'm referring to is that inside the ifs all the blocks of HTML code (<a...><i...></i></a>) is returned like a simple text containing the value of <?php the_field('XXX_link'); ?> and not in the correct format (a series of social network icons).
When I remove the ifs the pieces of code are shown correctly.
Any suggestion on how this may occur?

the_field() echos the data, you have to use get_field() in the if statement
From the ACF docs here
Please note this function is the same as echo get_field().
like this
<span class="team-member-social">
<?php
if(get_field('facebook_link')) { ?>
<i class="<?php the_field('facebook_icon'); ?>"></i>
<?php }
if(get_field('github_link')) { ?>
<i class="<?php the_field('github_icon'); ?>"></i>
<?php }
if(get_field('dribble_link')) { ?>
<i class="<?php the_field('dribble_icon'); ?>"></i>
<?php }
if(get_field('tumblr_link')) { ?>
<i class="<?php the_field('tumblr_icon'); ?>"></i>
<?php } ?>
</span>

Related

Add to favorites functionality using Jquery in CodeIgniter framework

I am currently working on a tour website, where the number of destinations is available for the user. A Login user can like any destination which will be added to his favorites destination section. I have done this task successfully. But after liked the destination the like button appears multiple times or equal to the number of likes have the user done. Maybe I am wrong in my logic here is my code. The first loop is fetching all the destination and the second loop fetching the favorites destination.
Note:-Here I am showing only logic code not complete HTML or other PHP code
<?php foreach($listing_data as $list){ ?>
<?php foreach($favorites as $fav){ if($fav['link_id']==$list['id']){?>
<li class="pull-right"> <a class="Theme" id="liked"><i class="fas fa-heart"></i> </a> </li>
<?php } else {?>
<li class="pull-right"> <i class="far fa-heart"></i> </li>
<?php } } }?>
Just add a helper variable
and take out the html in 2th foreach.
$like = false; in each Site
Then in each site you loop $favorites, then walk in $favorites for check if TheSite is liked;
after second foreach you should compare if $like is true for use class "fas" or false for use class "far"
<?php foreach($listing_data as $list){
$like = false;
foreach($favorites as $fav){
if($fav['link_id']==$list['id']){
$like = true;
}
}
if($like){ ?>
<li class="pull-right">
<a class="Theme" id="liked"><i class="fas fa-heart"></i></a>
</li>
<?php } else { ?>
<li class="pull-right">
<i class="far fa-heart"></i>
</li>
<?php
}
}
?>
This is just a refactoring of the code to make it look a bit cleaner.
The links are not included as they are ? and also use id's that will not be unique.
So the code could become something like...
<?php foreach ($listing_data as $list): ?>
<?php
foreach ($favorites as $fav) {
$like = ($fav['link_id'] == $list['id']);
}
?>
<li class="pull-right">
<?php if ($like): ?>
<span>More Sensible Link 1</span>
<?php else: ?>
<span>More Sensible Link 2</span>
<?php endif; ?>
</li>
<?php endforeach; ?>

Jquery executing at all times

I have a dropdown switcher in order to change storeviews. I would like to implement a function done with PHP once an option from the dropdown is clicked.
I have been trying to implement this event via Jquery as below:
<?php if (count($block->getGroups()) > 1): ?>
<div class="switcher store switcher-store" id="switcher-store">
<strong class="label switcher-label"><span><?php /* #escapeNotVerified */
echo __('Select Store') ?></span></strong>
<div class="actions dropdown options switcher-options">
<?php foreach ($block->getGroups() as $_group): ?>
<?php if ($_group->getId() == $block->getCurrentGroupId()): ?>
<div class="action toggle switcher-trigger"
role="button"
tabindex="0"
data-mage-init='{"dropdown":{}}'
data-toggle="dropdown"
data-trigger-keypress-button="true"
id="switcher-store-trigger">
<strong>
<span><?php echo $block->escapeHtml($_group->getName()) ?></span>
</strong>
</div>
<?php endif; ?>
<?php endforeach; ?>
<ul class="dropdown switcher-dropdown" data-target="dropdown">
<?php foreach ($block->getGroups() as $_group): ?>
<?php if (!($_group->getId() == $block->getCurrentGroupId())): ?>
<li class="switcher-option">
<a href="#" data-post='<?= $block->getTargetStorePostData($_group->getDefaultStore()); ?>'>
<?php echo $block->escapeHtml($_group->getName()) ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
<script>
require(['jquery', 'jquery/ui'], function($){
$("#switcher-option").trigger("click", clickConfirm());
function clickConfirm(){
var isGood=confirm('You will lose your cart items if you switch between seasons!');
if (isGood) {
<?php $seasonsHelper->getClearCart(); ?>
} else {
<?php $this->getUrl('*');?>
}
}
});
</script>
The store change works perfectly, but the confirm event appears on different places, not only when I select the dropdown button. If I add to cart, it also appears, when I load the page, It also appears.
I've been trying to accurate identifier with ".class" or "#id" but nothing works.
Is this the proper way to do it?
You need to make some changes to your JS code:
require(['jquery', 'jquery/ui'], function($){
if( $("#switcher-option").lengh>0 ){ /// check for element existance
$("#switcher-option").trigger("click", clickConfirm());
}
function clickConfirm(){
var isGood=confirm('You will lose your cart items if you switch between seasons!');
if (isGood) {
<?php $seasonsHelper->getClearCart(); ?>
} else {
<?php $this->getUrl('*');?>
}
}
});
Note: code is not tested, just shared the logic.

OpenCart get Product Viewes

First i should say i couldn't login into OC Support forum somehow, hope get solution here.
Trying to show Product Viewes in Product Page, I googled and found out how can to get another data from OC database and show it up on Product Page, so here is:
controller - product.php:
$data['viewed'] = $product_info['viewed'];
template - product.php;
<?php echo $viewed; ?>
but get error:
Undefined variable: viewed in
/homepages/5/xxxxxx/htdocs/xxxxx/catalog/view/theme/xxxxx/template/product/product.tpl
According to this Post i did right way, but i don't know why got get this error? any idea?
here is part of template code:
<h1><?php echo $heading_title; ?></h1>
<?php echo $viewed; ?>
<ul class="list-unstyled product-info">
<li><b><?php echo $text_stock; ?></b> <span class="Stock"><?php echo $stock; ?></span></li>
<li><b><?php echo $text_model; ?></b> <span class="Model"><?php echo $model; ?></span></li>
<?php if ($manufacturer) { ?>
<li><b><?php echo $text_manufacturer; ?></b> <?php echo $manufacturer; ?></li>
<?php } ?>
<li><b><?php echo $text_category; ?></b> <span>
<?php if( $categories ): ?>
<?php foreach( $categories as $category ): ?>
<?php echo $category['name']; ?><span class="Comma"> ، </span>
<?php endforeach; ?>
<?php endif; ?>
</span></li>
<?php if ($reward) { ?>
<li><b><?php echo $text_reward; ?></b> <span class="reward-points"><i class="fa fa-plus" aria-hidden="true"></i> <?php echo $reward; ?></span></li>
<?php } ?>
</ul>
If you're getting that error the only possible explanation is that the variable is not defined, which logically leads me to the conclusion that the file you edited is not the same controller that's calling your template. This can happen if you are using any OCMOD which has an unchanged version of product.php in the cache, since OCMOD doesn't know about changes you've made until you do a refresh. Do you have any OCMOD installed? Have you tried refreshing the OCMOD cache since you made the change?

PHP ifelse display white screen of death

My code doesnt work properly and it does now shows any error or any clue, what am I missing here ? any hints ? TIA
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php } ?>
<?php
elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
One unnecessary <?php and else if is in wrong way, do like below:-
<?php if(isset($_SESSION['user_session_organizer'])){ ?>
<li>VIEW TRIPS</li>
<?php } elseif(isset($_SESSION['user_session'])){?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
Note:-
Always add
error_reporting(E_ALL);ini_set('display_errors',1);
on top of your each php page to prevent yourself from a situation like:-"White Screen Of Death"
(It will on the error reporting setting for all type of errors and every error will displayed on the page if any occur).
Thanks
Try this, your else if is wrong ?> is a close tag and then the compiler dont understand the elseif, better this:
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php }elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
This is easier to read and to debug:
<?php if (isset($_SESSION['user_session_organizer'])) : ?>
<li>
VIEW TRIPS
</li>
<?php elseif (isset($_SESSION['user_session'])) : ?>
<li>
VIEW TRIPS
</li>
<?php else : ?>
<li>
<a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a>
</li>
<?php endif; ?>
Check about control structures.
try this, its neat and simple code.
<?php if (isset($_SESSION['user_session_organizer'])): ?>
<li>VIEW TRIPS</li>
<?php elseif (isset($_SESSION['user_session'])): ?>
<li>VIEW TRIPS</li>
<?php else: ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php endif; ?>

Error parsing JSON with PHP

I have a problem parsing my JSON result with specific term.
This my JSON sample result : http://bit.ly/1FbZbde
If in "curriculum" I have "__class": "chapter", I'm looking for a result of:
<h2>Title from chapter</h2>
and if I have "__class": "lecture", I'm looking for a result of:
<li>Title from lecture</li>
My code:
<?php foreach($json['curriculum'] as $item) { ?>
<li><i class="fa fa-play-circle"></i> <?php echo $item['title']; ?></li>
<?php } ?>
My results include all titles from chapters and lectures.
I hope I understood your question right.
So if the __class is lecture you want the title in a <li> and if the _class is a chapter, you want the title in a <h2>.
<?php foreach($json['curriculum'] as $item): ?>
<?php if ($item['__class'] == 'chapter'): ?>
<h2><i class="fa fa-play-circle"></i>
<?php echo $item['title']?>
</h2>
<?php elseif ($item['__class'] == 'lecture'): ?>
<li><i class="fa fa-play-circle"></i>
<?php echo $item['title'] ?>
</li>
<?php endif; ?>
<?php endforeach; ?>

Categories