Filter Records by Author

9 posts by 2 authors in: Forums > CMS Builder
Last Post: November 20, 2014   (RSS)

By claire - November 14, 2014

Hi Perch

Yeah, you can't use that particular code snippet as is. Here's how you can change it to only get distinct values:

<form method="post" action="blog_list.php">
<h3>&nbsp;Filter by Author</h3>
<select name="createdBy.fullname">

<?php $authors = array_unique(getListOptions('bridlington_blogs', 'createdBy.fullname'));
<?php foreach ($authors 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>

This should work, provided that you change the table and variable names to whatever you're using.

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

Claire Ryan
interactivetools.com

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

Hi, Claire -

Thanks for this. Unfortunately, no matter which combination of $tablename and $fieldname I use, the getListOptions() function always throws the same error:

Unknown optionsType ''!

I have no idea what's wrong?!

:0(

Perch

Doh!

Of course, the getListOptions() $fieldname must be a list type field!

OK. Now that we've got that sorted... :o)

Claire, your ammendment to the code doesn't help me much I'm afraid!

:0/

Perch

By claire - November 17, 2014

Hi Perch

Apologies, I assumed that would do the job. Not to worry though - it's still possible to get the right records using a quick MYSQL query. It'll look something like this:

<form method="post" action="blog_list.php">
<h3>&nbsp;Filter by Author</h3>
<select name="authornum">

<?php $authors = mysql_select_query("SELECT DISTINCT num, fullname from {$TABLE_PREFIX}authors JOIN {$TABLE_PREFIX}bridlington_blogs ON {$TABLE_PREFIX}bridlington_blogs.createdBy = {$TABLE_PREFIX}authors.num");
<?php foreach ($authors as $record): ?>

<option value = "<?php echo $record['num'];?>" <?php selectedIf($record['num'], @$_REQUEST['authornum']);?>>

<?php echo $record['fullname']; ?></option>

<?php endforeach ?>

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

I'm assuming your authors table is called 'authors', so you'll have to change it if it isn't. This is more advanced stuff so it may not work exactly right out of the box. Try it out and let me know if it works?

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

Claire Ryan
interactivetools.com

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

Hi, Claire -

Thanks for your help. I've spiced it up with a little jScript.

<?php $authors = mysql_select_query("SELECT DISTINCT {$TABLE_PREFIX}accounts.num, fullname from {$TABLE_PREFIX}accounts JOIN {$TABLE_PREFIX}gallery ON {$TABLE_PREFIX}gallery.createdByUserNum = {$TABLE_PREFIX}accounts.num"); ?>

<form method="post" action="?user=<?php echo $record['num'];?>">
<h3>Filter by Author</h3>

<select name="authorNum" onChange="top.location.href = this.form.authorNum.options[this.form.authorNum.selectedIndex].value; return false;">
<?php foreach($authors as $record): ?>
<option value = "?user=<?php echo $record['num'];?>" <?php selectedIf($record['num'], @$_REQUEST['authornum']);?>>
<?php echo $record['fullname']; ?></option>
<?php endforeach ?>
</select>
</form>

Is there a way of re-writing this in jQuery?

:0)

Perch

By claire - November 19, 2014

Hey Perch

You could rewrite this in jQuery, yeah, but is there any particular reason why you'd want to, if this works as is?

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

Claire Ryan
interactivetools.com

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

Hi, Claire

no reason other than to learn a bit more about jquery!

:0)

Perch

By claire - November 20, 2014

Alright then :) easiest thing to do is put the JS into a separate function, like so:

<select name="authorNum" id="authorNum" onChange="selectChange()">

Then you add something like this elsewhere:

<script>

$(document).ready(function() {
    function selectChange() {
        var val = $("#authorNum").val();
        // do stuff with the value here
    }
});

</script>

That'll get you started at least!

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

Claire Ryan
interactivetools.com

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