Jump to content


Photo

Need some help adding some conditionals to an array.


  • Please log in to reply
3 replies to this topic

#1 Linda A

Linda A
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 April 2006 - 09:50 AM

Hi,

I am using a php array as part of a menu, to determine which section is currently being viewed and based on that serve up the correct submenu. I don't really know much about php myself, so I had help putting the code together, and now I have run into a few instances where the code fails.

To fix this, I need to add some further conditions to the array, so I cobbled together an example of what I'd like it to do:


$URL = $_SERVER['PHP_SELF'];

$Parts = explode("/",$URL);

Step 1: If $Parts[2] is NOT index.php or index.html, go ahead and define $CurrentSection. Not sure how I would get it to not to go onto step 2 and 3 if step 1 is as it should be, though.

if ($Parts[2] != "index.php|index.html")
{
  $CurrentSection = "/".$Parts[2]."/"; 
}

Step 2: If step 1 wasn't true, check to see if $Parts[2] IS index.html. If so, terminate (the default menu should be displayed, so no values need to be set). Again, if this is true, it shouldn't go on to step 3.

elseif ($Parts[2] == "index.html")
{
  die; 
}

Step 3: If part 2 is index.php and $Parts[3] exists and $Parts[3]  is within the array, set $CurrentSection. Otherwise, terminate.

elseif ($Parts[2] == "index.php") && ($Parts[3] != "NULL") && (array_key_exists($WhichMainMenuItem,$Parts[3]))
{
  $CurrentSection = "/".$Parts[3]."/"; 
}

else 
{
  die;
}

Step 4: The following variables should only be defined in those instances when $CurrentSection has been defined.

$MainMenuItem = $WhichMainMenuItem[$CurrentSection];

$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];

$SubMenu = $WhichSubMenu[$CurrentSection];

$SubMenuItem = $WhichSubMenuItem[$CurrentSection];

If anyone could help me to get this to work as intended, I would really appreciate it. :)

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 23 April 2006 - 01:19 PM

Where did you set $WhichMainMenuItem (Marked with red)?
After you set it, this should work:


<?php
$URL = $_SERVER['PHP_SELF'];
$Parts = explode("/",$URL);

if ($Parts[2] != "index.php" && $Parts[2] != "index.html")
{
$CurrentSection = "/".$Parts[2]."/";
}

elseif ($Parts[2] == "index.html")
{
die;
}

elseif ($Parts[2] == "index.php" && $Parts[3] != "NULL" && array_key_exists([!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]$WhichMainMenuItem[!--colorc--][/span][!--/colorc--],$Parts[3]))
{
$CurrentSection = "/".$Parts[3]."/";
}

else
{
die;
}

if(isset($CurrentSection)){
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];
$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
$SubMenu = $WhichSubMenu[$CurrentSection];
$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
};
?>



Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Linda A

Linda A
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 April 2006 - 01:51 PM

$WhichMainMenuItem is an array defined at the beginning of the script. I didn't include the whole before because it got so spammy, but here it is:

<?php 

$WhichMainMenuItem = Array ( 
'/FAQ/' => 'VolumeI',
'/SSM/' => 'VolumeI',
'/Concordance/' => 'VolumeII',
'/Encyclopaedia/' => 'VolumeII',
'/Heraldry/' => 'VolumeII',
'/History/' => 'VolumeII',
'/Characters/' => 'VolumeIII',
'/Prophecies/' => 'VolumeIII',
'/Artwork/' => 'VolumeIV',
'/Books/' => 'VolumeIV',
'/Images/' => 'VolumeIV'
); 

$WhichMainMenuItemID = Array ( 
'/FAQ/' => 'main01',
'/SSM/' => 'main01',
'/Concordance/' => 'main02',
'/Encyclopaedia/' => 'main02',
'/Heraldry/' => 'main02',
'/History/' => 'main02',
'/Characters/' => 'main03',
'/Prophecies/' => 'main03',
'/Artwork/' => 'main04',
'/Books/' => 'main04',
'/Images/' => 'main04'
); 

$WhichSubMenu = $WhichMainMenuItem; 

$WhichSubMenuItem = Array ( 
'/FAQ/' => 'FAQ',
'/SSM/' => 'SSM',
'/Concordance/' => 'Concordance',
'/Encyclopaedia/' => 'Encyclopaedia',
'/Heraldry/' => 'Heraldry',
'/History/' => 'History',
'/Characters/' => 'Characters',
'/Prophecies/' => 'Prophecies',
'/Artwork/' => 'Artwork',
'/Books/' => 'Books',
'/Images/' => 'Images'
); 

$URL = $_SERVER['PHP_SELF'];

$Parts = explode("/",$URL);

if ($Parts[2] != "index.php" && $Parts[2] != "index.html")
{
$CurrentSection = "/".$Parts[2]."/";
}

elseif ($Parts[2] == "index.html")
{
die;
}

elseif ($Parts[2] == "index.php" && $Parts[3] != "NULL" && array_key_exists($WhichMainMenuItem,$Parts[3]))
{
$CurrentSection = "/".$Parts[3]."/";
}

else
{
die;
}

if(isset($CurrentSection)){
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];
$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
$SubMenu = $WhichSubMenu[$CurrentSection];
$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
};

?>

Do I have the correct syntax then for checking whether $Parts[3] is part of this array?

Edited to add: I tried out the modified code, and I now get this error:

Warning: array_key_exists(): The second argument should be either an array or an object in <filename>.

However, it only occurs when I access the code on an URL including the 'www'. So, wwww.domain.com/Citadel/About/ generates the error, but not domain.com/Citadel/About/.

Why would that happen?

#4 Linda A

Linda A
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 April 2006 - 03:04 PM

In addition to the error mentioned in the previous post, I've also found another problem. When I try this code out on an actual page (the page with it on is then included into the page at the top), the page doesn't load beyond the include code. Is that due to the 'die' killing the rest of the page too? If so, how I can get to just stop processing the script in question, but to go on to process the rest of the page?





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users