suggest proper conditional statement to fade tabs with CSS via PHP - php

I want to change the class of two tabs via PHP, but I am stuck in basic condition. here is my code:
CSS
.myinfo { background-color:black }
.deactive { background-color : white }
HTML
<li class="myinfo <?=$deact?>">
<a href="myaccount.php?<?=$qry_str?>" >My Info</a>
</li>
<li class="myinfo <?=$deact?>">
My Contacts
</li>
What I need is
if $_GET['mycontacts'] is active
then My Info link should have class deactive
otherwise My Contacs link should have class deactive
I tried this:
if (isset($_GET['mycontacts'])){
$deact ='deactive';
}
But it did not succeed. Please help to write this condition (I think a one line ternary condition could work).

Try this:
<li class="myinfo <?php echo ($_GET['mycontacts'] === 'active' ? 'deactive' : '') ?>">
<a href="myaccount.php?<?=$qry_str?>" >My Info</a>
</li>
<li class="myinfo <?php echo ($_GET['mycontacts'] !== 'active' ? 'deactive' : '') ?>">
My Contacts
</li>
By the way I hate php short tags! Personal preference...

Related

How to know which screen is active if I use templates with PHP

I am using a template where the menu of my website goes, however I want to add the bootstrap active class to it so that the user knows which page he is on. At the moment the way I do it is the following:
Note that the "templates" file has several functions with an echo to display the html. Outside of the function that displays the HTML I call this option that PHP gives us.
$activePage = basename($_SERVER['PHP_SELF'], ".php");
Then I create a function to show the menu that looks like this, clearly it has more options, but to avoid putting so much code I only show this one.
function MostrarMenu(){
echo
'
<li class="nav-item">
<a class="nav-link <?= ($activePage == about) active ?>" href="about.php">Sobre nosotros</a>
</li>
';
Then in the view I call this function so that it shows the menu
<?php MostrarMenu(); ?>
$activePage = $_SERVER['REQUEST_URI'];
function MostrarMenu(){
$Active = ($activePage == '/about') ? "active": "";
echo
'
<li class="nav-item">
<a class="nav-link '.$Active.'" href="about.php">Sobre nosotros</a>
</li>
';
}

Multiple if/then arguments in PHP for HTML classes

I would like to have an if/then argument for 2 classes (from the css) for a menu item. One where the menu item is blue if it is NOT the active page, and one where the menu item is red if it IS the active page. I have figured out the active page portion, now I am trying to figure out the if it is not active portion. I hope that makes sense. I have included a code snippit below.
<ul class="menu ul">
<li><a class="Blue <?php if($page =='home'){echo 'active';}?>" href="../index.php" >Home</a></li>
I have tried multiple variations, however I cannot figure it out. Thanks for your help!
Oleksandr is correct: It's better to have your links styles blue by default and overwrite it with the active class.
If you would want to give a hyperlink either one class or the other based on a simple condition, I would recommend this syntax:
<ul class="menu ul">
<li>
<a class="<?= $page == 'home' ? 'active' : 'Blue' ?>" href="../index.php" >Home</a>
</li>
</ul>
The example above uses the ternary operator and the echo shortcut syntax, and simply echoes one of two values based on the outcome of the condition.
as far as I understood you want to add different classes to link depending on $page variable.
for this i would recommend you to just use else statement
<ul class="menu ul">
<li><a class="<?php if($page =='home'){echo 'Blue';}else{echo 'Red';} ?>" href="../index.php" >Home</a></li>`
However it would be much better to check state of $page somewhere up (to not make spagetti code). And then echo only class in the a element.
<?php if($page =='home'){$menu_class='Blue';}else{$menu_class= 'Red';};?>
<ul class="menu ul">
<li><a class="<?php echo $menu_class; ?>" href="../index.php" >Home</a></li></pre>

how to load the default active class with href, while loading the page

<ul class="nav nav-pills nav-stacked project-body" style="background-color: white">
<?php foreach($get_policies as $policies) : ?>
<li role="presentation" class="<?php echo $policies['policies_id'] == $policies_id ? "active" : "" ?>"><?php echo $policies['policies_title'] ?>
</li>
<?php endforeach; ?>
</ul>
i have the above code snippet, where i can display all the policies in vertically. Now i want the first policies should be loaded to the while loading the page. make the class active by default with policies loaded.
You can use nth-child for doing this
$('.project-body li:first-child').addClass('active');
You can use jquery to check if li have active class, get href then use window location.href:
if($('.project-body li').hasClass('active'))
{
window.location.href = $(this).attr('href');
}
In php way
Assuming your URL is
<?php echo base_url().'policies/index/'.$policies['policies_id'] ?>
When you click on the <li> link, to get the polilcies_id from the URL you can use $this->uri->segment(n); // n=1 for controller, n=2 for method, etc
for example
localhost/policies/index/4 where 4 is the policies_id, this->uri->segment(3)
<li role="presentation" class="<?php echo $policies['policies_id'] == this->uri->segment(3) ? "active" : "" ?>"> YOUR LINK </li>

put class in html if php condition meet

I am using html navigation panel,
below is one of example of open tab.
<li class="nav-item active open">
Now, i want 'active open' to be applied when my url contains \testing\one.
I could get that url by using php code below, i am using codeigniter.
<?php echo $this->uri->uri_string();?>
I can compare it with if statement, but my main question is how can i put this php if condition in
<li></li>
Thanks,
You can do this way -
<li <?php echo ($this->uri->uri_string() === 'value') ? 'class="nav-item active open"' : '' ?>>
<li class="nav-item <?php if($this->uri->uri_string() === 'Value') { echo 'active open' } ?>" ></li>

Change an element's CSS class with PHP

How can I change a CSS class of an HTML element in response to a changing page using PHP?
I already got the solution exactly like in this but this solution is for Javascript.
Basically, I want to add additional class when a certain IF condition is met.
This is my code.
<li <?php if ($selected == "profile") echo 'class="active"'; ?> class="dropdown">
Profile
</li>
From my code, you can see that the <li> already got a class in it. So, when I echo another class in IF condition, it completely change to the class in IF condition.
What I need is an extra class, not different class. How can I do that?
Just move it?
<li class="dropdown<?php if ($selected == "profile") echo ' active'; ?>">
This results in class="dropdown active", which is perfectly fine in HTML - you can have multiple space-separated classes on an element. All CSS rules targeting either of the two will be applied, and you can even combine them to require both:
li.active.dropdown {
/* Underline active dropdown elements */
text-decoration:underline;
}
<li class="dropdown <?php if ($selected == "profile") echo 'active'; ?>">
Profile
</li>
try this
You can try this :
<li class="<?php if ($selected == "profile") echo 'active'; ?> OTHER CONDITION HERE ">
Profile
</li>

Categories