简体   繁体   中英

If a div tag that includes php is empty, hide the div or display certain text

There seem to be a ton of answered questions about hiding empty divs, but after I have tried several with no luck of it working I ask my own question. I have this section on my page to display various things in this case it's titles.

This is how it looks when there is a title to display:

在此处输入图片说明

This is how it looks when there is no title to display:

在此处输入图片说明

This is how what I want it to look like when there are no title to display:

在此处输入图片说明

Below is the code part that deals with this area the question concerns, the titles are being collected after how many points a member have and are collected from functions.php, so I want something that can read that there are no title to collect from functions.php and then instead of the ugly little square it shows No title if anyone has the answer to this I will be very happy, I have been on it for hours but with no success, I should say to that I am very much a novice when it comes to php and not even that when it comes to javascript and jquery.

 <div id='dumb'>
    <div id="assocpoint2">TITEL/S:</div><div id="assocpointprint2">
    <? if($gender === 'Mare'){echo find_asda_title($horse);}elseif($gender === 'Stallion'){echo find_asda_title($horse);}elseif($gender === 'Gelding'){echo find_asda_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_asea_title($horse);}elseif($gender === 'Stallion'){echo find_asea_title($horse);}elseif($gender === 'Gelding'){echo find_asea_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_asja_title($horse);}elseif($gender === 'Stallion'){echo find_asja_title($horse);}elseif($gender === 'Gelding'){echo find_asja_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_sisf_title($horse);}elseif($gender === 'Stallion'){echo find_sisf_title($horse);}elseif($gender === 'Gelding'){echo find_sisf_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_aspa_title($horse);}elseif($gender === 'Stallion'){echo find_aspa_title($horse);}elseif($gender === 'Gelding'){echo find_aspa_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_aswa_title($horse);}elseif($gender === 'Stallion'){echo find_aswa_title($horse);}elseif($gender === 'Gelding'){echo find_aswa_title($horse);} ?>
    </div></div>

Most of that code seems to be utterly pointless/repetitive. Why not something more like:

$output = find_asda_title($horse) . find_asea_title($horse) . etc...
if (in_array($gender, array('Mare', 'Gelding', 'Stallion')) {
   echo $output;
} else {
   echo stuff to hide the div
}

You can handle the content of empty divs through css if you want:

Just target the empty div with the :empty selector, and add a pseudo content to it:

#assocpointprint2:empty:after {
   content: "No Title";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM