PHP ifelse display white screen of death - php

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; ?>

Related

PHP 7.4.21 Strange Behaviour inside Wordpress

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>

how to make icons with no links invisible to the user

I need help on this please. I want to show only those whose links are not empty. Meaning if there are empty links from the database, that particular list item for where there is an empty link should not show
<?php
foreach ($stmtsocials as $socials) { ?>
<ul class="social-profile social-profile-styled">
<li><i class="facebook-bg"></i></li>
<li><i class="lab la-twitter"></i></li>
<li><i class="lab la-instagram"></i></li>
</ul>
<?php } ?>
One option is that you can do like following using if condition.
<?php
foreach ($stmtsocials as $socials) { ?>
<ul class="social-profile social-profile-styled">
<?php if (!empty($socials['LT_SOC_FB'])){?>
<li><i class="facebook-bg"></i></li>
<?php }?>
<?php if (!empty($socials['LT_SOC_TWITTER'])){?>
<li><i class="lab la-twitter"></i></li>
<?php }?>
<?php if(!empty($socials['LT_SOC_IG'])){?>
<li><i class="lab la-instagram"></i></li>
<?php }?>
</ul>
<?php } ?>
And 2nd option is you can select only those rows which are not empty using query. Then you don't need for if condtion

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; ?>

Active Navigation Bar with PHP?

I'm trying to make an active navigation bar using PHP where the current page will be highlighted or colored in the navigation bar. The code I used:
<ul class="topmenu">
<li <?php if($_GET['page']="home") { ?> class="active" <?php } ?>>
<b>Bienvenue</b>
</li>
<li <?php if($_GET['page']="livres") { ?> class="active" <?php } ?>>
<b>Livres</b>
</li>
<li <?php if($_GET['page']="bibliotheque") { ?> class="active" <?php } ?>>
<b>Bibliothèque</b>
</li>
<li <?php if($_GET['page']="universite") { ?> class="active" <?php } ?>>
<b>L'université</b>
</li>
<li <?php if($_GET['page']="contact") { ?> class="active" <?php } ?>>
<b>Contact</b>
</li>
</ul>
The code will put the attribut in the class active if the page is the current page in the navigation bar. However, all the attributs will be given the class active in this case. How do I fix this?
P.S: I am not looking for any JS or jQuery alternatives, I'm trying to make this work with PHP only.
You could use $_SERVER['SCRIPT_NAME'].
<ul class="topmenu">
<li <?php if($_SERVER['SCRIPT_NAME']=="/home.php") { ?> class="active" <?php } ?>><b>Bienvenue</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/livres.php") { ?> class="active" <?php } ?>><b>Livres</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/bibliotheque.php") { ?> class="active" <?php } ?>><b>Bibliothèque</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/universite.php") { ?> class="active" <?php } ?>><b>L'université</b></li>
<li <?php if($_SERVER['SCRIPT_NAME']=="/contact.php") { ?> class="active" <?php } ?>><b>Contact</b></li>
</ul>
I don't use PHP, but give the current page the active tag, and change it per file. Then a single class definition in the CSS handles changing the color for each page.
HTML:
<nav>
<a class="active" href="/home/">Home</a>
Calendar
Community
</nav>
CSS:
.active {
background-color: #4CAF50;
}
Declare page variables at the very top of each individual page.
Example: <? $page="home";?>
For each list item in your nav bar add if statment with corresponding variable and active class.
Example: <li class="<?if($page=="home"){?>active<?}?>"><b>Bienvenue</b></li>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
I'm using bootstrap 4 class. I've achieved Active Class selection something like this.
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile1.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile1.php">Home
</a>
</li>
<li class="nav-item">
<a
<?php if ($_SERVER['SCRIPT_NAME'] == "/somepath/yourfile2.php") { ?>
class="nav-link active"
<?php } else { ?>
class="nav-link"
<?php } ?>
href="yourfile2.php">Home
</a>
</li>
Repeat this logic for further li tags. Hope this helps someone else.
The code work correctly if you use "==" instead of "=" in The if construct
Еxample:
if($_GET['page'] **==** "home")...
Instead:
if($_GET['page'] **=** "home")...
Hope this helps someone else...

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