Remove record num from _link

23 posts by 4 authors in: Forums > CMS Builder
Last Post: December 12, 2012   (RSS)

By weblm - October 11, 2010

If I have a multi record section where the Title field has to be unique, is there anyway to not have the -num be appended to the url string?

So I have a page with a bunch of local school listings. Instead of having the url be:

/schooldetail.php?ADAMS-ELEMENTARY-SCHOOL-1

I'd like it to be:
schooldetail.php?ADAMS-ELEMENTARY-SCHOOL

Is this possible? Do I have to manually construct the link? Since I am making the Title field unique, I'm hoping I can use just the Title in the URL.

Thanks.

-Kevin
LM

Re: [kblm] Remove record num from _link

By Chris - October 11, 2010

Hi Kevin,

You'll need to manually construct the link, yes.

One thing to keep in mind is that when CMS Builder generates those URLs, it replaces all characters that are not in the set [a-z0-9._] with hyphens, therefore, (theoretically) many different titles could turn into "ADAMS-ELEMENTARY-SCHOOL".

You can use this code to generate a link like CMS Builder does:

<?php
$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);
$query_string = preg_replace("/(^-+|-+$)/", '', $query_string);
echo $query_string;
?>


Then, on your detail page, you'll need to replace the hyphens with underscores to be able to match any character in their place (but not before escaping your existing underscores:)

$title_like = preg_replace("/_/", '\\_', @$_SERVER['QUERY_STRING']);
$title_like = preg_replace("/-/", '_', $title_like);

list($sampleRecords, $sampleMetaData) = getRecords(array(
'tableName' => 'sample',
'where' => mysql_escapef('title LIKE ?', $title_like),
));
$sampleRecord = @$sampleRecords[0]; // get first record


I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Remove record num from _link

By weblm - October 12, 2010

Chris,

Thanks. This worked.

-Kevin
LM

Re: [kblm] Remove record num from _link

By RGC - October 27, 2010

This worked well on my clients site also. However on the actual detail page, I have a viewer displaying links to other related sub pages - what code do i use to modify the <?php echo $record['_link'] ?> links so it will display the link without the number as well?:

This is the code i have used to display navigation links to the other pages in the about section:
<?php
// load viewer library
$libraryPath = 'cm/lib/viewer_functions.php';
$dirsToCheck = array('/usr2/home/clientsdomain.com/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($aboutRecords, $aboutMetaData) = getRecords(array(
'tableName' => 'about',
));

?>
<?php foreach ($aboutRecords as $record): ?>
| <a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php endforeach; ?>
Rod

Re: [rgc] Remove record num from _link

By Jason - October 27, 2010

Hi,

You would use the same code that Chris posted to just use the title for your link:
<?php
$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);
$query_string = preg_replace("/(^-+|-+$)/", '', $query_string);
?>


The variable $query_string is what you would append to your .php page in the link like this:


<a href="myPage.php?<?php echo $query_string;?>">Link</a>

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Remove record num from _link

By RGC - October 27, 2010

All the navigation links are now appearing the same - http://www.mylcient.com/about.htm?

What needs to be changed or added to get the title field to display after the ?
Here is my complete page code:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);
$query_string = preg_replace("/(^-+|-+$)/", '', $query_string);
echo $query_string;
?>
<?php
// load viewer library
$libraryPath = 'cm/lib/viewer_functions.php';
$dirsToCheck = array('/usr2/home/myclient.com/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($aboutRecords, $aboutMetaData) = getRecords(array(
'tableName' => 'about',
));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body> <?php foreach ($aboutRecords as $record): ?>
| <a href="about.htm?<?php echo $query_string;?>"><?php echo $record['title'] ?></a>
<?php endforeach; ?>
</p>
</body>
</html>
Rod

Re: [Jason] Remove record num from _link

By RGC - October 27, 2010

I appreciate your help Jason....we are almost there.
The correct link is now appearing, however i can not figure out what is causing the page title to be duplicated, first just title no link, then title with the link, looks like this :
Page-One | Page-One Page-Two | Page-Two Page-Three | Page-Three

How do i correct this? Thanks!

Here is my latest body code:
<body><?php foreach ($aboutRecords as $record): ?>
<?php
$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);
$query_string = preg_replace("/(^-+|-+$)/", '', $query_string);
echo $query_string;
?>
| <a href="about.htm?<?php echo $query_string;?>"><?php echo $record['title'] ?></a>
<?php endforeach; ?>

</body>
Rod

Re: [rgc] Remove record num from _link

By Jason - October 27, 2010

Hi,

What's happening is you are echoing $query_string in 2 places, where you only need to be echoing it in one.

Remove the line indicated below:

<body><?php foreach ($aboutRecords as $record): ?>
<?php
$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);
$query_string = preg_replace("/(^-+|-+$)/", '', $query_string);
echo $query_string;
?>
| <a href="about.htm?<?php echo $query_string;?>"><?php echo $record['title'] ?></a>
<?php endforeach; ?>

</body>


Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Remove record num from _link

By RGC - October 27, 2010

That did the trick....Thanks for your help Jason.
Rod