Solved

html conditional output?

Posted on 2011-09-12
4
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

717 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