5 posts by 2 authors in: Forums > CMS Builder
Last Post: July 18, 2017   (RSS)

Hi

1.  My clients wants to be able to edit the story categories, so I have created a section called "Blog Categories".  Within this, they can create new categories as they go.  A story can belong to multiple categories.

2.  In the stories section, the client has a drop down to select which category (or more than one) which the story belongs to.  

3.  The problem:  if I use the below to only list the Adventure Education stories, all the stories appear even though 1/2 aren't marked as Adventure Education.

"where" => "`category` != 'Adventure Education'",

4.  Also, where the story belongs in several categories, is there a way to separate the categories (ie, tags), so there is a divider to some space between them? eg. <?php echo join(', ', $record['category:values']); ?>   produces a run on string of words without any real spacing between them.

Thanks in advance for your help!

By Dave - July 5, 2017

Hi degreesnorth, 

For front end views you may actually be able to use the search features like this to automatically list the category you want: 
https://www.interactivetools.com/docs/cmsbuilder/viewer_search.html

For example: yourViewer.php?category=Adventure Education

But if you need to do it with MySQL this should match a single category: 

"where" => " `category` = 'Adventure Education' ",

Or if you're searching a multi-value pulldown then the values are separated by tabs, so you can use this: 

"where" => " `category` LIKE '%\tAdventure Education'\t%",

% means "any characters and \t means tab.  So it matches your keyword surrounded by tabs anywhere in the string.

And for displaying the list of categories, your code should work.  what output do you see when you try <?php showme($record['category:values']); ?> or even <?php showme($record); ?>

Hope that helps!  Let me know what you find out.  Thanks!

Dave Edis - Senior Developer
interactivetools.com

Hi Dave

Thanks for responding.  

Unfortunately, I am getting an error of 

(when I use " marks)

// load records from 'blogs'
list($blogsRecords, $blogsMetaData) = getRecords(array(
'tableName' => 'blogs',
'loadUploads' => true,
"where" => " `category` LIKE '%\tAdventure education'\t%",
));

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%) AND `blogs`.hidden = 0 ORDER BY dragSortOrder DESC' 

and when I change it to single ' (quote marks)

// load records from 'blogs'
list($blogsRecords, $blogsMetaData) = getRecords(array(
'tableName' => 'blogs',
'loadUploads' => true,
'where' => ' 'category' LIKE '%\tAdventure education'\t%',
));

Parse error: syntax error, unexpected 'Education' (T_STRING), expecting ')' 

Any other suggestions?

Thanks in advance

PS.  Also, I I want to add the blog categories to a page (so they are clickable, how do I use the <a href= ???> to make it go only to, for example, adventure education?

<?php foreach ($blog_categoriesRecords2 as $record): ?>
<ul class="list list-border angle-double-right">
<li><a href="#"><?php echo htmlencode($record['title']) ?></a></li>
</ul>
<?php endforeach ?>

By Dave - July 18, 2017

Hi degreesnorth, 

On the first issue.  I find adding spaces can help a lot.  Basically you just need to use different quotes on the inside of the string and can't quote the fieldname (or have to use `backticks` to do that).  So instead of this: 

'where' => ' 'category' LIKE '%\tAdventure education'\t%', 

Try this: 

'where' => " `category` LIKE '%\tAdventure education\t%' ", 

The first one is comparing against the text 'category', the second one is comparing against the MySQL database column 'category'.

And for the link, the first step would be printing out the category, but your field may have multiple, so you'd need to decide what you want to do there.  In general, it's something like this: 

<li><a href="yourpage.php?category=<?php echo urlencode($record['category']); ?>"><?php echo htmlencode($record['title']) ?></a></li>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com