Jump to content

Inserting a CSS class/id into an element using PHP


bronzemonkey

Recommended Posts

I'd like to use php to insert a class/id into an <a> element.

 

I'll provide some details so that my situation is clear and hopefully help someone to help me.

 

I use a file called "header.php" which also contains this piece of code:

 

<body<?
if(isset($bodyID))
echo ' id="'.$bodyID.'"';
?>>

 

Every webpage on my site (over 50) calls in this "header.php" and contain this piece of code - where xx is one of 7 ids that correspond to main 7 categories that all pages are grouped into:

 

<? $bodyID = 'xx'; ?>

 

I then have a dropdown menu with xhtml as below (but with more categories)

 

<ul>

<li><a id="aa" href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="aadrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a id="bb" href="#no">Category2<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="bbdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a id="cc" href="#no">Category3<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="ccdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

</ul>

 

I then use css to change the style of the <a> element and corresponding <ul> element if they match with a specific <body> element id. For example, if a page contains <body id="ccbody"> then my css will change the link style of <a id="cc"> and the hover style of <a id="cc"> and <ul id="ccdrop">.

 

However, this fails to complete do what I am asking in IE6. To avoid writing even more css, and to be able to remove all the ugly css required to trigger these style changes, I'd like to use php to insert one single id/class into whatever <a> element matches with a specific <body> id.

 

A conditional basically - every <a> element to have something that asks "if that body id up there is 'xxbody' i'll stick id=xx into this element.

 

My knowledge of php is extremely limited, and perhaps I have been presumptuous in assuming that I would have an idea as to what kind of php solution I need, but I would greatly appreciate some help here.

Link to comment
Share on other sites

Hi!

 

I'm not entirely sure that this will actually answer your question, but what I'm thinking:

 

I assume that your goal with this is to display each page according to one of seven style definitions, correct? It may be easier to have seven stylesheets where you define these separate styles, then include one of those according to the $bodyID instead of trying to change all the ids on the actually HTML page:

 

<?php
//page1.php of 50

$bodyID = 'cc';

@include 'header.php';
       ?>
       ________________HEADER.PHP__________________
       |<html>
       |<head>
       | <link ref="stylesheet" type="text/css" media="screen" href="stylesheets/<?php echo $bodyID; ?>Styles.css" />
       |</head>
       |<body> <!--Note that you don't have to worry about a body id tag -->
       |______________END HEADER.PHP________________
       <?php
//Code Body, all that stuff, etc.....

echo '
</body>
</html>';
?>

 

Would that work?

Link to comment
Share on other sites

Thanks for the reply. I may not have explained myself as clearly as I had hoped.

 

I am not using 7 different styles. What I have is one css style that is to be applied to whatever category a page falls under. I've been using the <id> attribute to do this but IE6 is not liking it.

 

What I am looking for is...rather than using 7 ids (one for each of the 7 <a> elements) I'd just like some php to write one id into the single <a> element that corresponds to the category the page is in. Basically, it produces a visual sign to the user that the page they are on can be found in the highlighted category in the menu (and then the drop down is also styled differently in that category).

 

I thought I could end up with some php in each of the 7 <a> elements, each looking for a specific body id. So the first <a> element would look for <body id="aa", second for "bb", etc and if that id was not present it would write nothing into the <a> element. If the body id was present then it would write the ONE id into that <a> element. That id being written into <a> would trigger a single fixed style coded for in a single stylesheet.

 

Have I managed to explain well?

Link to comment
Share on other sites

oooook. sorry bout that; I think I get it now (I hope - it'll be embarrassing if I dont!).

 

So you want to have this printed on the screen when the user is on one of your pages (assume the <<: and :>> denote your special style):

 

Category1   |   Category2   |<<:Category3:>>|   Category4   |   Category5   |   Category6   |   Category7



Congratulations! You're on one of the many pages we have to offer under Category3!

_____________________________________________________________________________________________
(User clicks Category7)
_____________________________________________________________________________________________


Category1   |   Category2   |   Category3   |   Category4   |   Category5   |   Category6   |<<:Category7:>>



Congratulations! You're on one of the many pages we have to offer under Category7!

 

IF that's a correct interpretation of your problem, then I've always solved it with arrays.

 

I would handle that by doing the following for your menu:

 

// Put your menu into array form.
// I usually use a database, so I would say if you have access to one,
// you may look into that as an easier option to building a huge menu array

$menu = array(); // this will contain the entire menu list
$menu['aa'] = array(); //this array will contain the submenu list under bodyID 'aa'
  $menu['aa'][] = array([link],[linkText]); //this array is fixed with 0 = linkReference and 1 = linkText
  $menu['aa'][] = array([link2],[link2Text]);
$menu['bb'] = array(); //this array will contain the submenu list under bodyID 'bb'
  $menu['bb'][] = array([link],[linkText]);
....

echo '<ul>' //initiate the main menu list

foreach($menu as $mId=>$menuItem) { //iterate through the entire menu list
$anchorID = null; // Make sure to nullify style before each iteration
if ($mId == $bodyID) { // If the user is requesting a page under the current submenu list, set the style!
  $anchorID = ' id="specialStyle"';
}
echo '<li><a id="$mId.$anchorStyle.'" href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="aadrop">'; // initiate submenu list - build the ids from the menuItemID ($mId) plus $anchorStyle, which will either append special style to the menu that matches the page or remain silent
foreach($menuItem as $mi) { //Iterate through the submenu list
  echo '
<li><a href="'.$mi[0].'" title="'.$mi[1].'">'.$mi[1].'</a></li>';
}
echo '
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>'; // Finish off submenu list
}

echo '</ul>'; // Finish off menu list

 

Did I get it this time?

Link to comment
Share on other sites

I'm not sure. Maybe you did get it but I just don't get what your reply means!

 

Like I said, my knowledge of php is at one of the most basic levels imaginable so I could be totally wrong in the kind of php I had imagined that I could use. I thought I might be able to use some php similar to the php that I used to write the <body> id but to write the <a> id.

 

I was hoping I could use php to read this code (which appears at the top of every page, each page using one of 7 possible values - b1 to b7)

 

<? $bodyID = 'b1'; ?>

 

and write this - id="active" - into the single corresponding <a> element (out of 7 in the menu). A conditional insertion of id="active" basically.

 

Something like this, but obviously the "php" in this example is nonsense...

 

<li><a
<?
if($bodyID='b1')
echo ' id="active";
?>
href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="aadrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a
<?
if($bodyID='b2')
echo ' id="active";
?>
href="#no">Category2<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="bbdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a
<?
if($bodyID='b3')
echo ' id="active";
?>
href="#no">Category3<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="ccdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

 

That way I keep the <ul> ids I already have (the css is very easy to trigger that and works in IE6) but if a page contains <? $bodyID = 'b1'; ?> then the php in the first <a> of the menu in the header will write id="active" into that <a> while all the other bits of php leave the <a> element untocuhed. Then when I click on a link that drops from the second category I end up on a page that contains <? $bodyID = 'b2'; ?> and this triggers the php in the second <a> of the menu to write id="active" while the first <a> reverts to the default state.

 

Is this possible? It seems like a simple conditional - "if this page has x, then write y into z" - with each <a> searching for a different x value...but I have no idea how I could use php in a simple manner to execute this simple conditional.

Link to comment
Share on other sites

See, what you're doing is an elongation of what I'm saying. I'm using the php "foreach()" function to write the menu blocks for me. In doing so, it checks each menu block against the supplied bodyID and inserts the 'id="active"' accordingly. If it helps, here are two situations where the output is the same:

 

Example 1:

<ul>

<?php
$lId = 'aa';
if ($bodyID = 'aa') { $lId = 'active'; }
?>

<li><a id="<?php echo $lId; ?>" href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="aadrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<?php
$lId = 'bb';
if ($bodyID = 'bb') { $lId = 'active'; }
?>

<li><a id="<?php echo $lId; ?>" href="#no">Category2<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="bbdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<?php
$lId = 'cc';
if ($bodyID = 'cc') { $lId = 'active'; }
?>

<li><a id="<?php echo $lId; ?>" href="#no">Category3<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="ccdrop">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

</ul>

 

Example 2:

<?php
$menu = array();
$menu['aa'] = array();
  $menu['aa'][] = array([link],[linkText]);
  $menu['aa'][] = array([link],[linkText]);
  $menu['aa'][] = array([link],[linkText]);
  $menu['aa'][] = array([link],[linkText]);
  $menu['aa'][] = array([link],[linkText]);
$menu['bb'] = array();
  $menu['bb'][] = array([link],[linkText]);
  $menu['bb'][] = array([link],[linkText]);
  $menu['bb'][] = array([link],[linkText]);
  $menu['bb'][] = array([link],[linkText]);
  $menu['bb'][] = array([link],[linkText]);
  $menu['bb'][] = array([link],[linkText]);
$menu['cc'] = array();
  $menu['cc'][] = array([link],[linkText]);
  $menu['cc'][] = array([link],[linkText]);
  $menu['cc'][] = array([link],[linkText]);
  $menu['cc'][] = array([link],[linkText]);
  $menu['cc'][] = array([link],[linkText]);
  $menu['cc'][] = array([link],[linkText]);

echo '<ul>';

foreach($menu as $mId=>$menuItem) {
$lId = $mId;
if ($mId == $bodyID) {
  $lId = 'active';
}
echo '<li><a id="'.$lId.'" href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="aadrop">';
foreach($menuItem as $mi) {
  echo '
<li><a href="'.$mi[0].'" title="'.$mi[1].'">'.$mi[1].'</a></li>';
}
echo '
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>';
}

echo '</ul>';
?>

 

Both of these output the exact same thing (that being the initial code block that you presented in your first post). The difference is that the latter uses one conditional as it iterates through your menu, while the former uses 3 different conditionals to check each time you write a menu block. The latter also has about 60,000 extra keystrokes, since you'll be writing out ALL of the code involved in each menu, rather than writing the block template and then supplying the template with the information it needs to complete the code block.

 

You might try copying both of these into a word processor and removing all of the extra code and comments and stuff. Then just try running them. You'll see that (once you change the [link] and [linkText] things to the appropriate names and references) you'll get the same output from both.

 

The advantages of the "foreach()" method are the following: all of your actual information is in one place, so if you need to add to it or edit it (i.e., a menu item or a link reference or something), it'll be really easy. Also, because now you're separating your content from your presentation, you can more easily change the menu later if you want (you'll have one code change to make instead of 7 in the case of the main menu items, and 1 code change instead of 35 in the case of submenus). Finally, along those same lines, if you find you need to alter the conditional, again, it's just one change instead of seven.

 

Anyway, hope we got it this time.

Link to comment
Share on other sites

Neither of your solutions worked I'm afraid (I tested them on my server)...even after I tried to replace all the "aa" "bb" "cc" stuff in the code (which is actually all the stuff I want to avoid having to write at all, by simply inserting 'id="active"' into <a>)

 

What happens is that "active" gets written into every <a> id irrespective of what <body id=""> says. The array example produced a blank page and didn't seem to give me any way to write the titles for each menu category.

 

 

Every page on my website contains this php at the very top, with * being 1-7 depending on the category it is part of (e.g. shoes, socks, gloves, hats, etc)

 

<? $bodyID = 'b*'; ?>

<?php require("header.php"); ?>

 

 

The "header.php" file has some php like this inside the <body> tag

 

<body<?
if(isset($bodyID))
echo ' id="'.$bodyID.'"';
?>>

 

The header.php file also contains my drop-down menu. When the visitor hovers over one of the 7 category names a set of links drop-down benefit it to pages within that category. For example, if they hovered over "hats" it would drop down some links to "small hats", "kids hats", "stupid hats", "animal hats", etc. Each one of those pages would have $bodyID='b5'.

 

I need my basic menu to look like this (note that each category contains a specific id for the <ul> element that it contains:

 

<ul>
<li><a "href="#no">Category1<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="drop1">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a "href="#no">Category2<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="drop2">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

<li><a "href="#no">Category3<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul id="drop3">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

through to Category 7
</ul>

 

But with some php being used to write 'id="active"' into the single <a> element of the category that a page is found in.

 

You seem to know what you are talking about so I am assuming that my explanation of my needs has been insufficiently clear! My apologies, but hopefully this time I have explained well!

Link to comment
Share on other sites

Ok I have found a solution that is valid xhtml but it is obviously not as efficient as it could be!

 

I'm a total beginner/ignorant so I just used some basic php tutorials to adapt some of the ideas that kael.shipman generously took the time to provide. This is the code I am using at the moment to get the desired effect:

 

<li><a <?php
if ( $bodyID == 'b1' ) {
echo 'id="active"';
} ?> href="#no">Category1</a>
<ul id="drop1">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
</li>

<li><a <?php
if ( $bodyID == 'b2' ) {
echo 'id="active"';
} ?> href="#no">Category2</a>
<ul id="drop2">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
</li>

<li><a <?php
if ( $bodyID == 'b3' ) {
echo 'id="active"';
} ?>" href="#no">Category3</a>
<ul id="drop3">
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
<li><a href="#" title="">Link</a></li>
</ul>
</li>

etc

 

What would be a more efficient way to do this? Could I use switch (tried but failed to implement it in a functioning manner)? What about using an array again?

Link to comment
Share on other sites

Yeah, admittedly the array trick can get a little bit messy, but it always does the job for me. If what you have works, then it works and that's all there is to it! However, if you're dissatisfied with it and wanted to try again I'll try to explain myself a little better.

 

As far as my code not working, that's my own carelessness; sorry. In the first case, you'll see that I used the assignment operator (=) instead of the equality operator (==) to check the equality of $bodyID to whatever I was comparing it to. This always evaluates to true, which is why it put "active" in all of your <a> tags. For the second case (the arrays), if you didn't replace "[link]" and "[linkRef]" with real quoted strings in those arrays, it would have thrown a parse error. Otherwise, I don't know what's wrong. It works fine on my system.

 

I know I've posted a lot of code, but below is an absolute breakdown of what I'm talking about. The top codeblock is the actual output of the bottom codeblock when I run it on my system. Like I said, the arrays can get a bit complicated, so it may not be worth it in this case, but you can decide that. I've stocked the array with the category names as keys, then the bodyID for that category as an element of the array, and an 'sm' key pointing to an array of all the submenu items. The names and links of those submenu items are then stored in an array. To make it (a little) easier to read, I've told the php to put two underscores (__) around each item that was inserted dynamically by the php, so if you want to, you can go through each inserted element and find it in the menu array to see why it's there.

 

SO; This

 

<ul>


<li><a href="#no">__Category1__<!--[if IE 7]><!--></a><!--<![endif]-->__<!-------BODYID b1-------->__
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="__linkRef1.1__" title="__linkText1.1__">__linkText1.1__</a></li>
<li><a href="__linkRef1.2__" title="__linkText1.2__">__linkText1.2__</a></li>
<li><a href="__linkRef1.3__" title="__linkText1.3__">__linkText1.3__</a></li>

<li><a href="__linkRef1.4__" title="__linkText1.4__">__linkText1.4__</a></li>
<li><a href="__linkRef1.5__" title="__linkText1.5__">__linkText1.5__</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li><a__id="active"__ href="#no">__Category2__<!--[if IE 7]><!--></a><!--<![endif]-->__<!-------BODYID b2-------->__
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="__linkRef2.1__" title="__linkText2.1__">__linkText2.1__</a></li>

<li><a href="__linkRef2.2__" title="__linkText2.2__">__linkText2.2__</a></li>
<li><a href="__linkRef2.3__" title="__linkText2.3__">__linkText2.3__</a></li>
<li><a href="__linkRef2.4__" title="__linkText2.4__">__linkText2.4__</a></li>
<li><a href="__linkRef2.5__" title="__linkText2.5__">__linkText2.5__</a></li>
<li><a href="__linkRef2.6__" title="__linkText2.6__">__linkText2.6__</a></li>
</ul>

<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>
<li><a href="#no">__Category3__<!--[if IE 7]><!--></a><!--<![endif]-->__<!-------BODYID b3-------->__
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="__linkRef3.1__" title="__linkText3.1__">__linkText3.1__</a></li>
<li><a href="__linkRef3.2__" title="__linkText3.2__">__linkText3.2__</a></li>
<li><a href="__linkRef3.3__" title="__linkText3.3__">__linkText3.3__</a></li>
<li><a href="__linkRef3.4__" title="__linkText3.4__">__linkText3.4__</a></li>

<li><a href="__linkRef3.5__" title="__linkText3.5__">__linkText3.5__</a></li>
<li><a href="__linkRef3.6__" title="__linkText3.6__">__linkText3.6__</a></li>
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>

</ul>

 

Is the result of this

 

<?php
$bodyID = 'b2';

$menu = array();
$menu['Category1'] = array();
  $menu['Category1']['bodyID'] = 'b1';
  $menu['Category1']['sm'] = array();
   $menu['Category1']['sm'][] = array('linkRef1.1','linkText1.1');
   $menu['Category1']['sm'][] = array('linkRef1.2','linkText1.2');
   $menu['Category1']['sm'][] = array('linkRef1.3','linkText1.3');
   $menu['Category1']['sm'][] = array('linkRef1.4','linkText1.4');
   $menu['Category1']['sm'][] = array('linkRef1.5','linkText1.5');
$menu['Category2'] = array();
  $menu['Category2']['bodyID'] = 'b2';
  $menu['Category2']['sm'] = array();
   $menu['Category2']['sm'][] = array('linkRef2.1','linkText2.1');
   $menu['Category2']['sm'][] = array('linkRef2.2','linkText2.2');
   $menu['Category2']['sm'][] = array('linkRef2.3','linkText2.3');
   $menu['Category2']['sm'][] = array('linkRef2.4','linkText2.4');
   $menu['Category2']['sm'][] = array('linkRef2.5','linkText2.5');
   $menu['Category2']['sm'][] = array('linkRef2.6','linkText2.6');
$menu['Category3'] = array();
  $menu['Category3']['bodyID'] = 'b3';
  $menu['Category3']['sm'] = array();
   $menu['Category3']['sm'][] = array('linkRef3.1','linkText3.1');
   $menu['Category3']['sm'][] = array('linkRef3.2','linkText3.2');
   $menu['Category3']['sm'][] = array('linkRef3.3','linkText3.3');
   $menu['Category3']['sm'][] = array('linkRef3.4','linkText3.4');
   $menu['Category3']['sm'][] = array('linkRef3.5','linkText3.5');
   $menu['Category3']['sm'][] = array('linkRef3.6','linkText3.6');

echo "<ul>\n\n";

foreach($menu as $mNm=>$menuArray) {
$lId = null;
if ($menuArray['bodyID'] == $bodyID) {
  $lId = '__id="active"__';
}
echo '
<li><a'.$lId.' href="#no">__'.$mNm.'__<!--[if IE 7]><!--></a><!--<![endif]-->__<!-------BODYID '.$menuArray['bodyID'].'-------->__
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>';
foreach($menuArray['sm'] as $mi) {
  echo '
<li><a href="__'.$mi[0].'__" title="__'.$mi[1].'__">__'.$mi[1].'__</a></li>';
}
echo '
</ul>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</li>';
}

echo "\n\n</ul>";

 

So if you like that, take it. If you like what you've got better, use that instead. I hope that did it, cause if it didn't, I'm out of ideas; good luck!

 

-kael

Link to comment
Share on other sites

As a beginner I really appreciate your patience and the time you've taken to try and help me understand arrays.

 

I'm going to use the code I already have simply because my deadline is fast approaching. Then I'll take my time to play around with the code you've provided, try to make sure that I understand what it all means, and then incorporate what I've learnt into future developments in order to improve efficiency and make updates easier. I can already see that php is a very powerful tool in web development. Looking forward to learning more about it and the possibilities it creates.

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.