Hajosch Posted August 4, 2018 Share Posted August 4, 2018 (edited) Hello everyone! I am an absolute ProcessWire novice and unfortunately I don't have too good PHP knowledge. Nevertheless, I have made good progress with the development of our ProcessWire website thanks to this helpful forum. But now I have a problem with the formatting of field queries, where I still haven't found a solution after 2 days of searching. I have a field "authors", which is created as type: PageReference / MultiplePages. I would now like to query all authors of this field who are saved for a single book title. This works well with the following query: <!-- Query of the authors --> <?php if (count($page->authors)) { echo "<u>AUTOR/EN</u>: "; foreach($page->authors as $authors) { echo "<a href='{$authors->url}'>{$authors->title}</a>; "; } } ?> Starting with the first author, each of the following authors is inserted a semicolon to separate the names of the individual authors. That works correctly. But I would like to have a dot after the last author, not a semicolon. So I want to get a result like this: Author-1; Author-2; Author-3. In the meantime I found out on this forum that the command 'implode' can be used for this. But I simply can't implement this solution in my specific case due to lack of PHP knowledge and I haven't found any patterns that I can use for my type of field query. So I would be very grateful for support. Where do I have to insert the command 'implode' with which additional parameters in my code? It would be great if someone could help me by showing me how to change the code of my previous field query. Thank you and best regards Hajosch, Potsdam/Germany Edited August 4, 2018 by cstevensjr Formatted the Code for Readability Link to comment Share on other sites More sharing options...
teppo Posted August 4, 2018 Share Posted August 4, 2018 There are various ways you can achieve this, including plain PHP and even plain HTML+CSS (depending on your use case you might be able to convert this to a list and add those delimiters as ::after pseudo-elements – it might even make more sense semantically), but probably the easiest way would be the implode() method of the WireArray class. Since $page->authors returns an instance of PageArray and PageArrays inherit the methods of the WireArray class, you can do something like this in your code: <!-- Query of the authors --> <?php if (count($page->authors)) { echo "<u>AUTOR/EN</u>: "; echo $page->authors->implode("; ", function($item) { return "<a href='{$item->url}'>{$item->title}</a>"; }); echo "."; } ?> A plain PHP solution using a counter variable would look something like this – this is probably the easiest method to understand for a not-so-experienced developer: <!-- Query of the authors --> <?php $author_count = count($page->authors); if ($author_count) { $count = 0; echo "<u>AUTOR/EN</u>: "; foreach ($page->authors as $author) { ++$count; echo "<a href='{$author->url}'>{$author->title}</a>"; echo $count < $author_count ? "; " : "."; // note: this is a ternary operator, see http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary for details } } ?> 3 1 Link to comment Share on other sites More sharing options...
dragan Posted August 4, 2018 Share Posted August 4, 2018 You can also use some function like substr / str_replace or rtrim. https://stackoverflow.com/questions/5592994/remove-the-last-character-from-string You would have to build up a variable instead of echo-ing instantly to achieve this. i.e. use something like $output .= .... inside the foreach loop, then do the str_replace() and finally echo() it. 1 1 Link to comment Share on other sites More sharing options...
AndZyk Posted August 4, 2018 Share Posted August 4, 2018 Hello @Hajosch, you could also do something like this: <!-- Query of the authors --> <?php if (count($page->authors)) { echo "<u>AUTOR/EN</u>: "; foreach($page->authors as $authors) { echo "<a href='{$authors->url}'>{$authors->title}</a>"; if ($authors === $page->authors->last) { echo "."; } else { echo "; "; } } } ?> Regards, Andreas 3 1 Link to comment Share on other sites More sharing options...
OLSA Posted August 5, 2018 Share Posted August 5, 2018 Hello, here is example using HTML "li" tags for list, and CSS pseudo-classes for styling HTML. <?php if (count($page->authors)) { $out = ''; foreach($page->authors as $authors) { $out.= "<li><a href='{$authors->url}'>{$authors->title}</a></li>"; } echo '<ul class="author-list">'.$out.'</ul>'; } ?> And CSS part: .author-list { list-style-type: none; } .author-list li { display: inline; } .author-list li:after { content: "; "; } .author-list li:last-child:after { content: "."; } This example provide few options to style list inside CSS file only (inline or not, without bullets or not, various dividers etc.).Codepen demo Regards. 3 1 Link to comment Share on other sites More sharing options...
Hajosch Posted August 6, 2018 Author Share Posted August 6, 2018 Wow. This is an incredible feedback and an amazingly friendly forum. I thank everyone who took the time to offer me a solution to my problem. I have now started with the first solution of teppo (the implode() method of the WireArray class), and it worked immediately. On 8/4/2018 at 11:16 AM, teppo said: <!-- Query of the authors --> <?php if (count($page->authors)) { echo "<u>AUTOR/EN</u>: "; echo $page->authors->implode("; ", function($item) { return "<a href='{$item->url}'>{$item->title}</a>"; }); echo "."; } ?> Thank you, thank you and thank you again! I will now have another thorough look at the other solutions proposed by Dragan, AndZyk and OLSA and then give you a feedback that may also help other users who stumble across the same problem. Thank you very much and have a nice day! Hajosch 2 Link to comment Share on other sites More sharing options...
Soma Posted August 6, 2018 Share Posted August 6, 2018 This is the shortest method using implode(): if($page->authors->count) echo $page->authors->implode("; ", "<a href='{url}'>{title}</a>") . "."; 5 1 Link to comment Share on other sites More sharing options...
Hajosch Posted August 8, 2018 Author Share Posted August 8, 2018 Hello Soma and everyone else who helped me to solve my problem. SOMA: Thanks for your suggestion, which also works well and which I took as standard because of the shorter code. It's amazing how many different solutions Processwire has to offer. I've learned a lot. Thanks again to everyone and best regards Hajosh 4 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now