Jump to content

"Inserting" Other field into foreach


louisstephens
 Share

Recommended Posts

I had a sidebar template set up with a repeater (as it needed to display links offsite) and everything was going smoothly:

<ul class="sideNav" data-spy="affix" data-offset-top="490" >
				<?php
					foreach($page->selectHeader->pageSidebar as $sideNav) {
    					echo "<li><a href='{$sideNav->sidebarURL}'' >$sideNav->sidebarTitle </a></li>";   
					} 
				?>
			</ul>

The repeater was set to include 2 fields (the link title and link URL) and output it in my template (usually constrained to 8 links). However, I now realize that I must remove the 1st and 3rd links and put them in another template as they are dependent on the page (and cant just be entered manually.

My question, is it possible to inject the two links into the foreach loop so that they still appear in the 1st and 3rd spot respectfully? The two were changed to a page select, but I didnt know if that was even possible to achieve.

I was hoping that I didnt have to create 8 separate fields to achieve the desired result.

Link to comment
Share on other sites

Why not? :-)

If those 1st and 3rd links need to be grabbed only once, then first grab them using normal PW API, outside the foreach.

Use a counter with your loop:

$myFirstLink = 'http://www.google.com';// however you got it
$myThirdLink = 'http://www.bbc.co.uk';// however you got it

$out = '';

$i = 1;
foreach($page->selectHeader->pageSidebar as $sideNav) {	
  if($i == 1) $out .= "<li><a href={$myFirstLink}>{$whateveYouWantHere}</a></li>";
  if($i == 2) $out .= "<li><a href={$myThirdLink}>{$whateveYouWantHere}</a></li>";
  $out .= "<li><a href='{$sideNav->sidebarURL}'' >$sideNav->sidebarTitle </a></li>";
  $i++;
} 

echo $out;
Link to comment
Share on other sites

I might be misinterpreting the requirements, but at the first read, they screamed "Repeater Matrix" at me, a 3.0.5 alpha feature which would let you set up a repeater with a set of fields for external links like you already have and alternatively allow a page field in that same repeater as well. It would even be a perfect playground for the new field templates (as explained at the end of the linked blog entry) to make the code as concise as it can get. No idea if pro fields or PW 3 are valid options for this project, but I thought it worth mentioning.

Link to comment
Share on other sites

Thanks Kongondo. That was exactly what I needed. However, I think I might have goofed somewhere. I can pull in Link#3 just fine, but when I attempt to pull the first link (page field) I can get the id. Using ->url doesnt seem to be working for me. Perhaps I used it in the wrong location?

<?php

  $myFirstLink = $page->sideBarURL1->url; //however you got it
  $myThirdLink = $page->sideBarURL3;// however you got it
  $out = '';
  
  $i = 1;

  foreach($page->selectHeader->pageSidebar as $sideNav) {	
  if($i == 1) $out .= "<li><a href='{$myFirstLink}'>{$whateveYouWantHere}</a></li>";
  if($i == 2) $out .= "<li><a href='{$myThirdLink}'>{$whateveYouWantHere}</a></li>";
  $out .= "<li><a href='{$sideNav->sidebarURL}' >$sideNav->sidebarTitle </a></li>";
  $i++;
  } 
echo $out;
				
				
				
				
				
				
				?>
Link to comment
Share on other sites

Yeah, to be honest I dont know what was wrong with it. It was set on asm multi select so I did change it to "single". However, after deleting the field and setting up a new one it seems to be working now. I have it back on my local set up trying to figure out why it "broke".

Link to comment
Share on other sites

  • 2 weeks later...

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...