Remove record num from _link

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

By weblm - November 26, 2012

Resurrecting this old thread because I've run into an issue that I'm wondering if it can be solved.

This code works fine.....as long as the record title doesn't end in a number.

I'm running across this issue with some real estate clients. We might have a record like: 1 Main Street #3

In this case, the code fails, I'm guessing because now it's looking for a number.

Any ideas if there is a workaround?

Thanks!

-Kevin
LM

Re: [kblm] Remove record num from _link

By Chris - November 26, 2012

Hi kblm,

What code exactly are you using at the top of your page, and can you provide the example URL that is being generated for "1 Main Street #3" and failing to find the record?

Thanks,
Chris
All the best,
Chris

By weblm - November 26, 2012

Can't give a URL right now because I'm on a protected server.

I can give you my details.

I have 2 pages, a listings page and a detail page.

On the listings page, within the foreach loop I create a link as follows:

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


I then for each record create the manual URL like this:

<a href="/home-listings-detail.php?<?php echo $propertyLink ?>">LINK</a>

For a record that has a property address of 1234 Main Street, I get the nice URL of this: /home-listings-detail.php?1234-Main-Street

On the detail page, at the top I have the following:

<?php

//CODE USED TO REMOVE THE RECORD num from the URL
$title_like = preg_replace("/_/", '\\_', @$_SERVER['QUERY_STRING']);
$title_like = preg_replace("/-/", '_', $title_like);

// load records
list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'where' => mysql_escapef('address LIKE ?', $title_like),
'limit' => '1',
));
$listingsRecord = @$listingsRecords[0]; // get first record

// show error message if no matching record is found
if (!$listingsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

?>


For that test URL of /home-listings-detail.php?1234-Main-Street, the code works fine. It's loads the detail page.

However, if I change the property address to 1234 Main Street #3, the url it wants to go to is: /home-listings-detail.php?1234-Main-Street-3

For the detail page I get the error thrown from the CMS: Record not found!


Hope that helps.....let me know.

-Kevin
LM

Re: [kblm] Remove record num from _link

By Chris - November 26, 2012

Hi Kevin,

Thanks for the details! I think what's happening here is that the adjacent space and number sign (" #") in "1234 Main Street #3" are getting turned into a single hyphen ("-") in the link, and that's being turned into a LIKE expression which is only looking for one character there (e.g. LIKE "1234_Main_Street_3").

I think the simplest solution here is to not truncate adjacent hyphens into one. Wherever you're generating links, remove the plus sign from this regular expression:

$query_string = preg_replace('/[^a-z0-9\.\-\_]+/i', '-', $record['title']);

It should look like this instead:

$query_string = preg_replace('/[^a-z0-9\.\-\_]/i', '-', $record['title']);

That should cause your link to have an extra hyphen in it, like this: /home-listings-detail.php?1234-Main-Street--3

Does that help?
All the best,
Chris

By weblm - November 26, 2012

I'll have to test some more but right now it looks to be working. Thanks Chris!!

-Kevin
LM

Re: [kblm] Remove record num from _link

By weblm - December 11, 2012

Hey Chris,

Your code is working great. I do have one more question.....is there anyway to have the code strip out or ignore trailing spaces in the title field? We have some clients that inadvertently space an extra space at the end....this causes this link method to not find the record.

Can this be done in the preg_replace.....or is there a way in the admin to strip out any trailing spaces?

Thanks!

-Kevin
LM

Re: [kblm] Remove record num from _link

By weblm - December 12, 2012

Jason thanks.

Technically that does help. Forgot I could just trim the string. However, I think I'm thinking of this incorrectly, because even when I trim the string, the CMS still doesn't find the record. I'm guessing because the record itself HAS the spaces, therefore the LIKE isn't matching?

I guess, can someone explain to be exactly what these preg_replace statements are doing? Maybe that will help me understand how to tackle this.


These are on the list page to form the link:

$propertyLink = preg_replace('/[^a-z0-9\.\-\_]/i', '-', $record['property_name']);
$propertyLink = preg_replace("/(^-+|-+$)/", '', $propertyLink);


These are on the detail page:

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


Thanks for any help.

-Kevin
LM

Re: [kblm] Remove record num from _link

By Jason - December 12, 2012

Hi Kevin,

To get a better idea of exactly what's happening, try putting a debugSql option in your getRecords call:

// load records
list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'where' => mysql_escapef('address LIKE ?', $title_like),
'limit' => '1',
'debugSql' => '1',
));


This will print out the exact query being executed. If you could post this along with the exact value of the address field in the record you are expecting to retrieve. That should point us in the right direction.

Thanks
---------------------------------------------------
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: [kblm] Remove record num from _link

By weblm - December 12, 2012

Jason,

Ok, with the trim NOT in the script....and the record having NO trailing spaces in the Title in the CMS.....this is the resulting query (which works):

SELECT SQL_CALC_FOUND_ROWS `property_listings`.*
FROM `cms_property_listings` as `property_listings`
WHERE (property_name LIKE 'Acorn_Lodge')
ORDER BY SUBSTRING_INDEX(property_name,'The ',-1)
LIMIT 1
tableName = 'property_listings' AND
fieldName IN ('photos') AND
recordNum IN (4)
ORDER BY `order`, num
SELECT SQL_CALC_FOUND_ROWS `accounts`.*
FROM `cms_accounts` as `accounts`
WHERE (`num` IN (1))
ORDER BY fullname, username


Now, if I just add a space to the end of the record title in the CMS....I then get this query:

SELECT SQL_CALC_FOUND_ROWS `property_listings`.*
FROM `cms_property_listings` as `property_listings`
WHERE (property_name LIKE 'Acorn_Lodge')
ORDER BY SUBSTRING_INDEX(property_name,'The ',-1)
LIMIT 1
Warning: Cannot modify header information - headers already sent by (output started at /home/lmdianet/public_html/mmp/cmsAdmin/lib/viewer_functions.php:411) in /home/lmdianet/public_html/mmp/cmsAdmin/lib/common.php on line 1063 Record not found!


Looks to me like something IS trimming this already, even though I haven't added the TRIM to the link.

-Kevin
LM