• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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

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
LB1234
Asked:
LB1234
  • 11
  • 4
  • 3
  • +2
7 Solutions
 
Ray PaseurCommented:
HEREDOC notation is useful for something like this.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
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
 
Ray PaseurCommented:
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
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!

 
LB1234Author Commented:
Logu, that doesn't make sense why that would work!  Can you please explain the logic?
0
 
LB1234Author Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
Chris StanyonCommented:
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
 
LB1234Author Commented:
Chris, but isn't anything not in PHP blocks automatically displayed?  That's what's throwing me off here.
0
 
Chris StanyonCommented:
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
 
Ray PaseurCommented:
... 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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
Dave :-)
0
 
Dave BaldwinFixer of ProblemsCommented:
I make most of my money doing that, Ray.  Very educational and profitable.
0
 
Ray PaseurCommented:
Yeah, I had a similar experience last year.  When I see a code smell, I go hourly immediately!
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
Yeah me too.  It takes about 100 times to get proficiency at my age ;-)
0
 
Dave BaldwinFixer of ProblemsCommented:
But Ray!!  You're Still a Young Man....
0
 
Ray PaseurCommented:
Erm :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now