Page 1 of 1

Help. Is There a plugin for this?

Posted: Fri May 05, 2006 10:34 pm
by nerkette
I am new to the serendipity world, and completely new to the smarty templating system. I was looking for a blogging solution for a multi-user blog. Serendipity fit the bill and seemed very user friendly(Nice Job!), so I installed it. Everything worked great but now my employer is requesting a change to our index page, and I'm not completely certain how to print out the data in the right format through the smarty templates.

I installed the secured version, with multiple users, and multiple categories also. One look, Just multiple authors.

Here is an example of what I'm going for:

Category Name
Author Name
Author Name

Category Name
Author Name
Author Name

Each of the Category names would link to the category page, and the author's names would link to their author's page. I need it to only list the Authors one time each, instead of once per entry. Is there a simple way to do this without creating my own plugin? There doesn't seem to be a function that pulls the data with this kind of association. I can combine some functions if someone could point me in the right direction, or can I script that using the Smarty Templating System?

Re: Help. Is There a plugin for this?

Posted: Sat May 06, 2006 10:14 am
by garvinhicking
Hi!

First of all, it can be done. :-)

Second, I need some more details. :) Where to you want to fetch the Category->Author assignments from? By the "Write" permissions to a Category?

If so, you would really need your own custom plugin that fetches the data for it. I can give you the SQL query you need to do that, it's not hard. But first I need to know if that is what you want. :)

Best regards,
Garvin

Posted: Mon May 08, 2006 2:28 pm
by nerkette
Thanks for your help Garvin,

For each Entry created in the UI, We assign it to a Category. I need to pull a list of distinct authors for the entries done by category. Basically just from the database. So anyone who has entered an entry under that category will be in the list of authors that get pulled. I wrote a query outside of serendipity to see if I could get the results I wanted and then I was going to try and convert it to serendipity, but wasn't having much luck getting it in the proper places.

Here's what I'm aiming for.

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

//1ST QUERY, GET CATEGORIES AND IDs
$q1 = mysql_query("SELECT DISTINCT(categoryid), category_name FROM serendipity_category");
$num_rows3 = mysql_num_rows($q1);

while($rows = mysql_fetch_array($q1)) {
$categoryid = $rows["categoryid"];
$categoryname = $rows["category_name"];
$categoryLink = "<strong><a href='[SERENDIPITY_PATH]?/categories/$categoryid-".preg_replace("/ /","-",$categoryname)."'>$categoryname</a></strong><BR>";


$q2 = mysql_query("SELECT DISTINCT(serendipity_entries.authorid) FROM serendipity_entries
LEFT JOIN serendipity_entrycat ON serendipity_entries.id = serendipity_entrycat.entryid
WHERE serendipity_entrycat.categoryid = '$categoryid'");
$num_rows4 = mysql_num_rows($q2);
if ($num_rows4 > 0) {
print $categoryLink;

//LOOP THROUGH CATEGORIES ARRAY AND PULL AUTHORS
while ($rows2 = mysql_fetch_array($q2)) {
$authorid = $rows2["authorid"];
#echo "The Author ID is: $authorid, ";
$q3 = mysql_query("SELECT realname FROM serendipity_authors WHERE authorid = '$authorid'");
while ($rows3 = mysql_fetch_array($q3)) {
$authorname = $rows3["realname"];
echo "  <a href='[SERENDIPITY_PATH]?/authors/$authorid-".preg_replace("/ /","-",$authorname)."'>$authorname</a><BR>";
}
}
echo "<BR>";
} else {
//do nothing
}

}
----------------------------

If there are no entries assigned to the category, the category link won't get printed.

Posted: Mon May 08, 2006 3:58 pm
by garvinhicking
Hi!

Ah, I see now. Basically, what you want to do is one large SQL query joint. Do not put SQL queries within a while() or foreach() loop ever, if you can't avoid it. It will scale poorly.

Thus I wrote you one large JOIN query, which I hope will do the trick (I did not have good demo data, so I couldn't test it here):

Code: Select all

<?php

include 'serendipity_config.inc.php';

$x = serendipity_db_query("SELECT c.category_name, c.categoryid, a.authorid, a.realname
                             FROM serendipity_category AS c
                  LEFT OUTER JOIN serendipity_entrycat AS ec
                               ON ec.categoryid = c.categoryid
                  LEFT OUTER JOIN serendipity_entries AS e
                               ON e.id = ec.entryid
                  LEFT OUTER JOIN serendipity_authors AS a
                               ON a.authorid = e.authorid");
          
$output = array();                     
foreach($x AS $i => $category) {
    $output[$category['categoryid']]['category_name'] = $category['category_name'];
    if ($category['authorid'] > 0) {
        $output[$category['categoryid']]['authors'][$category['authorid']] = $category['realname'];
    }
}

foreach($output AS $category => $info) {
    echo 'Category #' . $category . ' is called ' . $info['category_name'] . '<br />';
    echo 'Listed authors:<br />';
    foreach($info['authors'] AS $authorid => $authorname);
        echo '#' . $authorid . ' -- ' . $authorname . '<br />';
    }
    echo '<br />';
}
HTH,
Garvin

Posted: Mon May 08, 2006 9:44 pm
by nerkette
Thanks!

That worked beautifully!