JerryDi Posted November 1, 2023 Share Posted November 1, 2023 Hi, I am new to PW so please excuse me if this is a bit basic! I have ~1000 golfer names using the player template. Other templates with results for various championships use the player name in a field called winner_name which is a Page Reference field linked to the player template title field. When viewing the player page I want to show all related pages that use a specific template. Please can you advise me on the php syntax required? Thanks Jerry Link to comment Share on other sites More sharing options...
ngrmm Posted November 1, 2023 Share Posted November 1, 2023 assuming your other template name is championship you could do this in your player.php template $playerChampionships = $pages->find('template=championship, winner_name=$page->id'); foreach($playerChampionships as $championship) { // … } Link to comment Share on other sites More sharing options...
JerryDi Posted November 1, 2023 Author Share Posted November 1, 2023 Thank you for your reply. This is what I have inserted into the template: <p>Below you can follow links to this player's tournament wins </p> <?php $playerChampionships = $pages->find('template=championship_results, winner_name=§page->id'); foreach($playerChampionships as $championship) { echo "$championship <br>"; } ?> It does not return any results, although the player I am testing it with has 20+ wins to his name. Have I done something wrong? Note..I am also a php novice! thanks Jerry Link to comment Share on other sites More sharing options...
ngrmm Posted November 1, 2023 Share Posted November 1, 2023 I guess, it's this typo winner_name=§page->id' Should be winner_name=$page->id' You could also try winner_name=$page' Link to comment Share on other sites More sharing options...
JerryDi Posted November 1, 2023 Author Share Posted November 1, 2023 thanks, I tried both of these but they both produce an error: Error: Exception: Unrecognized operator: $ (in wire/core/Selectors.php line 416) Link to comment Share on other sites More sharing options...
ngrmm Posted November 1, 2023 Share Posted November 1, 2023 try this $playerChampionships = $pages->find("template=championship_results, winner_name=$page->id"); // or $playerChampionships = $pages->find("template=championship_results, winner_name={$page->id}"); 1 Link to comment Share on other sites More sharing options...
JerryDi Posted November 2, 2023 Author Share Posted November 2, 2023 thank you! This now produces the ID numbers for each championship. How would I change that to give the championship page titles as URLs? Link to comment Share on other sites More sharing options...
ngrmm Posted November 2, 2023 Share Posted November 2, 2023 @JerryDi you can access fields and variables of your page (in this case champrionship pages) echo "<a href='$championship->url'>$championship->title</a>; have a look here: https://processwire.com/api/ref/page/ 2 Link to comment Share on other sites More sharing options...
JerryDi Posted November 2, 2023 Author Share Posted November 2, 2023 thank you for your patience with me! Having now grasped this I am sure I can complete my project as it covers almost all of the functionality needed J 2 Link to comment Share on other sites More sharing options...
JerryDi Posted November 7, 2023 Author Share Posted November 7, 2023 One further question if I may: this works fine, but I now want to add winner_name [which is a PageRef field derived from the title field in a separate template, Players] to the string that is returned I have tried this additional text [in bold as below], but it only returns the id number for the winner How can I get it to return the title field from the Players template ie the full winner's name? <?php $playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}"); foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - $championship->winner_name</a></li>"; } ?> thanks Link to comment Share on other sites More sharing options...
da² Posted November 7, 2023 Share Posted November 7, 2023 41 minutes ago, JerryDi said: How can I get it to return the title field from the Players template ie the full winner's name? $championship->winner_name->title Link to comment Share on other sites More sharing options...
JerryDi Posted November 7, 2023 Author Share Posted November 7, 2023 thanks for this da2, I had tried this but it produced this: County Championship 2023 - 1442->title I have reverted to this: foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}</a></li>"; } ?> This produces County Championship 2023 - 1442 So it's getting the player page ID (1442) but not the title value. As a php newbie I am baffled. Any further thoughts? thanks Link to comment Share on other sites More sharing options...
da² Posted November 7, 2023 Share Posted November 7, 2023 6 minutes ago, JerryDi said: I had tried this but it produced this: County Championship 2023 - 1442->title @JerryDi Looks like you missed something when adding "->title". I suppose you wrote: echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}->title</a></li>"; instead of: echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->title}</a></li>"; Link to comment Share on other sites More sharing options...
JerryDi Posted November 8, 2023 Author Share Posted November 8, 2023 Hi, I have tried both of these options and a few others but none of them work The two you have shown above do not return the page ID, but when I remove ->title, it does return the page ID [1442 being the ID]: County Championship 1995 - 1442 Many thanks J Link to comment Share on other sites More sharing options...
ngrmm Posted November 8, 2023 Share Posted November 8, 2023 @JerryDi could you paste in here the latest version of your code. Link to comment Share on other sites More sharing options...
JerryDi Posted November 8, 2023 Author Share Posted November 8, 2023 2 minutes ago, ngrmm said: @JerryDi could you paste in here the latest version of your code. <?php $playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}"); foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}</a></li>"; } ?> Link to comment Share on other sites More sharing options...
ngrmm Posted November 8, 2023 Share Posted November 8, 2023 as @da² mentioned you missed adding the "->title" $playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}"); foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->title}</a></li>"; } Link to comment Share on other sites More sharing options...
JerryDi Posted November 8, 2023 Author Share Posted November 8, 2023 as per previous post, when I add ->title as per your post above, the page ID then disappears and I get just this: County Championship 1995 A bit baffling.... Link to comment Share on other sites More sharing options...
ngrmm Posted November 8, 2023 Share Posted November 8, 2023 Then maybe winner_name is a multipagefield and you try this: $playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}"); foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->first->title}</a></li>"; } Link to comment Share on other sites More sharing options...
da² Posted November 8, 2023 Share Posted November 8, 2023 Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhh yeah I understand, we are all forgetting something tiny but quite important: the property to use on User is "name" instead of "title". ? $playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}"); foreach($playerChampionships as $championship) { echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->name}</a></li>"; } Property "winner_name" should be renamed to "winner" since it's a User instance and not a string. ? 1 Link to comment Share on other sites More sharing options...
ngrmm Posted November 8, 2023 Share Posted November 8, 2023 @da² yes, but user IDs have normally only two digits Link to comment Share on other sites More sharing options...
da² Posted November 8, 2023 Share Posted November 8, 2023 16 minutes ago, ngrmm said: yes, but user IDs have normally only two digits No, how to store thousands of users on 2 digits ? ? Link to comment Share on other sites More sharing options...
ngrmm Posted November 8, 2023 Share Posted November 8, 2023 Just now, da² said: No, how to store thousands of users on 2 digits ? ? that's right ? 1 Link to comment Share on other sites More sharing options...
JerryDi Posted November 8, 2023 Author Share Posted November 8, 2023 Unfortunately using name does not work Is the issue here that this string is trying to select from a different template than the primary find? Link to comment Share on other sites More sharing options...
da² Posted November 8, 2023 Share Posted November 8, 2023 On 11/1/2023 at 2:10 PM, JerryDi said: I have ~1000 golfer names using the player template. Reading that I understand you are not using the User template, isn't it? So the property is still "title" and not "name". Could you post 2 screenshots from PW admin? One of the "player" template properties (the green bars), and one of the "player" page with ID 1442. 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