Variable in WHERE clause

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 15, 2012   (RSS)

By terryally - May 14, 2012

Hi,

I am trying to achieve the following to build a page submenu (different for each page). I use a multi-record section editor that contains the names and links and each record also contains the field "which_page" which defines the page name and extension. So based on which page is currently displayed, I want the respective sub-menu items to be displayed. I tried defining the current page through a variable first and then using that variable as follows:

$page_name = basename($_SERVER['PHP_SELF']);

list($page_submenuRecords, $page_submenuMetaData) = getRecords(array(
'tableName' => 'page_submenu',
'where' => 'which_page = "$page_name"',
));


It throws the following error which is self-explanatory. So, will this only filter by columns or is there a way to include an external variable?

MySQL Error: Unknown column '$page_name' in 'where clause'

The current workaround that I am using is as follows but I feel that defining the current page in the WHERE clause, if possible, might be more efficient(??).

<?php foreach ($page_submenuRecords as $record): ?>
<?php if(basename($_SERVER['PHP_SELF']) == $record['which_page']):?>
<li id="<?php echo $record['style'] ?>"><a href="<?php echo $record['link'] ?>" title="<?php echo $record['title'] ?>"><?php echo $record['title'] ?></a></li>
<?php endif;?>
<?php endforeach ?>



Thanks
Terry

Re: [terryally] Variable in WHERE clause

By Steve99 - May 14, 2012

Hi Terry,
It looks like you need to wrap the variable so it doesn't see it as comparing a column.

Try this:
$page_name = basename($_SERVER['PHP_SELF']);

list($page_submenuRecords, $page_submenuMetaData) = getRecords(array(
'tableName' => 'page_submenu',
'where' => 'which_page = "'.$page_name.'"',
));


Hope this helps!