working outside an 'include'

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
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

working outside an 'include'

Post by Addos »

I wonder if anybody can help me work out the following. I have a single page in a small shopping cart and there are 2 categories of items for sale CD’s and books’. I want to run an ‘if statement’ looking for the categories and if it’s ‘1’ then I want the <h1> heading to read “CD’s for sale” and if its 2 I want it to read “books for sale”. Now my problem is that when I run the script below everything returns the correct result….

Code: Select all

<h1>???? for sale </h1>
 <hr width="680" size="1" noshade>
  <p><?php echo($cart->cartLinks());?></p>
<?php include("2/IncResults.php");?>
 <?PHP if ($urlcategory = = 1) echo 'heading h1';?>
<?PHP if ($urlcategory = = 2) echo 'heading h2';?>
However what I really need to run in order to make the page display/format in the browser correctly is

Code: Select all

<h1> <?PHP if ($urlcategory = = 1) echo 'heading h1';?>
<?PHP if ($urlcategory = = 2) echo 'heading h2';?></h1>
 <hr width="680" size="1" noshade>

  <p><?php echo($cart->cartLinks());?></p>
<?php include("2/IncResults.php");?>
But because the <?PHP if ($urlcategory = = 1) echo 'heading h1';?> etc is above the include statement it’s throwing the Undefined variable error, so can anyone give me a few ideas how this is normally resolved?
Many thanks
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

change the = = to == or === ?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi,
Thanks for the reply but the code works fine its just that I can't get it to work outside (above) <?php include("cw2/CWIncResults.php");?> and wonder how to go about sorting this.
Thanks
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

($urlcategory = = 2)

WILL NOT WORK CORRECTLY...

($urlcategory == 2)

WILL WORK AS YOU EXPECT...


Sorry for yelling, but I wanted to make sure you understood us..
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for this and I appreciate that = = should be == however as I typed this up in Word this is where the = = came from and in the code on my .php page it’s == so really I still have the same problem. I understand that there should not be the gap but as I say this is only cos I edited my question in a Word doc before I posted it. So I’m still stuck with the problem of trying to get this original question sorted.
Thanks again
B
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: working outside an 'include'

Post by volka »

Addos wrote:But because the <?PHP if ($urlcategory = = 1) echo 'heading h1';?> etc is above the include statement it’s throwing the Undefined variable error
If $urlcategory is defined in 2/IncResults.php you can use it only after the file is included.
Post Reply