Hide HTML on mobile device - php

I've figured out how to show an element on mobile
<?php if(detectMobile()) { ?>
htmlcode
<?php } ?>
However i'm trying the opposite and want to HIDE an element on mobile.
I know this is possible with css but in this case i don't want to use the display:none option because its using the same css like some other elements that should not be hidden on mobile. Also creating a unique css is not what i want in this case.

Simply change your if yes to an if no by adding a ! (not operator)
Your code becomes:
<?php
if( !detectMobile() )
{
?>
htmlcode
<?
}
?>
or alternatively, use an if else construction. Your code becomes:
<?php
if( detectMobile() )
{
// future code in case we must do something for mobile.
?>
<?
}
else
{
// if not mobile, then this code is being used.
?>
htmlcode
<?
}
?>

Related

Trouble Hiding PHP script if a DIV is present on the page

I am trying to hide the following:
<h1>aaa <?php echo HTML_SOBI::getMyCategories($mySobi, true);?> aaa</h1>
If the current div is present on the page:
<div id="bbb">
I tried to use this (Didn't work):
<div id="bbb" <?php if (condition) { echo 'style="display:none;"; } else {
echo <h1>Find More <?php echo HTML_SOBI::getMyCategories($mySobi, true);?> </h1>; } ?></div>
I thought that this would do what was expected but crashed the site in that area.
Sorry if this is newbie mistake or bad coding I am just starting out and couldn't find the right fit of code for this.
There will be tens of different eays to code this up. Critically, you need to ensure that:
you are closing any opening quotes associated with your inline style declarations
you are closing any opened html tags (<div)
These factors are essential to generate valid markup that will behave as you intend. If the following doesn't work as expected, you will need to clarify/edit your question.
<div id="bbb"<?php echo $condition ? ' style="display:none;"' : ''; ?>>
<?php
if (!$condition) {
echo '<h1>Find More ' , HTML_SOBI::getMyCategories($mySobi, true) , '</h1>';
}
?>
...
Note, I reckon the second (negated) condition is probably not needed, but I'll leave it to be demonstrative.
Another consideration is to use the condition block to assign a class to all elements that you wish to hide. If the condition is true, add the class like hiddenTag to the tags, then in your css file declare .hiddenTag { display: none; }.

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

Wordpress Footer Text PHP

Currently in the PHP file it has:
<?php if($myfooter_text){?>
<?php echo of_get_option('footer_text'); ?>
<?php } else { ?>
something else here
<?php } ?>
What I would like is for it to insert the footer text it finds using $myfooter_text but also add a link to the end of whatever has been pre filled in the footer text. I tried to use concatenation with the following:
<?php echo of_get_option('footer_text') . 'mylink; ?>
However this still just shows the predefined footer text and not the additional content. Is there a way to do this? I'm aware it could be added in the footer area of the dashboard but this isnt what i want to do.
Try it like this:
<?php echo of_get_option('footer_text'); ?> mylink
You can concatenate them as well like this or do it your way and be sure you close all the quotes:
<?php echo of_get_option('footer_text') . 'mylink'; ?>

If body has class then do this this etc

New to Magento (wordpress developer) and I'm struggling with this concept in Magento and not sure how to accomplish this with their api and set a varialbe of if body class = x
In short if the body has a class that is "x" then this happens (I'm trying target specific class's, so if a sub-page had a class of "other" for example, then it would run an if or elseif for that page):
<?php if ($this->getLayout()->createBlock("page/html")->getBodyClass() == 'home'): ?>
//add this content
<?php elseif($this->getLayout()->createBlock("page/html")->getBodyClass() == 'options'):?>
//add different divs content etc
<?php else: ?>
//stuff
<?php endif; ?>
you can get the css class through
$this->getBodyClass()
used to work with block type
Mage_Page_Block_Html
otherwise
$this->getLayout()->createBlock("page/html")->getBodyClass();
<?php
$body_classes = $this->getLayout()->createBlock("page/html")->getBodyClass();
if($body_classes=='cms-index-index'):
//your code
elseif($body_classes=='cms-page-view'):
//your code
else:
//your code
endif;
?>

Stacking PHP code?

I've done a project that now needs to be changed in order to display one div if a variable is in an array and a different div if it isn't in the array.
Normally I'd just do
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "quarter code in here";}
else
{echo "nonquarter code in here";}
?>
and be on my merry way, however the code I've got already contains a load of html and php code already, which doesn't like encapsulated within another PHP block (as far as I'm aware?)
e.g.
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "Quarter HTML CODE
<?php quarter phpcode ?>";}
else
{echo "Non-Quarter HTML CODE
<?php non-quarter phpcode ?>";}
?>
So my question is, what is the best way to tackle this? Is it simply to do a javascript hide div A when the variable is met and hide divB when the variable isn't met, or is there a better solution?
Thanks
<?php
if (in_array($month,$quartermonths))
{ ?>
Quarter HTML CODE
<?php quarter phpcode ?>
<?php } ?>
split your html code from php code like this.
It sounds like you just want to concatenate the value of quarter phpcode. Let's say it's a single function, quarter_phpcode(). You can just do this:
{ echo "Quarter HTML CODE" . quarter_phpcode(); }

Categories