Jump to content

two questions about rendering a navigation


lecrackffm
 Share

Recommended Posts

Hello everyone, 

i have written a simple function to render the Navigation for my "Onepager"

Here is the code: 

<?php
function renderOnepagenav($onepageroot) {
	$sections = $onepageroot->children;
	echo "<ul>";
	$id = '';
	foreach ($sections as $item) {
		$id ++;
		echo "<li><a href='#section-$id'>$item->title</a></li>";
	}
	echo "</ul>";
}

Every Sektion is a child-page of home in the backend.

It works fine but i would like to add two modifications, where i need your help:

1.  i would like to skip the first  (child-)page.

2. I would like to add a class only to the first  rendered <li>  Element.

 

All kind of advice is highly appreciated.

Thank you, 

Gregor

Link to comment
Share on other sites

Typically the way to do this is define $i=0; before the foreach loop and then at the end (just inside) do: $i++ 

This lets you check which iteration of the loop you are at, so if $i==0 then "continue;"

Hope that helps - it's not a full code example, but hopefully it will help you to understand what is required.

  • Like 3
Link to comment
Share on other sites

Hi,

I think this is not that difficult, you work already with an id which you can use now:

  On 1/27/2017 at 7:50 PM, lecrackffm said:

1.  i would like to skip the first  (child-)page.

Expand  

You could add a if statement for the very first id in your foreach loop:

$id = 0; // set it to zero
	foreach ($sections as $item) {
		if ($id == 0) continue
		else {
		echo "<li><a href='#section-$id'>$item->title</a></li>";
		}
		$id ++;
	}

NOTE: Haven't tested this

  On 1/27/2017 at 7:50 PM, lecrackffm said:

2. I would like to add a class only to the first  rendered <li>  Element.

Expand  

As you want to skip the first element, which id is equal to 0, the first rendered <li> will have id 1, so if you add another if statement in your foreach loop for id ==1 you can filter that item out and the rest gets the normal tags. See code below:

$id = 0; // set it to zero
	foreach ($sections as $item) {
		if ($id == 0) continue
		else {
			if ($id == 1){
				echo "<li class='yourclassforfirstitem'><a href='#section-$id'>$item->title</a></li>";
			}
		echo "<li><a href='#section-$id'>$item->title</a></li>";
		}
		$id ++;
	}

Haven't tested it so far, but I expect this works (maybe with some adjustments)

Edited by Harmen
edited the code ($i++ to the end)
  • Like 2
Link to comment
Share on other sites

  On 1/27/2017 at 8:10 PM, Harmen said:

 

$id = 0; // set it to zero
	foreach ($sections as $item) {
		if ($id == 0) continue
		else {
		echo "<li><a href='#section-$id'>$item->title</a></li>";
		}
		$id ++;
	}

 

 

Expand  

@Harmen you must ensure that you count up the $id before continue.

$id = -1; // set it -1, as we first count it up +1, (it will be 0 on its first check then!)
foreach($sections as $item) {
	$id++; // count up +1
	if($id == 0) continue; // skip the first child
	if($id == 1) {
		echo "<li class='yourclassforfirstitem'><a href='#section-$id'>$item->title</a></li>";
	} else {
		echo "<li><a href='#section-$id'>$item->title</a></li>";
	}
}

 

  • Like 1
Link to comment
Share on other sites

  On 1/28/2017 at 5:10 AM, horst said:

you must asure that you count up the $id before continue.

Expand  

You're right, but as I said. I did not tested it, and wrote this piece of code in a rush. Thanks for the addition.

  On 1/28/2017 at 6:54 AM, Robin S said:

Isn't it just a stylistic preference whether you increment the counter at the beginning or the end of your operations?

Expand  

I think it is, most of the time I use it at the end of operations.

  • Like 1
Link to comment
Share on other sites

  On 1/28/2017 at 6:54 AM, Robin S said:

Isn't it just a stylistic preference whether you increment the counter at the beginning or the end of your operations?

Expand  

Sure! Only important thing is to do it!  -- before you continue

If $id is 0 and the first thing you do is: if(0 == $id) continue; nothing ever will happen. :)

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...