Solved

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

Posted on 2014-01-21
21
293 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
Comment Utility
HEREDOC notation is useful for something like this.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
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
Comment Utility
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
Comment Utility
Logu, that doesn't make sense why that would work!  Can you please explain the logic?
0
 
LVL 1

Author Comment

by:LB1234
Comment Utility
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
Comment Utility
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
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 143 total points
Comment Utility
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
Comment Utility
Chris, but isn't anything not in PHP blocks automatically displayed?  That's what's throwing me off here.
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 143 total points
Comment Utility
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
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

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 357 total points
Comment Utility
... 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
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Dave :-)
0
 
LVL 82

Expert Comment

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

Expert Comment

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

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Yeah me too.  It takes about 100 times to get proficiency at my age ;-)
0
 
LVL 82

Expert Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
Erm :-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

772 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

11 Experts available now in Live!

Get 1:1 Help Now