Solved

html conditional output?

Posted on 2011-09-12
4
212 Views
Last Modified: 2012-05-12
I need to place the following php within the html of a web page.
The purpose is to conditionally output data if a web page is a specific name.
I am getting parse errors for the echo statement.
How can I fix this?



   
<?php
if ( is_page('test')) {
echo '
?><div class="widget">
<h2><a href="<?php blog('url'); ?>/policy"></a></h2>         
</div>';
}
?>

Open in new window

0
Comment
Question by:pillmill
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36527167
Try this.
<?php
if ( is_page('test')) {
echo '<div class="widget"><h2><a href=".blog('url')."/policy"></a></h2> </div>';
}
?>

Open in new window

0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 167 total points
ID: 36527170
Corrected again,
<?php
if ( is_page('test')) {
echo '<div class="widget"><h2><a href="'.blog('url').'"/policy"></a></h2> </div>';
}
?>

Open in new window

0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 166 total points
ID: 36527236
<?php
if ( is_page('test')) {

?><div class="widget">
<h2><a href="<?php blog('url'); ?>/policy"></a></h2>        
</div>
<?php
}
?>
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 36528624
It's not clear what you want to put where in the string posted above, but this is how I would probably do it.  Fill in the blanks with your own variables and constants.

The HEREDOC notation is very useful, because you can put PHP variables into the HEREDOC strings and you do not have to use escapes for quotes.  It makes the code much easier to read and get correct.  Used correctly, it lets you create the HTML string for the entire page in a single statement.  
http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

The choice of a string or a NULL might be made in the logic section of the PHP code.  I would try to separate the PHP from the HTML, since professional programmers would not mix the logic and presentation layers.

Good luck with your project, ~Ray
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);

// DEFINE A STRING WITH THE WIDGET DIV IN HEREDOC NOTATION:
$widget = <<<WIDGET
<div class="widget">
<h2>
<a href="--- PUT THE URL LINK HERE ---">AND SOMETHING FOR THE BROWSER TO DISPLAY HERE</a>
</h2>
</div>
WIDGET;

// IF THERE IS NOT A TEST PAGE, NULLIFY THE WIDGET STRING
if (!is_page('test')) $widget = NULL;

// ADD THE WIDGET TO THE BROWSER OUTPUT STREAM
echo $widget;

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

829 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