Solved

What is the best way to write this short and simple PHP/HTML code?

Posted on 2014-01-21
21
296 Views
Last Modified: 2014-01-21
If the if statement is true, I want the HTML below displayed, but there's gotta be a better than echoing out each line of HTML, right?

<?php

if (!isset($_GET["id"])) {

// then display this HTML below
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>
}

?>

Open in new window



As always, thanks!
0
Comment
Question by:LB1234
  • 11
  • 4
  • 3
  • +2
21 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 357 total points
ID: 39796717
HEREDOC notation is useful for something like this.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 39796727
This will also do the stuff,

<?php

if (!isset($_GET["id"])) {
?>

// then display this HTML below
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>
<?php
}

?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 357 total points
ID: 39796733
See:
http://www.laprbass.com/RAY_temp_lb1234.php?id=X
http://www.laprbass.com/RAY_temp_lb1234.php

If you use the HEREDOC, you can also use it to perform variable substitution in the string, making it perfect for templates!

<?php // RAY_temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343859.html

// THE HTML CODE FRAGMENT
$htm = <<<EOD
<div id="menu">
 <ul>
  <li><a href="#">Pending Expenses</a></li>
  <li><a href="#">Open Report</a></li>
 </ul>
</div>
EOD;

if (!isset($_GET["id"]))
{
    echo $htm;
}

Open in new window

HTH, ~Ray
0
 
LVL 1

Author Comment

by:LB1234
ID: 39796734
Logu, that doesn't make sense why that would work!  Can you please explain the logic?
0
 
LVL 1

Author Comment

by:LB1234
ID: 39796736
Ray, ok so something like this?

<?php

if (!isset($_GET["id"])) {

// then display this HTML below

echo <<< EOT
 <div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>

EOT;

}

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39796737
The learning resources in this article will help you get a handle on this, and many other PHP-related questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39796740
something like this?
Yes, but you'll find that the code is easier to maintain if you create your variables separately, away from the presentation (echo).  That's why I assigned a variable to the HEREDOC string then echo the variable, instead of echo the HEREDOC
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 143 total points
ID: 39797133
You don't need the HTML bits inside of the PHP block. Just have you starting and closing IF in the PHP and leave the rest in HTML:

<?php if (!isset($_GET["id"])): ?>
<div id="menu">
        <ul>
            <li><a href="#">Pending Expenses</a></li>
            <li><a href="#">Open Report</a></li>
        </ul>
</div>
<?php endif; ?>

Open in new window

0
 
LVL 1

Author Comment

by:LB1234
ID: 39797144
Chris, but isn't anything not in PHP blocks automatically displayed?  That's what's throwing me off here.
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 143 total points
ID: 39797160
No, because they're still contained inside an IF statement. You can do the same with any control structure (for, while, if etc.)

<?php while (some condition): ?>

This is back to standard HTML, but it's still contained inside the WHILE statement

<?php endwhile; ?>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 357 total points
ID: 39797163
... isn't anything not in PHP blocks automatically displayed?
Well, the answer is "maybe."  PHP has conditional control structures, like if() statements.  The status of PHP control structures takes overarching control of the script.  Install this and run it to see what happens.

<?php // RAY_temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343859.html

$x = 3;
if ($x == 2)
{
?>
This HTML will not be shown.
<?php
}

if ($x == 3)
{
?>
This HTML will be shown because $x == 3.
<?php
}

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 357 total points
ID: 39797198
Here's a slightly different take on HEREDOC.  I hope you will see the usefulness of this.  In a moment I'll post the version that jumps back and forth from PHP to HTML.

<?php // RAY_temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343859.html
// SHOW WHY HEREDOC NOTATION IS SO USEFUL

// VARIABLES CREATED IN OUR SCRIPT ...
$var = date('F, Y');
$nom = 'for Fred';

// ... GET SUBSTITUTED INTO THE HTML CODE FRAGMENT
$htm = <<<EOD
<div id="menu">
 <ul>
  <li><a href="#">Pending Expenses as of $var</a></li>
  <li><a href="#">Open Report $nom;</a></li>
 </ul>
</div>
EOD;

if (!isset($_GET["id"]))
{
    echo $htm;
}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 357 total points
ID: 39797208
And here is the version that intermixes data and presentation.  This is one of those "Just don't do that" things.  You'll find out why the first time you get a 500 line script like this and you have to read it, understand it and maintain it.

<?php // RAY_temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28343859.html
// SHOW WHY HEREDOC NOTATION IS SO USEFUL

// VARIABLES CREATED IN OUR SCRIPT ...
$var = date('F, Y');
$nom = 'for Fred';

if (!isset($_GET["id"]))
{
    // DROP OUT OF PHP
    ?>
<div id="menu">
 <ul>
  <li><a href="#">Pending Expenses as of<?php /* BACK INTO PHP */ if ($var) echo $var; /* DROP OUT AGAIN */ ?></a></li>
  <li><a href="#">Open Report <?php /* BACK INTO PHP */ if ($nom) echo $nom; /* DROP OUT AGAIN */ ?></a></li>
 </ul>
</div>
    <?php /* END THE CONTROL STRUCTURE */
}

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39797714
On the other hand, when you fix and maintain other people's code, you can get pretty good at reading code like Ray's last example where everything is all inter-mixed.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798051
Dave :-)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39798193
I make most of my money doing that, Ray.  Very educational and profitable.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798247
Yeah, I had a similar experience last year.  When I see a code smell, I go hourly immediately!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39798296
hourly?  Is there another way???

Actually, I will do new jobs or small projects on a fixed quote when I know how it's going to go.  I'm very good at the things I've already done 100 times...
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798432
Yeah me too.  It takes about 100 times to get proficiency at my age ;-)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39798544
But Ray!!  You're Still a Young Man....
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39798561
Erm :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now