Tags and permalinks

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

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 Jesus - April 1, 2015

ok, I made the modification, now my tag field looks like this:

Cancun, AMResorts, Todo Incluido

But the only working tag at this point seems to be Cancun (that tag url looks like this: /tags.php?tags_keyword=Cancun) but the other 2 are inclusing a space on the URL like this: /tags.php?tags_keyword=%20Todo%20Incluido and like this: tags.php?tags_keyword=%20AMResorts

If I manually remove the space right before the = sign then it loads the proper tag

So it seems like on the tag field, tag 1 works fine, but tag 2 and 3 are adding the extra space.

Of course I already tried removing the space on the tag field and that doesn't work.

Thanks for your help!

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