Solved

Correctly Nested Conditional Statements - PHP

Posted on 2012-03-14
5
245 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

830 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