Simple Portal

Support => English Support => Topic started by: mack on October 13, 2009, 08:23:24 AM

Title: Listing Articles by category
Post by: mack on October 13, 2009, 08:23:24 AM
hey guys,

i'm fairly new to simpleportal and have a question about article categories .. i've been looking through simpleportal and can find an answer

what i want on my homepage is a block that allows you to click to view a list of articles under a category name. so for example when i click on a link to see game news the link takes me to the 'games news' category, listing all articles in that category.

my issue isn't about settings this block up .. it's about how i link to the category id, and how to display the articles for that category id on a separate page.

thanks in advance for any help
Title: Re: Listing Articles by category
Post by: Nathaniel on October 13, 2009, 04:48:10 PM
My suggestion, would be that you make a Custom PHP Page, which you can add code to show the articles for a particular category

Ie. index.php?pageid=showarticles;cat=x

Then use $_REQUEST['cat'] to get the id, and show the articles. Refer to the 'sportal_articles' and 'sportal_articles_callback' functions from the Sources/PortalArticles.php file, for help on accessing and displaying articles. The Themes/default/PortalArticles.template.php file may also be useful.
Title: Re: Listing Articles by category
Post by: mack on October 13, 2009, 11:10:55 PM
cheers nathaniel .. i'll look into it  ;D
Title: Re: Listing Articles by category
Post by: mack on October 15, 2009, 12:03:44 PM
wow ..

well since i wrote i've been trying a few different things to get it working, and failing .. hard (hehe)

i took your advice and tried to work with the sportal_articles and sportal_articles_callback functions in the file you mentioned, but it's led to all sorts of problems (which i've only just reset)

my php is very rusty .. can anyone be more definitive about the code i need in my custom php block?

thanks again for any help
Title: Re: Listing Articles by category
Post by: mack on October 18, 2009, 05:26:44 PM
 /polite bump
Title: Re: Listing Articles by category
Post by: mack on October 19, 2009, 11:10:44 PM
114 views and no replies? does no-one know? this must've come up before for someone?

help meh .. i'm staring at code and peeing myself a little bit  :'(
Title: Re: Listing Articles by category
Post by: Nathaniel on October 20, 2009, 06:55:19 AM
As this is really a Customization request and we have just had a release, its a bit unreasonable to expect someone to code it for you within a day or two. ;)

Anyway try this code in a custom page.
    global $db_prefix, $context, $modSettings, $user_info, $article_request, $sourcedir, $scripturl, $txt;

    require_once($sourcedir . '/PortalArticles.php');
    require_once($sourcedir . '/Subs-Portal.php');

    $category = !empty($_REQUEST['cat']) ? $_REQUEST['cat'] : 1;
    $articleperpage = 5;

    loadLanguage('Stats');
    loadTemplate('PortalArticles');

    $request = db_query("
        SELECT COUNT(*)
        FROM {$db_prefix}sp_articles as a
            INNER JOIN {$db_prefix}sp_categories AS c ON (c.ID_CATEGORY = a.ID_CATEGORY)
            INNER JOIN {$db_prefix}messages AS m ON (m.ID_MSG = a.ID_MESSAGE)
            INNER JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = m.ID_BOARD)
        WHERE $user_info[query_see_board]
            AND approved = 1
            AND publish = 1
            AND c.ID_CATEGORY = $category", __FILE__, __LINE__);
    list ($totalArticles) = mysql_fetch_row($request);
    mysql_free_result($request);

    $context['start'] = !empty($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
    $context['page_index'] = constructPageIndex($scripturl . '?page=' . $context['SPortal']['page']['page_id'] . ';cat=' . $category, $context['start'], $totalArticles, $articleperpage);

    if (empty($modSettings['sp_disableColor']))
    {
        $members_request = db_query("
            SELECT m.ID_MEMBER
            FROM {$db_prefix}sp_articles AS a
                INNER JOIN {$db_prefix}messages AS m ON (m.ID_MSG = a.ID_MESSAGE)
                INNER JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = m.ID_BOARD)
                INNER JOIN {$db_prefix}sp_categories AS c ON (c.ID_CATEGORY = a.ID_CATEGORY)
            WHERE $user_info[query_see_board]
                AND approved = 1
                AND publish = 1
                AND m.ID_MEMBER != 0
            ORDER BY a.ID_MESSAGE DESC" . (empty($articleperpage) ? "" : "
            LIMIT $context[start], $articleperpage"), __FILE__, __LINE__);
        $colorids = array();
        while($row = mysql_fetch_assoc($members_request))
            $colorids[] = $row['ID_MEMBER'];
        mysql_free_result($members_request);

        if (!empty($colorids))
            sp_loadColors($colorids);
    }

    $article_request = db_query("
        SELECT
            a.ID_ARTICLE, a.ID_CATEGORY, a.ID_MESSAGE, a.approved, c.name as cname, c.picture, m.ID_MEMBER,
            IFNULL(mem.realName, m.posterName) AS posterName, m.icon, m.subject, m.body, m.posterTime,
            m.smileysEnabled, t.ID_TOPIC, t.numReplies, t.numViews, t.locked, b.ID_BOARD, b.name as bname,
            mem.avatar, at.ID_ATTACH, at.attachmentType, at.filename
        FROM {$db_prefix}sp_articles AS a
            INNER JOIN {$db_prefix}sp_categories AS c ON (c.ID_CATEGORY = a.ID_CATEGORY)
            INNER JOIN {$db_prefix}messages AS m ON (m.ID_MSG = a.ID_MESSAGE)
            INNER JOIN {$db_prefix}topics AS t ON (t.ID_FIRST_MSG = a.ID_MESSAGE)
            INNER JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = m.ID_BOARD)
            LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
            LEFT JOIN {$db_prefix}attachments AS at ON (at.ID_MEMBER = mem.ID_MEMBER)
        WHERE $user_info[query_see_board]
            AND approved = 1
            AND publish = 1
            AND c.ID_CATEGORY = $category
        ORDER BY a.ID_MESSAGE DESC" . (empty($articleperpage) ? "" : "
        LIMIT $context[start], $articleperpage"), __FILE__, __LINE__);

    $cat_request = db_query("
        SELECT name, ID_CATEGORY
        FROM {$db_prefix}sp_categories", __FILE__, __LINE__);

    $cats = array();
    while ($row = mysql_fetch_assoc($cat_request))
        $cats[] = ($category == $row['ID_CATEGORY'] ? '<strong>[' : '') . '<a href="' . $scripturl . '?page=' . $context['SPortal']['page']['page_id'] . ';cat=' . $row['ID_CATEGORY'] . '">' . $row['name'] . '</a>' . ($category == $row['ID_CATEGORY'] ? ']</strong> ' : '');
    mysql_free_result($cat_request);

    echo '
                <div class="sp_regular_padding;">', implode($cats, ' | '), '</div><br />
                <div class="sp_regular_padding;">';

    while ($article = sportal_articles_callback())
    {
        echo '
                    <div class="tborder">
                        <table class="sp_block">
                            <tr class="catbg">
                                <td class="sp_middle">', $article['message']['icon'], '</td>
                                <td class="sp_middle sp_regular_padding sp_fullwidth">', $article['topic']['link'], ' | ', $article['message']['timeyear'], '</td>
                            </tr>
                            <tr class="windowbg">
                                <td class="sp_regular_padding" colspan="2">';

        if (!empty($modSettings['articleavatar']) && $article['poster']['avatar']['name'] !== null && !empty($article['poster']['avatar']['href']))
            echo '
                                    <img src="', $article['poster']['avatar']['href'], '" alt="', $article['poster']['name'], '" width="30" style="float: right;" />
                                    <div class="middletext">', $article['message']['timeday'], ' ', $txt[525], ' ', $article['poster']['link'], '<br />', $txt['sp-articlesViews'], ': ', $article['topic']['views'], ' | ', $txt['sp-articlesComments'], ': ', $article['topic']['replies'], '</div>';
        else
            echo '
                                    <div class="middletext">', $article['message']['timeday'], ' ', $txt[525], ' ', $article['poster']['link'], ' | ', $txt['sp-articlesViews'], ': ', $article['topic']['views'], ' | ', $txt['sp-articlesComments'], ': ', $article['topic']['replies'], '</div>';

        echo '
                                    <div class="post"><hr />', !empty($article['category']['picture']['href']) ? '<div><img src="' . $article['category']['picture']['href'] . '" alt="' . $article['category']['name'] . '" class="sp_article_image" align="right" /></div>' : '', $article['message']['body'], '<br/><br/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td class="windowbg2" colspan="2">
                                    <div class="sp_right sp_regular_padding">', $article['article']['link'], ' ',  $article['article']['new_comment'], '</div>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <br />';
    }

    if (!empty($articleperpage))
        echo '
                    <div class="sp_page_index">', $txt['sp-articlesPages'], ': ', $context['page_index'], '</div>';

    echo '
                </div>';
Title: Re: Listing Articles by category
Post by: mack on October 20, 2009, 12:37:28 PM
thanks for the reply .. sry if i came across as being unreasonable .. i just thought this would've come up before somewhere which is why i was a little surprised at the lack of response .. but np :)

thanks for the code tho, i'll give it a whirl
Title: Re: Listing Articles by category
Post by: Nathaniel on October 23, 2009, 01:40:33 AM
Any luck with that code mack?
Title: Re: Listing Articles by category
Post by: mack on October 28, 2009, 05:22:09 AM
Quote from: Nathaniel on October 23, 2009, 01:40:33 AM
Any luck with that code mack?

sorry for the delayed response

in short .. no. basically it showed all of the articles, from all categories. i might've misunderstood something in your post ofc.
Title: Re: Listing Articles by category
Post by: Nathaniel on October 29, 2009, 05:19:02 AM
Doesn't it display a list of categories at the top of the page? If you select a specific category, then it will only show articles from that category.
Title: Re: Listing Articles by category
Post by: mack on October 29, 2009, 10:17:34 AM
i'll set it up again later on and see .. can't say i noticed that the first time i tested it
Title: Re: Listing Articles by category
Post by: mack on October 29, 2009, 06:38:45 PM
i think i hadn't noticed it first time around because of the way my design restricts a certain amount of simpleportal's previews

but yes .. it works really well ..

for anyone interested in how i incoporated it:

all i needed to do was create a page in simpleportal with the php in it (making sure the page type was selected as 'php'). then i added some buttons from my home page to point to each category that the new page switches between ('cat=1', 'cat=2' etc) and that was that.

@nathaniel - hopefully by the time you read this i'll have added in the extra buttons so you can see it at work (clan4 web site (http://www.macksites.co.uk/clan4/site/index.php) .. links on the right to games news, hardware news, and odds 'n' ends). oh and nathaniel .. awesome mate .. thank you very much :)

Title: Re: Listing Articles by category
Post by: peteewheat on October 31, 2009, 03:43:23 PM
I keep getting this error when I try to implement this:

Fatal error: Call to undefined function: db_query() in /home/xxxx/public_html/mysite/Sources/Subs-Portal.php(1333) : eval()'d code on line 12

Argh!
Title: Re: Listing Articles by category
Post by: Nathaniel on October 31, 2009, 06:14:11 PM
@peteewheat,
That code is for SMF 1.1.x, you are running SMF 2, so the code won't work properly.

I can convert the code for SMF 2 (when I find the time) if you want?
Title: Re: Listing Articles by category
Post by: peteewheat on October 31, 2009, 07:06:47 PM
Yes! That would be awesome. I have a football league with news articles broken out by team (30'ish teams). Each team has their own page and I would love to break out the articles for each team.
Title: Re: Listing Articles by category
Post by: Nathaniel on October 31, 2009, 09:23:00 PM
The code below is the same as the code above, but adapted for SMF 2.

    global $db_prefix, $context, $modSettings, $user_info, $article_request, $sourcedir, $scripturl, $txt, $smcFunc;

    require_once($sourcedir . '/PortalArticles.php');
    require_once($sourcedir . '/Subs-Portal.php');

    $category = !empty($_REQUEST['cat']) ? $_REQUEST['cat'] : 1;
    $articleperpage = 5;

    loadLanguage('Stats');
    loadTemplate('PortalArticles');

$request = $smcFunc['db_query']('','
SELECT COUNT(*)
FROM {db_prefix}sp_articles as a
INNER JOIN {db_prefix}sp_categories AS c ON (c.id_category = a.id_category)
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_message)
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
WHERE {query_see_board}
AND a.approved = {int:approved}
AND publish = {int:publish}
AND a.id_category = {int:id_category}',
array(
'approved' => 1,
'publish' => 1,
'id_category' => $category,
)
);
list ($totalArticles) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

    $context['start'] = !empty($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
    $context['page_index'] = constructPageIndex($scripturl . '?page=' . $context['SPortal']['page']['page_id'] . ';cat=' . $category, $context['start'], $totalArticles, $articleperpage);

if (empty($modSettings['sp_disableColor']))
{
$members_request = $smcFunc['db_query']('','
SELECT m.id_member
FROM {db_prefix}sp_articles AS a
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_message)
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
INNER JOIN {db_prefix}sp_categories AS c ON (c.id_category = a.id_category)
WHERE {query_see_board}
AND a.approved = {int:approved}
AND publish = {int:publish}
AND m.id_member != {int:guest}
ORDER BY a.id_message DESC' . (empty($modSettings['articleperpage']) ? '' : '
LIMIT {int:start}, {int:end}'),
array(
'approved' => 1,
'publish' => 1,
'start' => $context['start'],
'end' =>  $modSettings['articleperpage'],
'guest' => 0,
)
);
$colorids = array();
while($row = $smcFunc['db_fetch_assoc']($members_request))
$colorids[] = $row['id_member'];
$smcFunc['db_free_result']($members_request);

if (!empty($colorids))
sp_loadColors($colorids);
}

$article_request = $smcFunc['db_query']('','
SELECT
a.id_article, a.id_category, a.id_message, a.approved, c.name as cname, c.picture, m.id_member,
IFNULL(mem.real_name, m.poster_name) AS poster_name, m.icon, m.subject, m.body, m.poster_time,
m.smileys_enabled, t.id_topic, t.num_replies, t.num_views, t.locked, b.id_board, b.name as bname,
mem.avatar, at.id_attach, at.attachment_type, at.filename
FROM {db_prefix}sp_articles AS a
INNER JOIN {db_prefix}sp_categories AS c ON (c.id_category = a.id_category)
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_message)
INNER JOIN {db_prefix}topics AS t ON (t.id_first_msg = a.id_message)
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
LEFT JOIN {db_prefix}attachments AS at ON (at.id_member = mem.id_member)
WHERE {query_see_board}
AND a.approved = {int:approved}
AND publish = {int:publish}
AND c.id_category = {int:id_category}
ORDER BY a.id_message DESC' . (empty($articleperpage) ? '' : '
LIMIT {int:start}, {int:end}'),
array(
'approved' => 1,
'publish' => 1,
'start' => $context['start'],
'end' => $articleperpage,
'id_category' => $category,
)
);

    $cat_request = $smcFunc['db_query']('','
        SELECT name, ID_CATEGORY
        FROM {db_prefix}sp_categories',
array()
);

    $cats = array();
    while ($row = $smcFunc['db_fetch_assoc']($cat_request))
        $cats[] = ($category == $row['ID_CATEGORY'] ? '<strong>[' : '') . '<a href="' . $scripturl . '?page=' . $context['SPortal']['page']['page_id'] . ';cat=' . $row['ID_CATEGORY'] . '">' . $row['name'] . '</a>' . ($category == $row['ID_CATEGORY'] ? ']</strong> ' : '');
    $smcFunc['db_free_result']($cat_request);

    echo '
                <div class="sp_regular_padding;">', implode($cats, ' | '), '</div><br />
                <div class="sp_regular_padding;">';

    while ($article = sportal_articles_callback())
    {
        echo '
                    <div class="tborder">
                        <table class="sp_block">
                            <tr class="catbg">
                                <td class="sp_middle">', $article['message']['icon'], '</td>
                                <td class="sp_middle sp_regular_padding sp_fullwidth">', $article['topic']['link'], ' | ', $article['message']['timeyear'], '</td>
                            </tr>
                            <tr class="windowbg">
                                <td class="sp_regular_padding" colspan="2">';

        if (!empty($modSettings['articleavatar']) && $article['poster']['avatar']['name'] !== null && !empty($article['poster']['avatar']['href']))
            echo '
                                    <img src="', $article['poster']['avatar']['href'], '" alt="', $article['poster']['name'], '" width="30" style="float: right;" />
                                    <div class="middletext">', $article['message']['timeday'], ' ', $txt[525], ' ', $article['poster']['link'], '<br />', $txt['sp-articlesViews'], ': ', $article['topic']['views'], ' | ', $txt['sp-articlesComments'], ': ', $article['topic']['replies'], '</div>';
        else
            echo '
                                    <div class="middletext">', $article['message']['timeday'], ' ', $txt[525], ' ', $article['poster']['link'], ' | ', $txt['sp-articlesViews'], ': ', $article['topic']['views'], ' | ', $txt['sp-articlesComments'], ': ', $article['topic']['replies'], '</div>';

        echo '
                                    <div class="post"><hr />', !empty($article['category']['picture']['href']) ? '<div><img src="' . $article['category']['picture']['href'] . '" alt="' . $article['category']['name'] . '" class="sp_article_image" align="right" /></div>' : '', $article['message']['body'], '<br/><br/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td class="windowbg2" colspan="2">
                                    <div class="sp_right sp_regular_padding">', $article['article']['link'], ' ',  $article['article']['new_comment'], '</div>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <br />';
    }

    if (!empty($articleperpage))
        echo '
                    <div class="sp_page_index">', $txt['sp-articlesPages'], ': ', $context['page_index'], '</div>';

    echo '
                </div>';
Title: Re: Listing Articles by category
Post by: Pyccak on December 25, 2010, 11:04:35 AM
Can help me with this last script by Nathaniel
If user post $category = 0 , them show all Articles by date. :)
EhPortal 1.39.8 © 2024, WebDev