Hide a <li tab if a condition is met [duplicate] - php

This question already has answers here:
Hiding a Div using php
(6 answers)
Closed 1 year ago.
I have a foreach look that checks if user has a picture or not. If the user doesn't have the picture I want to hide a <li> tab that shows the picture.
Inside my else how do I say, for this user hide tab 2?
I tried using echo 'id="tabHeader_2" style:"visibility:hidden"; but that doesn't work. I need a reference to that tab2, don't I?
<div id="picture<?php echo $i ?>" style="display: none;">
<?php
$picture = $user->pic;
if(isset($picture))
{
// show picture.
}
else
{
// hide tab2
}
?>
</div>
Then the list of tabs:
<div class="profiles">
<div id="tabContainer">
<div class="tabs">
<ul>
<li id="tabHeader_1"> Profile </li>
<li id="tabHeader_2"> Picture </li>
</ul>
</div>
<div id="tabsContent">
<div class="tabpage" id="tabpage_1"></div>
<div class="tabpage" id="tabpage_2"></div>
</div>
<script src="<?php echo Yii::app()->theme->baseUrl; ?>/js/tabs.js"></script>
</div>
</div>
Picture of what the page looks like:

I am not 100% sure about your code, but you are using isset() which would not be correct, because you set the variable right behind that check so it will be set 100% of the time. You will want to check empty() or is_file() perhaps, depends on what is in that variable.
<!-- This display: none seems strange if you are going to show the wrapped elements -->
<div id="picture<?php echo $i ?>" style="display: none;">
<?php
$picture = $user->pic;
if(!empty($picture)) {
// show picture.
}
else {
// hide tab2
}
?>
</div>
If you put the real code, it would be easier to see how you are laying out your elements.

Do Like This :-
<div class="tabs">
<ul>
<?php
$i = 1;
foreach($yourarray as $key => $value){
if(isset($value) && $value != '')
?>
<li id="tabProfileHeader<?php echo $i;?>"> Profile </li>
<li id="tabPictureHeader<?php echo $i;?>"> Picture </li>
<?php
$i++;
}
?>
</ul>
</div>

Related

dynamic tabs inside Bootstrap 5 card

hope you're doing good !
I'm working on the interface of my activity report based on an SQL database and illustrated with pies created with a JS library "Charts".
So I want to display the different pies (depending of the year) in one bootstrap card with a nav where clickable years will be displayed. Just like the exemple here https://www.codeply.com/p/t6GGNuV2yb
To display the different nav buttons and the pies I'm using foreach php loops ($dataphp is the dictionnary structure that the js chart library requiers). The "active" class should handle the displaying tabs on nav items click but in my case, it doesn't work. In my code I add the class to the current year and this is the result :
https://i.stack.imgur.com/1YmPf.png
Thanks to everyone proposing a solution.
Here is my code :
<div class="card">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs" data-bs-tabs="tabs">
<?php foreach($dataphp as $annee => $value){
echo '<li class="nav-item">
<a class="nav-link '; if($annee==2022) echo 'active'; echo ' " href="#chartdiv'.$annee.'"><strong>'.$annee.'</strong></a>
</li>';
}; ?>
</ul>
</div>
<form class="card-body tab-content graph">
<?php foreach($dataphp as $annee => $value){
echo '<div class="tab-pane '; if($annee==2022) echo 'active'; echo ' graph" id="chartdiv'.$annee.'"></div>';
}; ?>
</form>
</div>

Highlight current item in the heading php

So I was wondering how do you highlight the current page in the heading of the website. I used the include method to call the heading in all of the pages, but how do I highlight the current page while maintaining the include method in every page.
HEADER.PHP
<div id="site-content">
<header class="site-header">
<div class="container">
<a href="index.php" id="branding">
<img src="images/logo.png" alt="Company Name" class="logo" width="100">
<div class="branding-copy">
<h1 class="site-title" style="font-size:60px;">Benedicto Law</h1>
<small class="site-description">Justice In Words</small>
</div>
</a>
<nav class="main-navigation">
<button type="button" class="menu-toggle"><i class="fa fa-bars"></i></button>
<ul class="menu">
<li class="menu-item">Home</li>
<li class="menu-item">Attorney</li>
<li class="menu-item">Service</li>
<li class="menu-item">Contact</li>
</ul>
</nav>
<nav class="mobile-navigation"></nav>
</div>
</header> <!-- .site-header -->
then I only used
<?php include_once('header.php') ?> for calling it to other pages.
I wanted it to highlight the current menu item where the user is in.
For example:
The user pressed Attorney button the Attorney button in the heading should have a highlight.
Any help is appreciated thanks
You have at least 2 options.
1. Pass the data in PHP manually
Before your include(header statement, create a variable for the onPage and set it. Then use it in your include.
For example:
<?php
$onPage = 'attorney';
include_once('header.php') ?>
Then in header.php, check for it like this:
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>
2. Automatically detect it
In header.php
$onPage = $_SERVER['PHP_SELF'];
// make sure it's getting just the page, no directory
$onPage= explode("/",$onPage);
$onPage = $onPage[count($onPage-1)];
// remove .php and make sure its lower case
$onPage = str_replace(".php", "", strtolower($onPage));
// for /directory/Attorney.php this will give you 'attorney'
The check for it like before
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>

add images to slider in php. My code just adds one image

my code only shows one image from the database. I dont know whats the problem. I tried looping it with allthe divs and ul but nothing it only shows one image. my code is
<div class="fullwidthbanner-container">
<div class="fullwidthbanner">
<?php
$about = slider();
while($about_fetch=mysql_fetch_array($about)){
?>
<ul>
<li data-transition="fade" data-slotamount="7">
<img src="images/<?php echo $about_fetch["slide"];?>" alt="">
<?php
}?>
</li>
</ul>
<!--<div class="tp-bannertimer"></div>-->
</div><!-- end .banner -->
</div><!-- end .bannercontainer -->
for the code-part, try this please:
(i assume, you dont want to have several <ul> elements and only the <li>'s)
<div class="fullwidthbanner-container">
<div class="fullwidthbanner">
<ul>
<?php
$about = slider();
while($about_fetch=mysql_fetch_array($about)){ ?>
<li data-transition="fade" data-slotamount="7">
<img src="images/<?php echo $about_fetch["slide"];?>" alt="">
</li>
<?php } ?>
</ul>
</div><!-- end .fullwidthbanner -->
</div><!-- end .fullwidthbanner-container -->
Well, your HTML isn't valid. You're looping over the start of a <ul> and <li> and not closing either until after the loop.
If that isn't the problem, then we can't help you since there is no debugging information available. Break your problem up into pieces. Is the issue server-side or client-side? Look at your output HTML to be sure. What are the return values of mysql_fetch_array() for each iteration? var_dump() is your friend.

Output content based on the users input

I am new to PHP and would like to display some information based on the users input; creating an unordered list on the website with their details.
I have the following scripts in my WordPress theme...
functions.php:
/* Add/remove author profile fields */
function new_contact_methods( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
$contactmethods['google_plus'] = 'Google+';
$contactmethods['twitter'] = 'Twitter';
$contactmethods['facebook'] = 'Facebook';
$contactmethods['linkedin'] = 'Linkedin';
return $contactmethods;
}
add_filter('user_contactmethods','new_contact_methods', 10, 1);
single.php:
<ul style="margin: 0;">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
How would I output the information from the users profile settings:
Only if they have placed content into any of the fields above.
For example, I don't wish to display an unordered list or list items which haven't been place into any of the fields:
https://plus.google.com/example
http://au.linkedin.com/in/example
thanks.
Using the_author_meta() will echo out the value, so you can't really check it ahead of time unless you buffer the output or something. Instead you can use get_the_author_meta. The difference is that instead of echoing the value, it just returns it. So you can look at the value, then use php's empty to see if it has been filled in, so your code might look like this
<ul style="margin: 0;">
<?php
$url=get_the_author_meta('url');
if (!empty($url))
{
?>
<li><a href="<?php the_author_meta('url'); ?>" target="_blank">
<?php the_author_meta('url'); ?></a>
</li>
<?php
}
//...
and a quick way to grab all the properties you want
<?php
$metaInfo=array('url','twitter','google_plus','facebook','linkedin');
$validProperties=0;
foreach($metaInfo as $infoItem)
{
$itemValue=get_the_author_meta($infoItem);
if (!empty($itemValue))
{
$validProperties++; //increments by 1
}
}
?>
<?php
if ($validProperties > 0)
{ //meaning we found at least 1
?>
<ul style="margin: 0;">
<?php
foreach($metaInfo as $infoItem)
{
$itemValue=get_the_author_meta($infoItem);
if (!empty($itemValue))
{
?>
<li>
<a href="<?php the_author_meta($infoItem); ?>" target="_blank">
<?php the_author_meta($infoItem); ?></a>
</li>
<?php
}
}
?>
</ul>
<?php
}
else
{
//else we found none so display nothing! or do something like
echo "no data found!";
}
?>

Condition put on images

I want to put a condition on images come from directory that if the images count is 1 then show this div. But the condition is not running properly after applying the condition on it. Please help me to resolve this problem: My code is given below:
<?php
$id = 115;
$path_image = "pageimage/".$id."/";
if(file_exists($path_image)){
$dir = dir($path_image);
//List files in images directory
while (($file = $dir->read()) !== false){
if($file>=1){
?>
<div id="middleimg" style="background:url(<?php echo $path_image.$file; ?>); background-repeat:no-repeat;">
<div id="nav">
<div id="navinner">
<ul>
<?php
foreach($pageMenu as $pmenu){
$url = generateURL($pmenu->custom_url, $pmenu->page_friendlyname, $pmenu->page_title, $pmenu->page_id);?>
<li><?php echo $pmenu->page_name; ?> </li>
<?php } ?>
<!--<li>HOME</li>
<li>ABOUT</li>
<li>MENU</li>
<li>LOCATION</li>
<li>DELIVERY</li>
<li>SPECIAL OFFER</li>
<li>CONTACT</li>-->
</ul>
</div>
</div>
<div id="banner-containerhome">
<div id="promoBanner">
<!--<div id="pizza">-->
<div id="wowslider-container1">
<div class="ws_images">
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
<span><img src="data1/images/banner.jpg" alt="" title="" id="wows0"/></span>
</div>
<div class="ws_bullets"><div>
<img src="data1/tooltips/banner.jpg" alt=""/>1
<img src="data1/tooltips/chrysanthemum.jpg" alt=""/>2
</div></div>
<div class="ws_shadow"></div>
</div>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- </div>-->
</div>
<div class="rightcontents">
<?php
echo $page_content[1]->page_content;
?>
</div>
</div>
</div>
<?php }}} ?>
There are a few things...
1.- read() reads the directory as an iterator, returning the file name, have you tried to echo $file; ?
if($file >= 1) will never be true, as $file is always a String, never an integer, you are comparing there: if("myfilename.ext" >= 1).
To count the total files you should do a while through the full directory and use a counter, increasing it for each file.
Or you can use scandir("myPath") function, that get all the files in the path specified as an array. for example:
$dir = scandir("myDir");
$total = count($dir); // this gives you the lenght of the array, therefore the files.
While counting, remember that the parent, and root directory are counted as "files" as well, and with some methods also the thumbs.db(windows) or DS_store(osx) are hidden files that store information about how you sort the folder, folder icons and stuff, will be counted as well, so remember to add an If statement to skip them.

Categories