?
Solved

html conditional output?

Posted on 2011-09-12
4
Medium Priority
?
228 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 668 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 664 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
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 how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 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