Jump to content

Archived

This topic is now archived and is closed to further replies.

soccerstar22003

problem with a simple if statement

Recommended Posts

[code]
<?

if (!isset($_GET['page'])) {
  include ("home.html");
} else {                         <-------- Line 36
  if ($_GET['page'] != (home|contracts|reminders|accessories|contactus) {
    include ("home.html");
  } else {
    $page = $_GET['page'];
    include ("/content/".$page.".html");
  }
}

?>
[/code]

to me that looks fine, but when i test it, it comes up and says "Parse error: syntax error, unexpected '}' in C:\website\index.php on line 36" [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] i marked line 36 in the code

any help appreciated

Share this post


Link to post
Share on other sites
You forgot to close the expression bracket in the second if statment.
The code should be this:
[code]
<?

if (!isset($_GET['page'])) {
  include ("home.html");
} else {                         <-------- Line 36
  if ($_GET['page'] != (home|contracts|reminders|accessories|contactus) ) {
    include ("home.html");
  } else {
    $page = $_GET['page'];
    include ("/content/".$page.".html");
  }
}

?>
[/code]

Share this post


Link to post
Share on other sites
try this ok
[code]
<?

if (!isset($_GET['page'])) {


  include ("home.html");

} elseif ($_GET['page'] != (home|contracts|reminders|accessories|contactus) {


    include ("home.html");

} else {

    $page = $_GET['page'];

    include ("/content/".$page.".html");

}


?>
[/code]

Share this post


Link to post
Share on other sites
thanks for the help, the code works now. i also have one other question. right now i am passing variable in the URL using /index.php?page=home but i have seen people use /index.php?home if i use the second script, how do i retrieve the variable home.

Share this post


Link to post
Share on other sites
I don't know if I have this right, but I will try to explain as I understand it.

If you use /index.php?home you setting the $_GET['home'] variable to NULL. If you use /index.php?page=home your are setting the $_GET['page'] variable to 'home'. I would say the /index.php?page=home is better, but If you want to use the /index.php?home method, try this:
[code]

if (
    isset($_GET['home']) |
    isset($_GET['contracts']) |  
    ....
    isset($_GET['contactus'])
   )
{
include ("home.html");
}
else
{
// the rest of your code....
}
[/code]

Share this post


Link to post
Share on other sites
i redesigned the code to use the index.php?home format and this is what i came up with

[code]<?

if (!isset($_GET)) {
  include ("home.html");
} elseif (isset($_GET['home'])) {            <--------Line 37
  include ("home.html");
} elseif (isset($_GET['contracts'])) {
  include ("contracts.html");
} elseif (isset($_GET['reminders'])) {
  include ("reminders.html");
} elseif (isset($_GET['accessories'])) {
  include ("accessories.html");
} elseif (isset($_GET['contactus'])) {
  include ("contactus.html");
} else {
  include ("home.html");
}

?>
[/code]

it looks good to me but now i got a new issue, the error is almost the same: "Parse error: syntax error, unexpected '{' in C:\Inetpub\PMPOOLS\index.php on line 37"

its as if it is not accepting the elseif statement isn't there so it is not expecting the condition that follows it. any reasons why it would do that?

Share this post


Link to post
Share on other sites
[code]
<?

if (! isset($_GET)) {

  include ("home.html");

} elseif (isset($_GET['home'])) {    
        
  include ("home.html");

} else { $_GET['contracts'];

  include ("contracts.html");

} elseif (isset($_GET['reminders'])) {

  include ("reminders.html");

} else { $_GET['accessories'];

  include ("accessories.html");

} elseif (isset($_GET['contactus'])) {

  include ("contactus.html");

} else {

  include ("home.html");
}

?>
[/code]

Share this post


Link to post
Share on other sites
Try this way or your have to alter it to the first old way ok.

[code]
<?

if (! isset($_GET)) {

  include ("home.html");

} elseif (! isset($_GET['home'])) {    
        
  include ("home.html");

} else { $_GET['contracts'];

  include ("contracts.html");

} elseif (! isset($_GET['reminders'])) {

  include ("reminders.html");

} else { $_GET['accessories'];

  include ("accessories.html");

} elseif (! isset ($_GET['contactus'])) {

  include ("contactus.html");

} else {

  include ("home.html");
}

?>
[/code]

Share this post


Link to post
Share on other sites
What the?

You cannot mix elseif, else elseif else. Its...
[code]
if () {
  } elseif () {
  } elseif () {
  } else {
}
[/code]

Share this post


Link to post
Share on other sites
Whats a mess! redarrow I suggest your go back to the manual and readup on if/elseif/else statements!

As this:
} else { $_GET['accessories'];
does nothing!

Also soccerstar22003 you might be better of with a switch statment instead:
[code]<?php

// check that p is set in the URL
if (isset($_GET['p']))
{
    // now we include the correct page
    switch($_GET['p'])
    {
        case 'home':
            include 'home.html';
        break;

        case 'contacts':
            include 'contacts.html';
        break;

        case 'reminders':
            include 'reminders.html';
        break;

        case 'accessories':
            include 'accessories.html';
        break;

        case 'contactus':
            include 'contactus.html';
        break;

        default:
            include 'home.html';
        break;
    }
}
?>
<a href="?p=home">Home</a> | <a href="?p=contacts">Contacts</a> | <a href="?p=reminders">Reminders</a> | <br />
<a href="?p=accessories">Accessories</a> | <a href="?p=contactus">Contact Us</a> | <a href="?p=contactus">Contact Us</a>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.