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

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

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

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