Jump to content

Attaching iteration number to foreach-generated HTML-tags.


NoDice
 Share

Recommended Posts

Tried to search but could not find a description of a good way to produce an ordering number (to use as classes in HTML-tags) based on the iteration order using foreach. Would be very helpful for any tips or hints on where I can find a method.

By ordering numbers I mean from 1 to whatever based on the iterations resulting from the foreach loop where the tag in question is generated.

Cheers from a sunny Stockholm!

Link to comment
Share on other sites

A follow-up question is how I would be able to retrieve those ordering numbers from another page where I also want to make a color selection based on the order number produced by the itterations on the first page (menu items that need to be the same color as the dynamically created menu-items across pages)?

In my case, a business area is assigned a color in an accordion and an index page based on the ordering number I want to create; and then that color has to also be used on the in-dept page for that business area (so I will need to grab the ordering number again).

Link to comment
Share on other sites

Not exactly sure what you are after, but a typical approach to numbering is:

$i=1
foreach($items as $item){
    echo '<li>Item#' . $i . ': ' . $item->title . '</li>';
    $i++;
}
 

Hope that helps.

EDIT: Just saw your second post, and I don't think I really understood what you are looking for. It sounds like you want to generate the number via the admin, rather than on the front-end. Is that correct?

  • Like 1
Link to comment
Share on other sites

Either manually iterate an iterator variable within the foreach loop:

$i = 0;
foreach ($somaeArray as $item) {
	// your code
	echo "my-class-$i";
	$i++;
}

Or you can deal with a for loop, and you can access your items in the page array like so:

$selectedItems = $pages->find("your-selector");
$len = count($selectedItems); // or $selectedItems->count();
for ($i=0; $i < $len; $i++) { 
	// so something with $selectedItems->eq($i);
	echo "my-class-$i";
}

Edit: adrian kind of beat me to it =)

  • Like 1
Link to comment
Share on other sites

A follow-up question is how I would be able to retrieve those ordering numbers from another page where I also want to make a color selection based on the order number produced by the itterations on the first page (menu items that need to be the same color as the dynamically created menu-items across pages)?

In my case, a business area is assigned a color in an accordion and an index page based on the ordering number I want to create; and then that color has to also be used on the in-dept page for that business area (so I will need to grab the ordering number again).

Why not save the number or color on each page as data?

echo $page->color; 

Or you can use

$page->id 

 for that too:

.my-class-1234 {
    background-color: green;
}

.my-class-1235 {
    background-color: red;
}

in your accordion:

<li class="my-class-<?php echo $child->id; ?>"><?php echo $child->title; ?></li>

on your page:

<body class="my-class-<?php echo $page->id; ?>">
	
	
	
</body>

That makes you dependent on the id though, which is generated form the system, so maybe you can define your own identifier $page->myColorID; or something, and work with that.

  • Like 1
Link to comment
Share on other sites

Thanks guys for the lightning replies - that is what I am looking for (at least for the first part)!

The second part of my problem is that I somehow need to get the number produced for color reference on another page.

From the beginning I set ordering numbers manually in admin, but that didn't feel like a very good solution (and I ran into some other problems since I have a checkbox that you tick if you want the item to be included in a menu and so on). So any ideas would great to hear!

I guess one way would be to run the full iteration and then hide all instances that does not match the page title. Does not feel very good though. 

Link to comment
Share on other sites

Thanks owzim (you answered when I was typing my last reply)!

I have to ponder that. The thing is, the point has been that the accordion is created dynamically with the creation of new business areas.

I have predefined a number of colors that will never be exceeded in css that will set the color for a new business-area when created (based on the order-number class).

The color is used on the front page accordion as well (no problem there as the order number will be the same if I sort on checkbox (meaning the ones that are ticked will come first).

The real problem is the page where I just have the one business area, that is the one I can't really figure out (without doing iterations there as well and hiding the ones I don't need).

Link to comment
Share on other sites

Thanks owzim (you answered when I was typing my last reply)!

I have to ponder that. The thing is, the point has been that the accordion is created dynamically with the creation of new business areas.

I have predefined a number of colors that will never be exceeded in css that will set the color for a new business-area when created (based on the order-number class).

The color is used on the front page accordion as well (no problem there as the order number will be the same if I sort on checkbox (meaning the ones that are ticked will come first).

The real problem is the page where I just have the one business area, that is the one I can't really figure out (without doing iterations there as well and hiding the ones I don't need).

OK if you really need the same iterators from the accordion, you might also iterate on your area page as well to get the same index as in the accordion:

$i = 0;
$iteratorInList = -1; // set the default -1 if not in list
// "/areas/" being the url to the parent of your areas
foreach ($pages->get("/areas/")->children as $area) {
	// find out if current page in loop is the same as current $page
	if($area == $page) {
		$iteratorInList = $i;
		break;
	}
	$i++;
}
echo $iteratorInList;

Seems a bit cumbersome, but I don't know of a method like $somePage->index($list) ... similar to jQuery.

  • Like 2
Link to comment
Share on other sites

Actually, my question remains after some more checking.

I want the foreach to order the same way the child pages are presented in the admin - with the exception that I want the non-checked items first (both checked and non checked ordered as in the admin but the non-checked group first). When I sort by checked, it puts the ones that are checked last the way I want, but the other items are sorted in reverse order compared to admin. Any idea why that might be, and what I can do about it?

Link to comment
Share on other sites

I actually solved it, putting sort=sort after the checkbox sort. Worked well. Kind of strange that the checkbox reversed the order (did not matter if if I inverted what it did, still did not work until I found the sort=sort).

Tomorrow, I will get to work on trying to get the business-area page going with your other suggestion - thanks again!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...