advanced sql select statement help

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2011   (RSS)

By rez - March 7, 2011

Hi, i am in a situation where i have to simply display some of cmsb data with a select statement.

I have a name, alcohol, available (yes or no), and a brewer. The brewer is a list in another editor.

I use:
$Driver = new Driver_Mysql($link, "SELECT brewer, name, alc FROM cms_draft_list WHERE available = '1'",

It works fine but unfortunately, the brewer is just showing a number instead of the label.

I have tried JOIN but instructions I found, were pertaining to IDs matching like:

$Driver = new Driver_Mysql($link, "SELECT cms_draft_list.name, cms_draft_list.alc, cms_brewer.brewer FROM cms_draft_list INNER JOIN cms_brewer ON cms_draft_list.num=cms_brewer.num",

that doesnt come out correct (i can see why).

Is there an easy way to display the label instead of a number? I also need to ASC by brewer. Currently, when I do this in the top of the page, it is of course sorting the brewer number and not the label. So when the above is all right, I need to know how to sort by brewer.

thanks

Re: [rez] advanced sql select statement help

By Jason - March 7, 2011

Hi,

From the look of your code, it looks like you're trying to connect directly to your MySQL database. CMS Builder can handle that for you. Also, if you're using version 2.04 or above, you can use the :label pseudo field to get the name of the brewer. To do this, you need to use the getRecords function.

for example:

list($draftListRecords, $draftListMetaData) = getRecords(array(
'tableName' => 'draft_list',
'where' => "available = 1",
'allowSearch' => false,
'orderBy' => 'brewer ASC',
));


After this code, you'll have all your records stored in $draftListRecords.

You can output your brewer names like this:

foreach($draftListRecords as $record) {
echo $record['brewer:label']. "<br/>";
}


Hope that helps get you started.
---------------------------------------------------
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: [rez] advanced sql select statement help

By Jason - March 7, 2011

Hi,

Glad that's working for you now.

Yes, you can use CMSB functions in this page so long as you are requiring viewer_functions.php

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/