Jump to content

Where & how to incorporate $sanitizer->selectorValue($title) in a foreach loop?


hollyvalero
 Share

Recommended Posts

What I am trying to do: On a blog post page, I am adding a small snippet at the bottom that has the 2 most recent blog posts by the current page author, excluding the blog post title that we are actively reading. That part works...

// if this page has an author listed, s/he becomes the active $author and this page is the $activepost
if ($page->cbpage2->id) {
     $author = $page->cbpage2;
     $activepost = $page->title;

// Get the posts...
 $articles = $pages->find("template=post, cbpage2=$author, limit=2, title!=$activepost, sort=-cbdate" );

// and then format the results for each found...
    foreach ($articles as $article) {
          echo "...";
          }

 

One author post had a comma in the blog post title which broke this, and I assume this needs to be santized because I reference title!=$activepost
this didn't work...

     $articles = $pages->find("template=post, cbpage2=$author, limit=4, title!=$activepost, sort=-cbdate" . $sanitizer->selectorValue($title));
 

I assume I have to sanitize BEFORE: $pages->find  since it's dragging in the comma, but since I have the $activepost = $page->title; No method I've tried seems to be working. Rather than tell everyone "don't use commas!" I'd love to figure this out, but coming up with tragic results like: 

     $dirtyarticles = $pages->find("template=post, cbpage2=$author, limit=4, title!=$activepost, sort=-cbdate")
     $cleanarticles = $sanitizer->selectorValue($dirtyarticles));

and that sure isn't right... 

 🤪

Link to comment
Share on other sites

I do not understand what you were trying in your examples -- I think maybe you are muddled about how $sanitizer works?  All it does it return the sanitized value.

All you need to do is this:

$activepost = $sanitizer->selectorValue($page->title);

$articles = $pages->find("template=post, cbpage2=$author, limit=2, title!=$activepost, sort=-cbdate" );

/**
 * OR: in case you need to use it unsanitized elsewhere
 */
$activepost = $page->title;

// note the position of the sanitizer method
$articles = $pages->find("template=post, cbpage2=$author, limit=2, title!=" . $sanitizer->selectorValue($activepost) . ", sort=-cbdate" );

/**
 * OR (again): if $activepost is only used in the selector, you can ditch it altogether and use $page->title directly
 */
$articles = $pages->find("template=post, cbpage2=$author, limit=2, title!=" . $sanitizer->selectorValue($page->title) . ", sort=-cbdate" );
 */

 

 

  • Like 1
Link to comment
Share on other sites

56 minutes ago, LMD said:

I do not understand what you were trying in your examples -- I think maybe you are muddled about how $sanitizer works?  All it does it return the sanitized value.

** Ah, thank you! I was thinking I had to santize the whole batch separately and then try to match the active post...  I only need to sanitize the active post because that's the part that is in the find->page thing... got it. 

 

 

 

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