Jump to content

Display entries depends on having an id/item in it ...


August
 Share

Recommended Posts

Thank you Ryan for this great open-source Software and - of course, hello to the processwire-community!

I follow some guides here in the Pws forum and have many great impressions of you.
Firstly I started on www.pwtuts.com because it was just convenient to me, indeed. 

Here I using url-segments-and-the-page-reference-field.
Building things up in order to, gave me the reason to ask now for a way, that uses the full processwire API - without amongst things . . .

So, the point here is a code snippet for rendering links in a navigation, depending only on the given IDs (found no better way getting the IDS  by col "data")

 

In the snippet below, which is not part of the tutorial on "pwtuts.com", is my solution getting the relations succesfully:

	<?php
	// get entries depends on having an item in it
	$rows = $db->query("SELECT DISTINCT data FROM field_group WHERE 1");
	while ($row = $rows->fetch_assoc()):
    	$string_rows = $row['data'];
        $data_ip = preg_split('/\s+/', $string_rows);
        foreach ($data_ip as $real):
        	$navItem = $pages->get($real);
            $focusGroup = ( $titleProp == $navItem->title || $navItem->name == $input->urlSegment1 ) ? " active" : "";
	?>
<li class="nav-item">
    <a class="nav-link<?php echo " " . $navItem->name . $focusGroup; ?>" href="<?php echo $homepage->url."example/{$navItem->name}/"; ?>"><?php echo $navItem->title; ?></a>
</li>
	<?php endforeach; ?>
	<?php endwhile; ?>

 

Sure, its not really nice, but it works.  Does anyone have a tip for optimization? Thanks for Suggestions.

 

image.png

Link to comment
Share on other sites

Hi August! I had a bit of a hard time understanding your post, but I think you are trying to get pages with has a page field filled, then, assuming your field is called group:

$selection = $pages->find("group!=''");

It's very rare to use SQL statements in ProcessWire unless you are thriving for extreme optimizations. 

Link to comment
Share on other sites

20 hours ago, elabx said:

$selection = $pages->find("group!=''");

Hi elabx, thank you for spending time on it. This way I tried before, but without success. 

The Question, maybe a bit more precise:

I searched for the existing entries, such as the displayed "data" column (image of the table), where the pages of the "Group" field are assigned, just to get the title once from the Field "field_group".

Link to comment
Share on other sites

If I understand right you want to get all the pages that are selected in a given Page Reference field, site-wide.

To do this efficiently you do need to use a SQL query - Ryan has shared a code snippet:

 

  • Like 2
Link to comment
Share on other sites

Hello August,

that looks to me like you have pages in groups and in your navigation you want to get urls like this:  mysite.com/example/some-group.
If that is your case than it's looks to me like projects where I use "tags" (eg. 1) group by tag, 2) pages list by tag url...).
Or, "groups" are independent pages and "post" pages are grouped using page reference field ( "group", multiselect or single select).

In that case, using "natural" PW API call, you can try this:

$groups = new PageArray();
//NOTE: inside selector write your template(s) name
foreach($pages->find("template=post, group!=null") as $post){   
    $groups->import($post->group); // for single select use (array($post->group)) 
}
// sort groups by title
$groups->sort('title');

Now inside $groups variable is distinct and sorted page array with used groups only (and every group inside is page/object).
Sorry if I don't understand you well, and if this example is not close to your question.

Regards.

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