Jump to content
Rob(AU)

solved - how to save $pages array for later use

Recommended Posts

I am building a portfolio site based on PW 3.0.124 and learning php as I go. The site uses various tags on the pages for each image. The tagging is sorted and clicking a tag link brings up a page of links to images with the same tag using the following code:

$searchTxt = $page->searchTag;
$thisTag = $page->name; // Current clicked tag
$tagList = $pages->find("$searchTxt=$thisTag");  // select all pages where page field "Tags" contains a page with title of our tag

I have a Prev / Next section on the image pages, but it relates to the tree location, rather than the tagList page. 

My guess is that the $tagList array needs to somehow be saved to be used to iterate through it, possibly using sessions ... but I can't figure out how to do that.

Any help/pointers would be appreciated.

Share this post


Link to post
Share on other sites

Thank you horst, that solved an issue I was yet to solve. dragan, your clue was too cryptic for my limited knowledge.

PW is certainly easy to work with if you are prepared invest some thought.

My issue is not solved, but that's because I didn't describe it clearly enough.

The page with the thumbnails (now paginated!) lists all the images with the selected tag. It is based on my tagging template. Clicking a thumbnail opens a page based on my portfolio template for the individual image, each of which has it's own page.

What I would like to do is have the prev/next links on the individual, portfolio template styled page move through the images that were on the tagging page. This will change depending on the selected tag, and will be different to where the images sit within the content tree.

How would this work in PW? I have a sense that it involves sessions ... but have not been able to work it out.

Share this post


Link to post
Share on other sites

I have found a solution and thought I would share it for the benefit of others. It may not be the most efficient or elegant solution but it is working.

After some trial and error the solutions suggested in topic 7659, especially by formulate, began to make sense.

In the tagging.php template file is the following code:

$fullList = $pages->find("$searchTxt=$thisTag");           // this select all pages where page field "Tags" contains a page with title of our tag
$tagList = $pages->find("$searchTxt=$thisTag, limit=10");  // this selects the same as above but limits the rsults for pagination
$session->taggedPages=(string)$fullList;                   // save the result of the first search for use in the portfolio template

In the portfiolio.php template file the following gets the prev / next pages from the tag search:

     $fullList=$pages->find("id=" .$session->taggedPages);
     $next = $page->next($fullList);
     $prev = $page->prev($fullList);

This means that going to the prev / next page is not limited to those paginated on the tagging page.

I hope that makes sense and helps someone else.

  • Like 1

Share this post


Link to post
Share on other sites

@Rob(AU), glad to hear you've got something working. Something to think about though: seeing as each portfolio page may have multiple tags, what happens if a user opens a portfolio page in a new tab via "tag=foo" and then the same portfolio page in a different tab via "tag=bar"? You only have a single session variable for the tagged pages so these will overwrite each other, and the portfolio page doesn't "know" which tag it is being viewed under.

I think it would be better not to use session but to pass the relevant tag as a parameter in the URL. So you would write link URLs to portfolio pages under tag "foo" like "/path/to/page/?tag=foo" and under tag "bar" they would be "/path/to/page/?tag=bar".

Then in your portfolio template you would have some code like this:

// Initialise some null pages as fallback
$prev_page = new NullPage();
$next_page = new NullPage();

// Get the tag from $input if any
$tag = $input->get->text('tag');
// If there is a tag
if($tag) {
	// Get the IDs of portfolio pages with this tag - assumes a tag field named "tag"
	// It's more efficient to just get the IDs rather than the full Page objects, especially if there are a lot of portfolio pages
	$tagged_ids = $pages->findIDs("template=portfolio, tag=$tag");
	// Get the position (key) of this current page in the array
	$page_position = array_search($page->id, $tagged_ids);
	// Get the previous page if any
	if(isset($tagged_ids[$page_position - 1])) {
		$prev_page = $pages($tagged_ids[$page_position - 1]);
	}
	// Get the next page if any
	if(isset($tagged_ids[$page_position + 1])) {
		$next_page = $pages($tagged_ids[$page_position + 1]);
	}
}

And where you want to output the prev/next links:

<?php if($prev_page->id): ?>
	<p><a href="<?= $prev_page->url ?>?tag=<?= $tag ?>">Prev page</a></p>
<?php endif; ?>
<?php if($next_page->id): ?>
	<p><a href="<?= $next_page->url ?>?tag=<?= $tag ?>">Next page</a></p>
<?php endif; ?>

This way each portfolio page knows the tag it is being viewed under, and it means you can do things like share a link to a portfolio page such that the tag is specified.

  • Like 4

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...