Jump to content

Get relational data from pagearray set using Input Field Type Multiple page, and without using echo


kathep
 Share

Recommended Posts

I am trying to output my first instance of multiple fields from a pagearray, and only have partial success.

My problem involves the following fields and templates:

  • assignment.php
    • readings_from_list
  • readings.php
    • title
    • book_section_reading_time

Using either of the following code options:

Code option 1, adapted from @teppo's solution here:

  • // using pages->find to display muliple values from a PageArray
    // only one selection displays though
    $reading = $page->reading_from_list;
    
    $reading_w_heading = '';
    
    $reading_w_heading = $pages->find();
    foreach ($reading as $read) {
    		$reading_w_heading = "<p><a href='$read->url'>" . $read->title . "</a> - " . $read->book_section_reading_time->title . " of reading</p>" ;
    	}
    
    $content = <h4>Read:</h4>" . $reading_w_heading;
    
    

​Code option 2, adapted from @nico's solution here:

  • $reading_w_heading = '';
    
    $reading = $page->reading_from_list;
    
    if($reading instanceof PageArray) {
    	foreach($reading as $read) {
    		$reading_w_heading = "<p><a href='$read->url'>" . $read->title . "</a> - " . $read->book_section_reading_time->title . " of reading</p>" ;
    	}
    }
    
    $content = <h4>Read:</h4>" . $reading_w_heading;
    

Outputs this result:

post-2947-0-19240500-1419864015_thumb.pn

Despite more than one item selected in the field readings_from_list:

post-2947-0-55949900-1419864149_thumb.pn

I suspect this has a really simple solution, but my knowledge is so basic that I can't figure it out. Frustrating!

Could someone point out to me where I am going wrong?

Note: I don't want to use echo because I am using _init.php and _main.php from the PW_default template, and like this system very much. However, when I echo anything, it appears above the _main.php content (and therefore above my main navigation) - suboptimal.

Link to comment
Share on other sites

Ah, @horst thank you! You have stopped many hours of frustration!

@nico, I wanted the output to look like this:

post-2947-0-49049200-1419867639_thumb.pn

which it now does, thanks to @horst reminding me of the concat dot in your original solution.

BTW, I am very grateful to have access to such a helpful community of processwire fans. This just adds to the already very awesome experience of using processwire.

  • Like 3
Link to comment
Share on other sites

...

BTW, I am very grateful to have access to such a helpful community of processwire fans. This just adds to the already very awesome experience of using processwire.

... and not to forget that your PHP skills will raise too. :)

BTW, how have you found PW?

Link to comment
Share on other sites

Yes, I am learning PHP quite fast using PW. I didn't know any before last week  :rolleyes:

I think I found PW by googling for a lightweight CMS. The bloated options of the big, popular CMSs, and the limitations of certain small open source CMSs were unsuitable for my needs. I hoped there was something better.

I find developing in PW quite addictive. I'm not sure if its the full control of information architecture, or how much fun php is, or the ease of use of the PW API. Do others find it addictive?

Anyway, I am having a lot of fun with it.

  • Like 3
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...