homepage Welcome to WebmasterWorld Guest from 23.23.12.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Edit - Adding classes/ID's
humpingdan

10+ Year Member



 
Msg#: 3824002 posted 6:18 pm on Jan 11, 2009 (gmt 0)

Here is the PHP code to generate a ul:

//Construct an unordered list from $navlinks
//Accessibility: heading hidden from visual browsers by default.
$navigation = get_accesshide(get_string('youarehere','access'), 'h2')." <ul>\n";
$lastindex = count($navlinks) - 1;
$i = -1; // Used to count the times, so we know when we get to the last item.
$first = true;
foreach ($navlinks as $navlink) {
$i++;
$last = ($i == $lastindex);
if (!is_array($navlink)) {
continue;
}
if (!empty($navlink['type']) && $navlink['type'] == 'activity' && !$last && $hideactivitylink) {
continue;
}
$navigation .= '<li class="first">';
if (!$first) {
$navigation .= get_separator();
}
if ((!empty($navlink['link'])) && !$last) {
$navigation .= "<a onclick=\"this.target='$CFG->framename'\" href=\"{$navlink['link']}\">";
}
$navigation .= "{$navlink['name']}";
if ((!empty($navlink['link'])) && !$last) {
$navigation .= "</a>";
}

$navigation .= "</li>";
$first = false;
}
$navigation .= "</ul>";

return(array('newnav' => true, 'navlinks' => $navigation));
}

Output:

<ul>
<li class="first">Home</li>
<li class="first">Course</li>
<li class="first">Activity</li>
</ul>

But i want this:

<ul id="nav">
<li class="navtop">Home</li>
<li class="navitem"><a href="http://www.google.com">Course</a></li>
<li class="navitem"><a href="http://www.msn.com">Activity</a></li>
<li class="navitem"><a href="http://">Web Page</a></li>
</ul>

I've alread done: Added a class=nav to the <ul>

$navigation = get_accesshide(get_string('youarehere','access'), 'h2')." <ul class=nav>\n";

Now i need to output the first link with class=navtop

Any ideas?

[edited by: eelixduppy at 9:33 pm (utc) on Jan. 12, 2009]
[edit reason] snipped url [/edit]

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3824002 posted 6:49 pm on Jan 11, 2009 (gmt 0)

You don't need to repeat
class=navitem on each link.

Each of those links can already be identified by ul.nav li a { ... }
Note the spaces (NOT commas) between the items.

Add a class name to just the first item, not all of them.

humpingdan

10+ Year Member



 
Msg#: 3824002 posted 7:02 pm on Jan 11, 2009 (gmt 0)

Thanks, but how do i change the php to only run the code for the first link and not the others?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3824002 posted 7:39 pm on Jan 11, 2009 (gmt 0)

If the links are written out in a loop then you will need to add the class name only on the first time round the loop.

Using the other notation to style the other links will make things a lot more easy to fix.

[Fixed typos]

[edited by: g1smd at 7:58 pm (utc) on Jan. 11, 2009]

humpingdan

10+ Year Member



 
Msg#: 3824002 posted 7:52 pm on Jan 11, 2009 (gmt 0)

Here is the PHP:

//Construct an unordered list from $navlinks
//Accessibility: heading hidden from visual browsers by default.
$navigation = get_accesshide(get_string('youarehere','access'), 'h2')." <ul>\n";
$lastindex = count($navlinks) - 1;
$i = -1; // Used to count the times, so we know when we get to the last item.
$first = true;
foreach ($navlinks as $navlink) {
$i++;
$last = ($i == $lastindex);
if (!is_array($navlink)) {
continue;
}
if (!empty($navlink['type']) && $navlink['type'] == 'activity' && !$last && $hideactivitylink) {
continue;
}
$navigation .= '<li class="first">';
if (!$first) {
$navigation .= get_separator();
}
if ((!empty($navlink['link'])) && !$last) {
$navigation .= "<a onclick=\"this.target='$CFG->framename'\" href=\"{$navlink['link']}\">";
}
$navigation .= "{$navlink['name']}";
if ((!empty($navlink['link'])) && !$last) {
$navigation .= "</a>";
}

$navigation .= "</li>";
$first = false;
}
$navigation .= "</ul>";

return(array('newnav' => true, 'navlinks' => $navigation));
}

As posted in the first post.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved