7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 19, 2013   (RSS)

By weblm - April 16, 2013

I want to use a multi record section, but I don't want a list page and a display page.  Basically I want the first record to show if no param is called in the URL.

I know this works by default, but I'm also using the following code to not have the record number on the end of the url:

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

Then in the SQL, I use:

// load records
list($site_programsRecords, $site_programsMetaData) = getRecords(array(
'tableName' => 'site_programs',
'where' => " TRIM(title) LIKE '".mysql_escape($title_like)."'",
'limit' => '1',
));

This works fine in setups where we have a list page and then a separate URL for the detail page.

The only way I could think to do this was to wrap the SQL in an if and check if there are any _GET variables like this:

if(empty($_GET)) {
// load records
list($site_programsRecords, $site_programsMetaData) = getRecords(array(
'tableName' => 'site_programs',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));

} else {
// load records
list($site_programsRecords, $site_programsMetaData) = getRecords(array(
'tableName' => 'site_programs',
'where' => " TRIM(title) LIKE '".mysql_escape($title_like)."'",
'limit' => '1',
));

}

Is this the best wat to do this?

-Kevin

By rconring - April 16, 2013 - edited: April 16, 2013

Use this code without the where clause

ist($site_programsRecords, $site_programsMetaData) = getRecords(array(
 'tableName' => 'site_programs',
 'limit' => '1',
 ));
 $site_programsRecord = @$site_programsRecords[0]; // get first record

This will get the first record.   Hope I interpreted your question correctly.

Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

By weblm - April 17, 2013

Ron,

That line is being used by default....sorry for not including it in my question.  The problem is I need the where clause because this is a multi record section, and if we are calling another page, we need to find it.

The default code works fine, but I don't want to use that because I'm using the special code to strip out the record id on the end of the urls.

-Kevin

Hi Kevin,

What about doing something like this to reduce the amount of code used:

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

//Get appropriate where string
$whereString = (empty($_GET))? '' : " TRIM(title) LIKE '".mysql_escape($title_like)."'" ;



// load records
list($site_programsRecords, $site_programsMetaData) = getRecords(array(
  'tableName' => 'site_programs',
  'where' => $whereString,
  'limit' => '1',
));

So the $whereString variable will equal either an empty string if there is no $_GET array (I changed this from the whereRecordNumberInUrl function, as this would have returned '1'), or it will equal the title search string if there is data in the URL. Then the $whereString is passed into the getRecords function.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas

PHP Programmer - interactivetools.com

Hi Kevin,

$_GET will return an array of all of the items in the URL string, for example if you have a url string that looks like this:

example.com/scratch.php?animal=swan&type=bird&feature%5B%5D=wings&feature%5B%5D=webbed+Feet&submitForm=yes

$_GET would return an array of items:

  Array
(
    [animal] => swan
    [type] => bird
    [feature] => Array
        (
            [0] => wings
            [1] => webbed Feet
        )

    [submitForm] => yes
)

$_SERVER['QUERY_STRING'] will return just the string in the URL:

animal=swan&type=bird&feature%5B%5D=wings&feature%5B%5D=webbed+Feet&submitForm=yes

Using $_SERVER['QUERY_STRING'] is probably slightly quicker as PHP doesn't have to process the items into an array.

Thanks!

Greg

Greg Thomas

PHP Programmer - interactivetools.com

By weblm - April 19, 2013

Good to know....thanks for the info!

-Kevin