how can I create a filter using <?php echo $record['createdBy.fullname'];?>

4 posts by 2 authors in: Forums > CMS Builder
Last Post: July 15, 2014   (RSS)

By willydoit - July 14, 2014

Hi all,

This issue may be due to going down completely the wrong road to resolve another issue I have posted in the forum but, I have a list of blogs being displayed and I can assign a creating author to each blog by using <?php echo $record['createdBy.fullname'];?> which appears to take the authors Name from the accounts table which is just what we want.

However I also want to be able to allow viewers to filter the blogs by author and have a search form as below which I had hoped would work using the same variable but it doesnt work, when included on the page it stops anything after the filter form from loading.

If someone could advise whether 

a) using the <?php echo $record['createdBy.fullname'];?> to display an author name is the correct way to do it - and

b) If it is the correct way, how should I incorporate it into my filter.

Thanks in advance for any help provided, filter code provided below.

<form method="post" action="blog_list.php">


<h3>&nbsp;Filter by Author</h3>

<select name="createdBy.fullname">
<?php foreach (getListOptions('bridlington_blogs', 'createdBy.fullname') as $value => $label): ?>

<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['createdBy.fullname']);?>>

<?php echo $label; ?></option>

<?php endforeach ?>



</select>
<br/>


<div align="center">
<input type="submit" name="search" value=" Show Blogs "/>
</div>



</form>

By claire - July 15, 2014

Hi Willy

The createdBy.fullname is a field that's added by the getRecords function. It doesn't appear natively in the table in question, which is why you're getting an error. The field that it references is usually called 'createdByUserNum' and it links to the user's ID number.

There's nothing native to CMSBuilder to do this, unfortunately. There's a few different ways to do it and they'd all be custom. The best way is to use a custom SQL query to get the authors' ID numbers and names, and use that to create the select dropdown - so, something like this:

<?php
$query = "SELECT a.num, a.fullname FROM {$TABLE_PREFIX}accounts a JOIN {$TABLE_PREFIX}bridlington_blogs b ON a.num = b.createdByUserNum";
$accountRecords = mysql_select_query($query);
?>

...

<select name="authorNum">
<?php foreach ($accountRecords as $thisAccount): ?>
<option value = "<?php echo $thisAccount['num'];?>" <?php selectedIf($thisAccount['num'], @$_REQUEST['authorNum']);?>>
<?php echo $thisAccount['fullname']; ?></option>
<?php endforeach ?>
</select>

This is a more complex SQL query than you're used to seeing, but basically what it does is check two tables, accounts and bridlington_blogs, and gives you an array of records (user ID and user name) for user IDs that appear in both. (Here's a W3C schools article for more info on how Joins work: http://www.w3schools.com/sql/sql_join.asp)

You'll likely need to modify your $_REQUEST variable too - I've added authorNum here instead of createdBy.fullname. Let me know if you still have issues with it.

--------------------

Claire Ryan
interactivetools.com

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

By willydoit - July 15, 2014

Hi Claire,

I think that what I want to achieve is quite simple and straightforward, given that the answer demands something quite complex it makes me think that I probably arent doing the right things to get what I want in the first place. All I want to do is to have a field within the blogs records which auto populates with the authors name which is contained in the accounts record. You showed me before how to create a drop down list of users taken from a different table but I dont want a list to chose from in this instance, I want the author_ id field to auto populate with the name of the person creating the blog, it would be easy to require the author to input their name manually but that would be a bit amateurish and could result in misspellings which would affect the filters I want to employ.

I do have another question in the forum which preceded this, perhaps a response to that will nullify this issue.

The reason I want to have this information is because I want to be able to provide a drop down list on the viewer page which will allow a viewer to chose from all the contributing authors and be taken to a page which contains all the blogs by that user. It's a pity that you dont have a pre-created template for a multi-user blog facility.

Thanks In advance.