Jump to content

Recommended Posts

Posted

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

 

Posted

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) {
	// …
}
Posted

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

Posted

I guess, it's this typo

winner_name=§page->id'

Should be winner_name=$page->id'
You could also try winner_name=$page'

Posted

thanks, I tried both of these but they both produce an error:

Error: Exception: Unrecognized operator: $ (in wire/core/Selectors.php line 416)

Posted

try this

$playerChampionships = $pages->find("template=championship_results, winner_name=$page->id");

// or 

$playerChampionships = $pages->find("template=championship_results, winner_name={$page->id}");


 

  • Like 1
Posted

thank you! This now produces the ID numbers for each championship. How would I change that to give the championship page titles as  URLs?

Posted

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

  • Like 2
Posted

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

Posted
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 

 

Posted

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

 

Posted
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>";

 

Posted

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

 

Posted
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>";

}
?>

Posted

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>";
}

 

Posted

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

Posted

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>";
}

 

Posted

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

  • Like 1
Posted
16 minutes ago, ngrmm said:

yes, but user IDs have normally only two digits

No, how to store thousands of users on 2 digits ? ?

image.png.7c4635922bfefd9b04a6567d0e9ea188.png

Posted

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?

 

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

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
  • Recently Browsing   0 members

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