Jump to content


Problem with using if statement


7 replies to this topic

#1 bfva

    Young Padawan

  • Members
  • Pip
  • 42 posts

Posted 28 August 2007 - 12:00 PM

I've been writing this code and just can't seem to get it working.

What i'm trying to do is when the url contains a certain, then the corresponding link is highlighted.

EG

If the link is www.mysite.com/h/home/index.php

The link Home is highlighted.

Here is my code

<?
$uri = $_SERVER['REQUEST_URI'];
if ($uri == 'home'){
echo"<ul>";
echo "<li id=\"current\"><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "</ul>";
}
elseif ($uri == 'home2'){
echo"<ul>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li id=\"current\"><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "</ul>";
}
?>


Thanks For Your Help

Edited by bfva, 28 August 2007 - 01:40 PM.


#2 Matt L

    Young Padawan

  • Members
  • Pip
  • 272 posts
  • Gender:Male
  • Location:Newcastle

Posted 28 August 2007 - 12:15 PM

You need to use elseif from the second if, and else on the last one. Otherwise it'll want an else statment for all of the if's.

#3 bfva

    Young Padawan

  • Members
  • Pip
  • 42 posts

Posted 28 August 2007 - 12:32 PM

View PostMatt L, on Aug 28 2007, 12:15 PM, said:

You need to use elseif from the second if, and else on the last one. Otherwise it'll want an else statment for all of the if's.

I tried that but it doesn't work. The menu loads but the same one loads even if the other word is present in the uri.

I have updated my code to show you what i did.

Thanks for your help

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 28 August 2007 - 01:00 PM

Right well this is simple. On your inside your if condition you are setting $uri = 'home2'. You need to check if it is equal to:

$foo = 'bar'; // $foo is being set to 'bar'
$foo == 'bar'; // Checks to see if $foo equals 'bar'

So you need to change the single '=' to '==', as currently you are setting $uri to home, which will always come out as true.

Matt

Edited by Mr. Matt, 28 August 2007 - 01:02 PM.


#5 bfva

    Young Padawan

  • Members
  • Pip
  • 42 posts

Posted 28 August 2007 - 01:41 PM

View PostMr. Matt, on Aug 28 2007, 01:00 PM, said:

Right well this is simple. On your inside your if condition you are setting $uri = 'home2'. You need to check if it is equal to:

$foo = 'bar'; // $foo is being set to 'bar'
$foo == 'bar'; // Checks to see if $foo equals 'bar'

So you need to change the single '=' to '==', as currently you are setting $uri to home, which will always come out as true.

Matt

I tried it but it still doesn't work, nothing loads.

#6 dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 28 August 2007 - 04:37 PM

Hi,

say your page is http://example.com/test.php the what your codes uri would be is /test.php. But if it was in more files like http://example.com/t...sting/index.php it would be /test/testing/index.php. So try this:

<?php
$uri = $_SERVER['REQUEST_URI'];
if ($uri == '/h/home/index.php'){
echo"<ul>";
echo "<li id=\"current\"><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "</ul>";
}
elseif ($uri == '/h/home2/index.php'){
echo"<ul>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li id=\"current\"><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "<li><a href=\"mysite.com\">Highlight</a></li>";
echo "</ul>";
}
?>

Edited by dEcade, 28 August 2007 - 04:56 PM.


#7 Marxx

    Young Padawan

  • Members
  • Pip
  • 116 posts
  • Gender:Male
  • Location:Finland

Posted 29 August 2007 - 03:43 AM

I have this same problem earlier and find solution..

lets say that you have 2 urls:
http://www.mysite.net/home1/
http://www.mysite.net/home2/

In here, it doesn't matter what file you're showing in those folders. Only folder name matters..

<?php
$origurl = $_SERVER['REQUEST_URI'];

if(eregi('.',$origurl)){
$break = explode('/',$origurl);
array_pop($break);
$origurl = implode('/',$break);

$url_array = explode('/', $origurl);
$theurl = $url_array[1];
}

if($theurl == "home1") {

	  print("This is home1 folder");

} elseif($theurl == "home2") {

	  print("This is home2 folder");

}



Thanks for curthard89 for guiding me out! :rolleyes:
And hope this help you bfva...

#8 bfva

    Young Padawan

  • Members
  • Pip
  • 42 posts

Posted 29 August 2007 - 06:20 AM

View PostMarxx, on Aug 29 2007, 03:43 AM, said:

I have this same problem earlier and find solution..

lets say that you have 2 urls:
http://www.mysite.net/home1/
http://www.mysite.net/home2/

In here, it doesn't matter what file you're showing in those folders. Only folder name matters..

<?php
$origurl = $_SERVER['REQUEST_URI'];

if(eregi('.',$origurl)){
$break = explode('/',$origurl);
array_pop($break);
$origurl = implode('/',$break);

$url_array = explode('/', $origurl);
$theurl = $url_array[1];
}

if($theurl == "home1") {

      print("This is home1 folder");

} elseif($theurl == "home2") {

      print("This is home2 folder");

}



Thanks for curthard89 for guiding me out! :rolleyes:
And hope this help you bfva...

Thanks mate, that worked perfectly. Thanks everyone for your help.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users