Tags and permalinks

13 posts by 2 authors in: Forums > CMS Builder
Last Post: April 2, 2015   (RSS)

By Jesus - March 26, 2015

Hello everyone,

I'm trying to implement a tag solution for my posts and found a few threads on the forums but still having issues while trying to implement it with permalinks.

Here's what I'm trying to do:

Based on this post: http://www.interactivetools.com/forum/forum-posts.php?postNum=2232085#post2232085, I created a text field on my viajes table where I'm putting tags values comma separated. Then on my details page I'm loading all viajes records as well as the single record in order to get the proper links.

  // load record from 'viajes'
  list($viajesRecords, $viajesMetaData) = getRecords(array(
    'tableName'   => 'viajes',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
    
  // load records from 'viajesALL'
  list($viajesallRecords, $viajesallMetaData) = getRecords(array(
    'tableName'   => 'viajes',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

On my HTML to display the tags I'm using this code:

            <div class="widget sidebar-widget tag-cloud" id="Tag-Cloud">
            <br/><br/>
              <h4>Etiquetas Populares</h4>

<?php $tagList = explode(',', $viajesallRecord['tags']); ?><?php sort($tagList); ?>
<?php foreach ($tagList as $tagLink): ?>
<a href="<?php echo $record['_link']; ?>?tags_keyword=<?php echo urlencode(trim($tagLink))"><?php echo $tagLink ?></a>
<?php endforeach ?>              
          
            </div>              


I'm getting a blank page and this error message:

E_PARSE: syntax error, unexpected '"', expecting ',' or ';'
/chroot/path/html/viajes/viajes-detail.php (line 489)
http://mywebsite.com/cms/plugins/permalinks/permalinks_dispatcher.php

I'm sure I'm missing something or I don't know if a solution like this can be implemented using the permalinks plugin.

Thanks for pointing me into the right direction,

Jesus

By Jesus - March 27, 2015

ok, sorry I was not doing this properly...

1st of all, each record has a text field named: tags (txt comma separated).

I'm loading records:

  list($viajesRecords, $viajesMetaData) = getRecords(array(
    'tableName'   => 'viajes',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));

Now, I'm displaying the tags using this code:

<?php foreach ($viajesRecords as $record): ?>
    <?php $tagList = explode(',', $viajesRecord['tags']); ?><?php sort($tagList); ?>
        <?php foreach ($tagList as $tagLink): ?>
            <?php echo $tagLink ?>
        <?php endforeach ?>
<?php endforeach ?>

Until here, all fine.... now I need to be able to create the tag link for each particular tag.

Example of what I need:

Tags field: hotels,all inclusive, family friendly

So I need to have my tags generated on the fly like this

for hotels tag: http://mydomain.com/hotels/tags/hotels/

for all inclusive tag:http://mydomain.com/hotels/tags/all-inclusive/

for family friendly tas: http://mydomain.com/hotels/tags/family-friendly/

and so on...

The thing here its that I don't know how many and which tags I'll use and I need those tag links to being generated on the fly for each particular case. Also I need to be able to filter all my records by each specific tag (when selected each tag link) in order to display just the proper records for each tag).

Thanks in advance for pointing me to the right direction,

Jesus

By Jesus - March 30, 2015

I'm getting close on this....

ok, I created a new php file named tags.php and I'm trying to get the content filter by tag.

So, on my details page I've this code:

<?php foreach ($viajesRecords as $record): ?>
    <?php $tagList = explode(',', $viajesRecord['tags']); ?><?php sort($tagList); ?>
        <?php foreach ($tagList as $tagLink): ?>
            <a href="tags.php?tags_keyword=<?php echo $tagLink ?>"><?php echo $tagLink ?></a>
        <?php endforeach ?>
<?php endforeach ?>

My tags field, contains keywords comma separated, so I'm displaying for this particular case 3 tags on this record (Cancun,All Inclusive,Family)

Now, my url looks like this: http://mydomainname.com/viajes/tags.php?tags_keyword=Cancun

On my tags.php page I've this code:

<?php
  
  // load viewer library
  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/chroot/mypath/html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
?>

<script LANGUAGE="JavaScript">
<!--
var tags_keyword =document.location.search.replace('?tags_keyword=', '');
document.getElementById("tagkeyword").value= tags_keyword;
//-->
</script>

<?php
  // load records from 'viajes'
  list($viajesRecords, $viajesMetaData) = getRecords(array(
    'tableName'   => 'viajes',
    'perPage'     => '6',
    'loadUploads' => true,
    'allowSearch' => false,
    'where'          => "tags='tagkeyword'",
  ));

?>

This way I'm passing the variable from one page to another and now I think I can do the filter. However, my tags field contains 3 words (comma separated as mention it before).

Am I'm heading to the right direction? Thanks in advance for your help!

Jesus

By gregThomas - April 1, 2015

Hi Jesus,

I think you're heading in the right direction. Assuming your tag field contained some text that is always structured like this:

"Cancun, Jamaica, Cuba, Chile"

If the URL you wanted to use to match tags on looked like this:

http://mydomainname.com/viajes/tags.php?tags_keyword=Cancun

Then the code you'd need to search would need to look something like this:

<?php

//Set required variables first.
$where      = '';
$tagKeyword = mysql_real_escape_string(@$_REQUEST['tags_keyword']);

//If a tag was passed in the URL
if($tagKeyword){
//Create where statement.
$where = "(`tags` LIKE '% $tagKeyword,%' OR `tags` LIKE '%, $tagKeyword' OR `tags` LIKE '$tagKeyword, %')";

}else{
diewith404('No tag passed in URL.');
}


// load records from 'viajes'
list($viajesRecords, $viajesMetaData) = getRecords(array(
'tableName' => 'viajes',
'perPage' => '6',
'loadUploads' => true,
'allowSearch' => false,
'where' => "$where",
));


?>

So the above code will create a where statement that will search the tags string for any matching keyword. The where statement is broken up into 3 searches like that to ensure we don't return partial matches (eg so searching for the tag "car" would not return carbon, carburetor, cards, etc).

Let me know if you have an questions.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Jesus - April 1, 2015

Hi Greg,

No luck while trying to implement it. Here's the code I'm using now:

<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/path.com/html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

//Set required variables first.
$where      = '';
$tagKeyword = mysql_real_escape_string(@$_REQUEST['tags_keyword']);

//If a tag was passed in the URL
if($tagKeyword){
//Create where statement.
$where = "(`tags` LIKE '% $tagKeyword,%' OR `tags` LIKE '%, $tagKeyword' OR `tags` LIKE '$tagKeyword, %')";

}else{
diewith404('No tag passed in URL.');
}


// load records from 'viajes'
list($viajesRecords, $viajesMetaData) = getRecords(array(
'tableName' => 'viajes',
'perPage' => '6',
'loadUploads' => true,
'allowSearch' => false,
'where' => "$where",
));


?>

and a couple of links for you to check:

Displaying the tags here: http://mundoregioviajes.com/viajes/idea-etiquetas.php?5

and Tag page here: http://mundoregioviajes.com/viajes/tags.php?tags_keyword=Cancun

I'm not getting any results and I should get at least 1 at this point (or with this example)

Thanks for your help!

By gregThomas - April 1, 2015

Hi Jesus,

I'm wondering if the issue is that structure of the tags is different to what my where statement is assuming. Could you copy the text that's in your tag field into a post for me?

Greg Thomas







PHP Programmer - interactivetools.com

By Jesus - April 1, 2015

Sure, here's the text:

Cancun,AMResorts,Todo Incluido

ans attached a screehshot.

By gregThomas - April 1, 2015

Hey Jesus, 

I think the problem is that my code assumes that there will always be a comma and space between tags. But your tags have no space between them. 

To resolve this you can either have your tags like this:

Cancun, AMResorts, Todo Incluido

Or you could modify the where statement in the previous example so it doesn't look for spaces.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - April 1, 2015

Hey, 

The easiest way around that is to explode your tags on ', ' instead of ',' so if you update your code to this:

<?php $tagList = explode(', ', $viajesallRecord['tags']); ?><?php sort($tagList); ?>
<?php foreach ($tagList as $tagLink): ?>
  <a href="<?php echo $record['_link']; ?>?tags_keyword=<?php echo urlencode(trim($tagLink))"><?php echo $tagLink ?></a>
<?php endforeach ?> 

The issue should hopefully be resolved, as it will remove those extra spaces as part of the explode function. 

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com