Newbie to PHP - trouble with if statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dragon76
Forum Newbie
Posts: 2
Joined: Wed Sep 15, 2010 11:32 am

Newbie to PHP - trouble with if statements

Post by dragon76 »

Please excuse the seriously amateur coding but
I am trying to call different HTML code depending on what page the user is on but it's not working.
Any help would be greatly appreciated.

Here is my code:

Code: Select all

<?php
if ($aboutSide == "AboutHome") {

include
<div class="twocolumns">
	<div id="sidebar">
		<ul class="accordion">
			<li class="first"> <a class="opener selected">About</a> </li>
				<li> <a href=/mission.php">Mission</a> </li>
				<li> <a href="/team.php">Management Team</a> </
			</ul>
</div>
;
}

} elseif ($aboutSide == "AboutCouncil") {
include
 <div class="twocolumns">
    <div id="sidebar">
        <ul class="accordion">
            <li class="first"> <a href="/about.php">About</a> </li>
                <li> <a class="opener selected">Mission</a> </li>
                <li> <a href="/team.php">Management Team</a> </
            </ul>
</div>
	;  
 }

 } else {
  include
<div class="twocolumns">
    <div id="sidebar">
        <ul class="accordion">
            <li class="first"> <a href="/about.php">About</a> </li>
                <li> <a href="/mission.php">Mission</a> </li>
                <li> <a class="opener selected"">Management Team</a> </
            </ul>
</div>
	  ;
 }
?>
Last edited by califdon on Wed Sep 15, 2010 12:30 pm, edited 1 time in total.
Reason: Added syntax tags to make php code readable
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newbie to PHP - trouble with if statements

Post by califdon »

For future reference, please use the "PHP Code" button to enclose your code in syntax tags so that we can read your code without going blind. I have done this for you in your post.

You are using the word "include" inappropriately, probably under a mistaken understanding of what that command does. Look that up in the manual. There are several options you may use in a situation like your example. One is to "echo" your HTML as quoted strings, like:

Code: Select all

if ($aboutSide == "AboutHome") {
    echo "<div class='twocolumns'>";
    echo "<div id='sidebar'>";
    ...
Alternatively, you can break out of PHP and write the HTML until the next place you need to invoke PHP. In this case, only the HTML that is within a PHP block that is executed will be sent to the browser:

Code: Select all

<?php
if ($aboutSide == "AboutHome") {
?>
<div class="twocolumns">
        <div id="sidebar">
                <ul class="accordion">
                        <li class="first"> <a class="opener selected">About</a> </li>
                                <li> <a href=/mission.php">Mission</a> </li>
                                <li> <a href="/team.php">Management Team</a> </
                        </ul>
</div>
<?php
} elseif($aboutSide == "AboutCouncil") { 
...
or you can use PHP's "Heredoc" syntax, like:

Code: Select all

if ($aboutSide == "AboutHome") {
echo <<<EOD
<div class="twocolumns">
        <div id="sidebar">
                <ul class="accordion">
                        <li class="first"> <a class="opener selected">About</a> </li>
                                <li> <a href=/mission.php">Mission</a> </li>
                                <li> <a href="/team.php">Management Team</a> </
                        </ul>
</div>
EOD;
} elseif($aboutSide == "AboutCouncil") { 
...
In any case, "include" is not used for this purpose. I also assume that you have defined the variable $aboutSide previously, although you do not show it here.
dragon76
Forum Newbie
Posts: 2
Joined: Wed Sep 15, 2010 11:32 am

Re: Newbie to PHP - trouble with if statements

Post by dragon76 »

Hi there,
Thanks for the tips. I have changed my strategy a little bit to keep the code simple and clean
I don't think i have everything in place for the code to work properly though...

I have template php pages (home, about us, contact us...)that have include tags for common elements (header, footer, top nav...)
This is easy enough and all works fine. My issue is with the side nav, depending on what page the user is on the side nav changes slightly.

For example on the about.php page I have the following:

Code: Select all

<!---SIDEBAR STARTS--->
<?php include("/shared/sideNavAbout.php"); ?>
<!---SIDEBAR ENDS--->    
On the sideNavAbout.php I have the following:

Code: Select all

$sideNavAbout=home("index.php");
$sideNavAbout=mission("mission.php);

<div id="sidebar">
<?php
	if ($sideNavAbout=="home"){
	?>
	<li class="first"> <a class="opener selected">About</a> </li>
	<?php
   }else
	?>
	<li class="first"> <a href="index.php">About</a> </li>

I'm not sure how to use or where to put the actual variables in order for them to be associated to a specific page.
Thanks in advance.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newbie to PHP - trouble with if statements

Post by califdon »

You now have the conditional syntax correct, but you have a problem with your variables. Unless you have defined functions like home() and mission() elsewhere, the syntax at the top of sideNavAbout.php doesn't make any sense, in fact it will cause your php script to not run.

There are several ways to handle this. One way would be to pass the desired value to sideNavAbout.php as a "query string" parameter when you include that file. So your about.php page would contain something like:

Code: Select all

<!---SIDEBAR STARTS--->
<?php include("/shared/sideNavAbout.php?opt=mission"); ?>
<!---SIDEBAR ENDS--->
and your sideNavAbout.php would obtain the query string as a $_GET array pair, like this:

Code: Select all

$opt=$_GET['opt'];
<div id="sidebar">
<?php
        if ($opt=="home"){
        ...
etc.

There are additional methods, and there are some precautions you should take when accepting query strings, to avoid leaving a security hole to be exploited, but that's the approach you probably need to take.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newbie to PHP - trouble with if statements

Post by califdon »

Thank for this help...i really appreciate it.
I'm still ahving some trouble though and I can't figure it out.

This is what the page looks like:
http://www.digitallocksmiths.ca/beta/about/index.php

After trying different options, this is the latest code used:
$opt=$_GET['opt'];
<div id="sidebar">
<ul class="accordion">
<?php
if ($opt=="home"){
echo '<li class="first"> <a class="opener selected">About Digital Locksmiths</a> </li>';
}else
echo '<li class="first"> <a href="http://www.digitallocksmiths.ca/beta/ab ... php">About Digital Locksmiths</a> </li>';
?>
</ul>
</div>[/syntax]
Any help would be greatly appreciated.
I'm afraid I slipped up when I showed you that code, of course the $opt=$_GET['opt']; must go within <?php and ?> tags, which my example code didn't. So move that line down 3 lines, to be inside the <?php ... ?> tags. Then, you are missing an opening curly bracket after the else. As I understand your intent, you want to have the List Item (<li>) include a link except for the item that would link to the same page. Your syntax is not correct, but why do you want to do that? It won't hurt anything to just leave the link there, if someone clicks it, it will just reload the page. Now, if you want the current page link to appear in a different color or something, you can do that, but you would want to use a different CSS class to do that.

I'm afraid that's all the time I can spend on this right now, but give it another try and I'll check back later.
Post Reply