?
Solved

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

Posted on 2014-01-21
21
Medium Priority
?
304 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
[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
  • 11
  • 4
  • 3
  • +2
21 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1428 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1428 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
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!

 
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 111

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 111

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 572 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 572 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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1428 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1428 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 111

Accepted Solution

by:
Ray Paseur earned 1428 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 111

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 111

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 111

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 111

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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