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

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; }.

Related

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

How to add a id tag in php output?

I have to add an html id tag to give some css properties inside this php code and I don't know how to do that?
<?php
//Start your session
session_start();
//Read your session (if it is set)
if (isset($_SESSION['user_login'])){
echo $_SESSION['user_login'];
}
?>
I have to add css property for that echo.
may be you want something like this -
if (isset($_SESSION['user_login']))
{
echo '<div id="YOUR-ID-HERE">'.$_SESSION['user_login'].'</div>';
}
This is simple HTML 101,
<tagname id="idValue">
Have you tried looking though w3schools
You need to enclose (wrap) your PHP output in a div.
And then give id to that div.
<div id="YOUR_CSS_ID_HERE">
<?php echo $_SESSION['user_login'];?>
</div>
Also, no need for brackets ( and ) for echo as it is a language construct, not a function.

Trying to understand what's going on in this PHP code?

I program in other languages but my job involves reading PHP and I'm trying to understand part of this view page (we use MVC).
<?
foreach ($slides as $i => $slide) {
?>
<li class="yui3-carousel-element<?=$i > 0 ? ' hidden-node' : ''?>">
<?
Why is the loop surrounded by <? and ?> ? I thought those go on either end of the entire PHP script, but instead I'm seeing them scattered throughout the whole thing.
That ternary expression seems to just be floating....it's not being echo'ed or print'ed or concatenated to anything....not to mention it weirdly seems to be comparing the number 0 to something I can't make out....it's not a string...it's an li element with no closing tag?? In php?? I'm very confused.
<? ?> are called short tags in PHP. They indicate the start and end of PHP code. The ternary is also wrapped with the short tags. That ternary is actually appending something to the class depending on whether or not the current index of $slide is greater than 0. The li is HTML and it should be closed.
<? is used as an opening tag for php-code and ?> as a closing tag. PHP can be mixed with other languages like HTML as seen in your example.
Actually it is being echo'ed, that's what the =right after the <? does. It is nothing else than a short command for echo. The other thing with the ? and : is another short form. Written out the whole thing equals if($i > 0) echo ' hidden-node'; else echo '';.
So the code adds <li>-Elements for every slide and for every slide except the first one it adds the class 'hidden-node', which most likely hides all other elements except the first one when the code gets loaded.
The <? and ?> tags are short tags saying that PHP code is inside of them. When they are closed, it goes back to simply outputting HTML.
The <?=that you see on the line is simply a shorthand for <?php echo
Personally I would have probably left the tag open at the start, then do echo on the line in the loop. What is there is the equivalent to the following:
<?php
foreach ($slides as $i => $slide) {
$hiddenElement = $i > 0 ? ' hidden-node' : '';
echo '<li class="yui3-carousel-element'.$hiddenElement.'">';
The loop is surrounded by those because it's exiting PHP code and entering HTML code. For example, you can do: <?php /*code here blah blah*/ if (...) { ?> <div>ha</div> <?php } /*more code*/ ?>. In PHP, if you don't wrap stuff with <?php ?>, then it's executed as HTML code.
That statement with <?= is a shortcut to echo'ing. It literally says <?php echo (($i > 0) ? ' hidden-node' : '' ?>.
the <? and ?> is shorthand for the php tags.
after that is clear you can see that the
<li class="yui3-carousel-element<?=$i > 0 ? ' hidden-node' : ''?>">
part is checking if $i is greater than 0 and displaying the hidden-node when it is

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(); }

CSS Class on end MySQL row

I am having trouble creating a solution that will target the end row of a MySQL query. Currently I have a foreach function that works through the query and displays each one as a div with the information inside:
<?php $residents = Resident::find_all();
foreach($residents as $resident): ?>
<div class="submenu">
<p class="menuitem submenuheader"><?php echo $resident->name; ?></p>
<img src="images/<?php echo $resident->image_path(); ?>" width="250" class="image" />
<p><?php echo $resident->info; ?></p>
</div>
.submenu currently has a bottom border. I need to remove this on the last row returned. I have looked at DESC LIMIT 1, however this requires another MySQL query and could make things very messy...
Addd this to your CSS:
.submenu:last-child { border-bottom: 0; }
Note: this is not supported by IE < 9.
You could switch to putting the border on the top of the element, and use the :first-child pseudo selector in CSS to remove it.
http://reference.sitepoint.com/css/pseudoclass-firstchild
The :last-child selector would be nice, but it's not supported in IE before version 9, so it's not a good idea to use it if you want compatibility.
If you separate your HTML and PHP a little this is easily achieved:
<?php
function echoBlock($resident,$pClass="menuitem submenuheader") {
echo "<div class=\"submenu\">\n<p class=\"$pClass\">\n";
echo $resident->name;
echo "</p>\n<img src=\"images/";
echo $resident->image_path();
echo "\" width=\"250\" class=\"image\" />\n<p>";
echo $resident->info;
echo "</p>\n</div>\n\n";
}
$residents = Resident::find_all();
$last=count($residents)-1;//2 element array last pos is 1
for ($i=0;$i<$last;$i++) {
echoBlock($residents[$i]);
}
echoBlock($residents[$last],"menuitem");
?>
echoBlock (which could easily be a method on a class) requires the calling code to know about the classes it uses, which isn't really separating intent but it does prevent the need for an if branch on every loop. That being said it would be less efficient but perhaps more usable to set it up as:
function echoBlock($resident,$isLast=false) {
$pClass="menuitem".($isLast?"":" submenuheader");
//...
Which then doesn't need the caller to know anything about what echoBlock does.
You could try and pop the array using array_pop(), to get the last value out of the array and then inputing it using the special class after the foreach loop.
What about
Instead of echo'ing each line one by one, create one big string in PHP, search for the last entry of "submenu" and change the class.

Categories