?
Solved

html conditional output?

Posted on 2011-09-12
4
Medium Priority
?
219 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 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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