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

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

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

By weblm - April 19, 2013

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

-Kevin

LM