Main Menu
collapse

Simple Portal Archived Forum

This is an Archive Forum.

The content in this forum may be out-of-date or have been superseded by newer information, and links in forum pages to other sites may not work.
This forum contains archives for future reference.

Visit our thread at Simple Machines Forum for current support.

SMF 2.1 users: EhPortal is a ported version of Simple Portal specifically designed for the SMF 2.1 branch.
Please visit web-develop.ca to download EhPortal and for its support.

User Info

Welcome Guest.
Please log in.

Who's Online

  • Dot Guests: 1305
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

Recent Posts

Adding Forums Button to Nav bar by jirapon
[August 01, 2019, 09:07:12 AM]


Re: Board Icons by ♦ Ninja ZX-10RR ♦
[July 30, 2019, 04:03:41 PM]


MOVED: Czech translation???? by ♦ Ninja ZX-10RR ♦
[July 30, 2019, 03:04:51 PM]


Board Icons by jirapon
[July 30, 2019, 07:28:44 AM]


Re: Thankyou Simpleportal, by ♦ Ninja ZX-10RR ♦
[July 29, 2019, 09:41:29 AM]


Installation errors? Mod incompatibilities? Upgrade problems? Make your way over to the Install and Upgrade Support board for all your solutions!

Listing Articles by category

Started by mack, October 13, 2009, 08:23:24 AM

Previous topic - Next topic

0 Members and 5 Guests are viewing this topic.

mack

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
Jango

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

Game-Bank

Nathaniel

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.
SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

mack

cheers nathaniel .. i'll look into it  ;D
Jango

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

Game-Bank

mack

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
Jango

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

Game-Bank

mack

Jango

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

Game-Bank

mack

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  :'(
Jango

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

Game-Bank

Nathaniel

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>';
SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

mack

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
Jango

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

Game-Bank

Nathaniel

SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

mack

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.
Jango

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

Game-Bank

Nathaniel

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.
SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

mack

i'll set it up again later on and see .. can't say i noticed that the first time i tested it
Jango

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

Game-Bank

mack

#12
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 .. links on the right to games news, hardware news, and odds 'n' ends). oh and nathaniel .. awesome mate .. thank you very much :)

Jango

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

Game-Bank

peteewheat

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!

Nathaniel

@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?
SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

peteewheat

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.

Nathaniel

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>';
SMF Friend (Former Support Specialist) | SimplePortal DeveloperMy SMF Mods | SimplePortal"Quis custodiet ipsos custodes?" - Who will Guard the Guards?Please don't send me ANY support related PMs. I will just delete them.

Pyccak

Can help me with this last script by Nathaniel
If user post $category = 0 , them show all Articles by date. :)