Solved

Correctly Nested Conditional Statements - PHP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

919 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

14 Experts available now in Live!

Get 1:1 Help Now