Jump to content

Help needed: Link to or search in multiple fields


Hajosch
 Share

Recommended Posts

Dear PW Experts!

I am a PW beginner and have only little PHP knowledge. Since I haven't found a solution for my problem here in the forum despite intensive searching, it would be nice if I could get support from the experts to solve my problem.

THE INITIAL SITUATION: 

With ProcessWire (3.0.98) I am developing an online bibliography in which certain data fields (e.g. author and editor) are displayed as links in the frontend representation of a title. See screenshot 1: yellow marks the names of the 2 authors, green marks the names of the editor).

dadaweb_3.0_screenshot_01.png.525c15f26f5b8a97a87eec138dc3d1f5.png

For example, if you click on the link of the author's name, all titles of this author will be displayed. The same is true for the name of the editor. For this I have set up the following fields and templates:

1. for the authors: field: author, template: authors

2. for the editors: field: editor, template: editors

The linking of the name and the search for the name is realized by the template file 'publication-page' as follows:

<?php namespace ProcessWire;

/** @var Page $page */
$authors = $page->get('authors');
$editors = $page->get('editors');
[. . .]

<!-- Query of the authors and editors -->
      <?php if($page->authors->count)
            echo "<u>Autor*in/nen</u>:&nbsp";
            echo $page->authors->implode("; ", "<a href='{url}'>{title}</a>"); ?>
      <?php if($page->editors->count)
            echo "<u>Herausgeber*in/nen</u>:&nbsp";
            echo $page->editors->implode("; ", "<a href='{url}'>{title}</a> (ed.)"); ?>  

The separate search for authors and editors by link works without problems. If you click on the author's name, all titles of this author will be displayed. The same works for the names of the editors.

dadaweb_3.0_screenshot_02.png.fcd5694ce66b3ca2e99d1672ad54f00e.pngdadaweb_3.0_screenshot_03.png.8ad6ffd0924aa0b34631826076e71834.png

THE PROBLEM:

So that the user does not have to perform separate searches for authors AND editors, the search for the linked name should be performed simultaneously in 'authors' and 'editors' to display all titles that have been entered under this name either as authors or as editors. So I want a function like the one used in the full text search with a selector definition as follows:
 

// if there are keywords, look in the following fields for the keywords (search terms)
if($input->get('keywords')) {
      $value = $sanitizer->selectorValue($input->get('keywords'));
      $selector .= "title|subtitle|authors|editors|publisher|place|tags|body|contents_table%=$value, ";
      $summary["keywords"] = $sanitizer->entities($value);
      $input->whitelist('keywords', $value);
}

This works in the full text search. All titles with the same name as author and editor are displayed simultaneously.

MY QUESTION:

How do I change the definition of the variables and the definition of the name display of the author and editor so that both the author's name and the editors name generate a link that searches both data fields (authors and editors) simultaneously and displays the corresponding titles for both the author and the editor in a search result?

I tried by changing the variables for authors and editors in the following way:
 

$authors = $page->get('authors|editors');
$editors = $page->get('editors|authors');

But that didn’t work.

I am grateful for every tip.

Thank you and best regards from Potsdam/Germany

Hajosch

Link to comment
Share on other sites

If I understand you correctly you want to display a list of all publications, where the author OR the editor is = the name that you clicked?

For this you would need a selector like

$publications = $pages->find('template=publication, authors|editors%=name_of_the_author');

Please take a look at https://processwire.com/api/selectors/#or-selectors1 how to use OR selectors.

Please also note, that you have to use $pages, instead of $page and "find" instead of "get" to find the correct pages.

$page is the actual page you are on, and queries the fields you mentioned on the actual page.

$pages->find performs a search on all pages that match your selector and returns the result.

You then have to iterate through the result, to display all books/publications of that author/editor.

Link to comment
Share on other sites

@jmartsch

Hello Jens,

thank you for your tips. And YES, I want all publications to be displayed for which the name you click on is either the author or the editor.

In order to implement your suggestions correctly, unfortunately I lack enough PHP knowledge. Specifically, I don't know where to put the "$pages" and where to put the selector recommended by you in the source code, so that the function works as desired.

Could you or someone else, who stumbles over this post, give me the code to install the selector based on my template file? I'll insert the complete source code of the template file "publication-page.php", which I use to display the titles.

Thank you and have a nice Sunday

Hajosch

Here the source code of the template file:

<?php namespace ProcessWire;
/**ABRIDGED SOURCE CODE OF PUBLICATION-PAGE.PHP */
/**for full code ask PW-User: Hajosch */
/** @var Page $page */
$authors = $page->get('authors');
$editors = $page->get('editors');
$subtitle = $page->get('subtitle');
?>
<div class='uk-grid uk-grid-medium'>

	<div class='publication-images uk-width-medium-1-3 uk-text-center'>
		<?php include('./publication-page-images.php'); ?>
	</div>
	
	<div class='uk-width-medium-2-3'>

	<h2><?=$page->title?></h2>
		
		<h3><font color=#dd1111>Bibliografische Darstellung mit Feldkennungen</font></h3>		
		<!-- Query of the authors & editors -->
		<?php if($page->authors->count) 
			echo "<u>Autor*in/nen</u>:&nbsp";
			echo $page->authors->implode("; ", "<a href='{url}'>{title}</a>"); ?>
		<?php if($page->editors->count) 
			echo "<u>Herausgeber*in/nen</u>:&nbsp";
			echo $page->editors->implode("; ", "<a href='{url}'>{title}</a> (Hrsg.)"); ?>		
		<br><u>Titel</u>:&nbsp<?=$page->title?>
		<!-- Feldabfrage mit IF-Abfrage (Feld nur anzeigen, wenn Inhalt) Lösung aus PW-Forum -->
		<?php if($subtitle): 	
			echo "<br><u>Untertitel</u>: $subtitle";
		endif; ?>
		<!-- Ende der Darstellung mit Feldkennungen -->
	
	</div>	
</div>

 

Link to comment
Share on other sites

First of all I want to say that learning PHP is crucial to get going with ProcessWire. You should understand the basic syntax and concepts and how to iterate over items.

Please also read http://processwire.com/docs/tutorials/but-what-if-i-dont-know-how-to-code/

However, you should create a template file "authordetails.php" in site/templates/ directory, then in ProcessWire admin also create a template with the same name (but without the .php suffix).

Then assign this template to each author and editor.

In the authordetails.php file enter the following code to get all publications for this author/editor:

$publications = $pages->find("template=publication, authors|editors={$page->id}");

Please note the double-quotes, that are required for {$page->id} to work!!!

If you are using a more recent version (3.0.107) of ProcessWire you could instead use

$publications = $page->references();

please read http://processwire.com/blog/posts/processwire-3.0.107-core-updates/#page-gt-references about this.

Then iterate over the publications

foreach ($publications as $publication){
echo $publication->title;
}

All this is completely untested and comes directly from my brain, so I hope everything works.

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

On 10/14/2018 at 2:25 PM, jmartsch said:

 

 

Hello Jens,

thanks for the additional tip. You are right. My basic problem is the lack of PHP knowledge. So far, I've managed well with Processwire and other CM systems (Joomla, Wordpress) in using existing code examples when developing my own websites, or I've found an extension that solves the problem. Unfortunately, I haven't found an example of how to solve the specific problem I described here in the PW forum or in the published PW profiles. And also the tips given in your last posting I could not implement successfully.

I would like to assign a programmer with good PW knowledge for the solution of this problem and of course I would like to pay a regular fee. But the website, which I develop with friends, is a non-commercial project (of anarchy and anarchism research), which unfortunately can't count on public financing either. Nevertheless, I would pay a small fee of maximum 50 EUR (via Paypal) for the solution of the problem in order to get on with the development of the project as soon as possible. For this I would give the person who wants to solve the problem access to the development website.

I would be happy if someone could help me with programming and solving my problem. You can contact me via my PW profile.

Thank you and kind regards

Hajosch

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