[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP:  How to Prevent Publishing an Empty Div

Posted on 2009-04-27
6
Medium Priority
?
313 Views
Last Modified: 2013-11-19
My index.php file outputs an empty div on certain pages where a PHP 'include' statement returns no content.  This intereres with the page layout on those pages.

What is the PHP code to prevent div id="registrationwrap" in the code below from being outputted if the content of div class="registration" (which is populated by <jdoc:include type="component" style="xhtml" /> is empty ?

------- portion of code in my index.php file ---------

    <div id="registrationwrap" align="center" >
            <div class="registration">
                  <jdoc:include type="component" style="xhtml" />
          </div>
    </div>

----------------------------------------------------------------

Thx
0
Comment
Question by:qeng
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:basic612
ID: 24247114
try this:

<?php 
 
if (strlen(trim(strip_tags($this->_buffer['component'][''])))) {
 
?>
 
    <div id="registrationwrap" align="center" >
            <div class="registration">
                  <jdoc:include type="component" style="xhtml" />
          </div>
    </div>
 
<?php 
}
?>

Open in new window

0
 

Author Comment

by:qeng
ID: 24249519
basic612,
Thanks for replying.  The div is still being published I think because the core files called in the jdoc:include statement introduce a <br />   and    ">&nbsp;    (see exceprt from published html below).
--------- exceprt from published html --------------
<div id="registrationwrap" align="center" >
<div class="registration">
<table class="contentpaneopen"> <tr>
<td valign="top">
<div style="text-align: center"><br /> </div>
 </td> </tr> </table> <span class="article_separator">&nbsp;</span>
</div>
</div>  
--------- end exceprt from published html --------------
Wondering if your suggestion can be adapted to strip those out as well and if that would solve it?
Or if I can use one of the option= or task= commands in the page address to conditionally publish div id="registrationwarp"?  For example:
If I knew any PHP my temptation would be to try to wrap the div id="registrarionwrap" in a statement which only published this div if the page (the page displayed in the browser's address bar) was pointing to the Registration page, something conceptually like:
---- conceptual example -----
if: (http address = http://mysite.com/index.php?option=com_user&task=register) = true
then:

<div id="registrationwrap" align="center" >
            <div class="registration">
                  <jdoc:include type="component" style="xhtml" />
          </div>
    </div>

else:
'do nothing'
 ----- end conceptual example -----
Thx for the help.
0
 
LVL 6

Expert Comment

by:basic612
ID: 24249614
I use the $Itemid global variable to determine what page I am on if I am going to change the template depending on the menu. You can see the Itemid in the CMS administrator pages in the menu config:

// check for home page
global $Itemid;
$isHomePage = ($Itemid == 1 ? true : false);

// only show on home page
if ($isHomePage) {
echo "show home page content";
}

alternatively you could try the code modification below:




<?php 
 
if (trim(strip_tags($this->_buffer['component']['']))) == '&nbsp;' {
 
?>
 
    <div id="registrationwrap" align="center" >
            <div class="registration">
                  <jdoc:include type="component" style="xhtml" />
          </div>
    </div>
 
<?php 
}
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Accepted Solution

by:
basic612 earned 2000 total points
ID: 24249651
or, to flesh out your concept, the following might work (untested):

<?php 
if ($option == 'com_user' && JRequest::getCmd('task') == 'register') {
?>
 
<div id="registrationwrap" align="center" >
            <div class="registration">
                 <jdoc:include type="component" style="xhtml" />
         </div>
   </div>
 
<?php 
} 
?>

Open in new window

0
 

Author Comment

by:qeng
ID: 24249901
basic612,
Thx a bunch.  I used the last example.  Works like a charm.
I'll also make use of your suggestion re:  changing the template based on the menu.  It's one of the things I've been headed towards.
Thx again.
0
 

Author Closing Comment

by:qeng
ID: 31575213
Many thanks basic612, bang on.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question