Best way to display default of multi record section

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

LM

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

LM

By weblm - April 18, 2013

Greg,

You rock (again).  Thanks so much.  I was trying to figure out the best way to do this but couldn't.  This works perfectly and simplifies the code.

Only thing I changed was to use the variable $_SERVER['QUERY_STRING'] instead of $_GET.  

$whereString = (empty($_SERVER['QUERY_STRING']))? '' : " TRIM(title) LIKE '".mysql_escape($title_like)."'" ;

Not sure why.....you can tell me if you know of any difference between the two.  Thought I read somewhere where the $_SERVER['QUERY_STRING'] method was better.  Can't find where now.

Thanks again!

-Kevin

LM

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

LM