Jump to content

Need some help adding some conditionals to an array.


Linda A

Recommended Posts

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:

[code]

$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];
[/code]

If anyone could help me to get this to work as intended, I would really appreciate it. :)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

$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:

[code]
<?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];
};

?>
[/code]

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?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.