Solved

Correctly Nested Conditional Statements - PHP

Posted on 2012-03-14
5
246 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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