Solved

html conditional output?

Posted on 2011-09-12
4
209 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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses how to create an extensible mechanism for linked drop downs.
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 …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now