Solved

Correctly Nested Conditional Statements - PHP

Posted on 2012-03-14
5
242 Views
Last Modified: 2012-03-14
Hi All,

I'm not a big PHP developer, I normally use VBscript...

Anyway I'm trying to write a nested conditional statement that does the following -

If HomePage then

Declare Variables (x2 sets)

If Variable Set 1 true then

Write this HTML

ElseIf Variable Set 2 true then

Write this HTML

Else

Write this HTML
End If
End If

My PHP guy is telling me that the logic in the code below is incorrect, but I just can't see where --

// home page fixed panels
if ( $_SERVER['REQUEST_URI'] == '/' ) {
$promotion_ends = strtotime('2012-03-19');
$promotion_starts = strtotime('2012-06-01');
$show_starts = strtotime('2012-09-01');
$show_ends = strtotime('2012-10-14'); 
 
	if ( (time() >= $promotion_starts ) && ( time() <= $promotion_ends )) {
    $fixed_panels[] = <<<HTML
      <div class="promotion">
        <h2><a href="/link1.html">Text here</a></h2>
      </div>
HTML;
 }
	elseif ( (time() >= $show_starts ) && ( time() <= $show_ends )) {
    $fixed_panels[] = <<<HTML
      <div class="show_2011 panel">
        <h2><a href="/link2.html">Text here</a></h2>
      </div>
HTML;
  }
  else {
    $fixed_panels[] = <<<HTML
      <div class="benefits panel">
        <h2>Partner Benefits</h2>
        <ul>
          <li><a href="/link3.html">Text here</a></li>
          <li><a href="/link4.html">Text here</a></li>
        </ul>
      </div>
HTML;
  }
}

Open in new window


Apparently I haven't nested the conditional statements.

Appreciate some feedback please.

Thank you
0
Comment
Question by:garethtnash
  • 2
  • 2
5 Comments
 
LVL 1

Assisted Solution

by:rakjosh
rakjosh earned 250 total points
ID: 37719155
Hi,

please try below code, it will surely.

<?
if ( $_SERVER['REQUEST_URI'] == '/' ) {
$promotion_ends = strtotime('2012-03-19');
$promotion_starts = strtotime('2012-06-01');
$show_starts = strtotime('2012-09-01');
$show_ends = strtotime('2012-10-14');

      if ( (time() >= $promotion_starts ) && ( time() <= $promotion_ends )) {
    $fixed_panels = '<div class="promotion">
        <h2><a href="/link1.html">Text here</a></h2>
      </div>';

 }
      elseif ( (time() >= $show_starts ) && ( time() <= $show_ends )) {
    $fixed_panels = '<div class="show_2011 panel">
        <h2><a href="/link2.html">Text here</a></h2>
      </div>';

  }
  else {
    $fixed_panels = '<div class="benefits panel">
        <h2>Partner Benefits</h2>
        <ul>
          <li><a href="/link3.html">Text here</a></li>
          <li><a href="/link4.html">Text here</a></li>
        </ul>
      </div>';
  }
}
echo $fixed_panels;
?>
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 37719192
I think the real problem is that you are using the same HEREDOC tags in three different places. The nesting of the IF statements looks OK
0
 

Author Comment

by:garethtnash
ID: 37719209
Thanks Rakjosh,

Just to check though, the nesting of the conditional statements is correct though? my logic is sound from the nesting side?

Thanks
0
 
LVL 1

Expert Comment

by:rakjosh
ID: 37719454
Hi garethtnash,
Your nesting of the conditional statements is correct, you can use HEREDOC tags or as i suggested.
0
 

Author Closing Comment

by:garethtnash
ID: 37719479
Thank you both :)
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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

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

20 Experts available now in Live!

Get 1:1 Help Now