TinyPortal

Development => Block Codes => Topic started by: IchBin on October 21, 2007, 05:22:59 AM

Title: [Block] Article Category Menu list
Post by: IchBin on October 21, 2007, 05:22:59 AM
This code snippet will grab all the titles of Categories and create a list with sub-categories indented. Thanks to Jaybachatero from SMF for the help.

Add this to a phpbox type block.

global $db_prefix, $scripturl;

$request = db_query("
SELECT id, value1, value2
      FROM {$db_prefix}tp_variables
        WHERE type = 'category'", __FILE__, __LINE__);

$links = array();
while ($row = mysql_fetch_assoc($request))
{
// It's a parent.
if (empty($row['value2']))
$links[$row['id']] = array(
'id' => $row['id'],
'name' => $row['value1'],
'link' => '<a href="' . $scripturl . '?cat=' . $row['id'] . '">' . $row['value1'] . '</a>',
'children' => array()
);
elseif (isset($links[$row['value2']]))
$links[$row['value2']]['children'][$row['id']] = array(
'id' => $row['id'],
'name' => $row['value1'],
'link' => '<a href="' . $scripturl . '?cat=' . $row['id'] . '">' . $row['value1'] . '</a>',
); 
}
mysql_free_result($request);

echo '
<ul style="list-style-type: none; margin-left: 0; padding-left: 0;">';

// Loop through the array.
foreach ($links as $link)
{
if (empty($link['children']))
echo '
<li>', $link['link'], '</li>';
else
{
echo '
<li>', $link['link'], '
';

// Indented Child links
                // Change margin setting for more indentation if needed.
foreach ($link['children'] as $child)
echo '
<li style="margin-left: 7px">', $child['link'], '</li>';

echo '

</li>';
}
}
echo '
</ul>';


Here's an additional nice looking layout and design of this code posted by Stardust.
http://www.tinyportal.net/index.php?topic=19495.msg203123#msg203123
Title: Re: Article Category Menu list
Post by: shawnlg on October 22, 2007, 03:04:40 AM
Great I look forward to applying the code.

Shawn Larson
Title: Re: Article Category Menu list
Post by: shawnlg on October 23, 2007, 08:29:00 AM
Looks good thank you.

One thing that would be nice to see in the future is the ability to collapse the subcategories in the categories if that makes any sense.  Thanks again for your help!
Title: Re: Article Category Menu list
Post by: IchBin on October 24, 2007, 11:58:17 PM
Well, in order to make the categories upshrink that would either require a database setting stored, or a cookie. Of which, I don't really know how to do atm. :(
Title: Re: Article Category Menu list
Post by: shawnlg on October 25, 2007, 12:00:50 AM
We will just leave this up and maybe somebody will have a suggestion.  I really need to learn php.  I have lots of cool ideas..
Title: Re: Article Category Menu list
Post by: IchBin on October 25, 2007, 12:20:32 AM
Well, doing the upshrink requires javascript. And I know pretty much zero when it comes to javascript.
Title: Re: Article Category Menu list
Post by: lebisol on October 25, 2007, 01:11:13 AM
You could reuse the upshrink code that came with default shirink-ers or implement this
http://www.tinyportalmedia.com/main/index.php/topic,1273.0.html
after Drgoon fixes up some details.
Title: Re: Article Category Menu list
Post by: IchBin on October 25, 2007, 01:16:49 AM
Feel free to do it lebisol. :)
Title: Re: Article Category Menu list
Post by: rockyrails on October 25, 2007, 01:44:06 AM
Thanks - great menu - even when made available only to admins makes it easy to access all the artilcles from the homepage. Thanks again
Cheers
Title: Re: Article Category Menu list
Post by: lebisol on October 25, 2007, 03:37:05 AM
Quote from: IchBinâ,,¢ on October 25, 2007, 01:16:49 AM
Feel free to do it lebisol. :)
:) Hey he asked for suggestions ...I will send you a pm if interested.
All the best!
Title: Re: Article Category Menu list
Post by: samson on October 25, 2007, 03:52:57 AM
after you select a section how can you make all the articles with no right blocks ??
Title: Re: Article Category Menu list
Post by: shawnlg on October 25, 2007, 10:51:53 PM
Not sure what you mean..  The category box just lists categories and subcategories.. Once you click the link it goes the category page with the article listings.
Title: Re: Article Category Menu list
Post by: shawnlg on October 25, 2007, 10:53:14 PM
PS Anything on the javascript idea..  Just curious..  It sounded really intriguing.. Thanks!
Title: Re: Article Category Menu list
Post by: samson on October 25, 2007, 10:59:25 PM
QuoteNot sure what you mean..  The category box just lists categories and subcategories.. Once you click the link it goes the category page with the article listings.

When you click on a category it brings you to a page with all the articles from that category. But I would like it to show the articles with no side blocks. I did not see an option for this
Title: Re: Article Category Menu list
Post by: Zetan on October 25, 2007, 11:01:05 PM
Each article has visual options.. and 3 of them are whether to display Left, Right or Center blocks.
Title: Re: Article Category Menu list
Post by: shawnlg on October 25, 2007, 11:02:27 PM
Would you just have your user just use top navigation or something to get to where ever they need to go after they view article?
Title: Re: Article Category Menu list
Post by: Zetan on October 25, 2007, 11:06:39 PM
Depends on the Article. I have some, like the chat room and other forum features that don't have any navigation, other than the main menu bar.. Help Articles, I display blocks on the left.
Title: Re: Article Category Menu list
Post by: samson on October 25, 2007, 11:06:56 PM
Yes if i click on the individual article the blocks go away but when it shows all the articles when you click on the category I would like no side blocks
Title: Re: Article Category Menu list
Post by: shawnlg on October 25, 2007, 11:09:23 PM
Zetan it sounds like you can do it.  How is it done?
Title: Re: Article Category Menu list
Post by: Zetan on October 25, 2007, 11:17:51 PM
When you see an Article in Admin view, or the edit screen.. each has two option panels.

[Edit options]  is where all the visual options are, like link tree, panels, commenting etc.

Remember though, if you are displaying more than 1 article on say, the home page.. and 1 has left blocks, and the other dosen't, they will conflict.. so both need to ideally be set the same.. One can have comments enabled though, and the other can have comments switched off.

It's generally blocks and article theme settings that can be a problem. No fault of the software, but conflicting visual options can cause confusion.
Title: Re: Article Category Menu list
Post by: samson on October 26, 2007, 12:01:53 AM
Ill look threw all the options again thanks
Title: Navigation Script - By IchBin
Post by: GiL-GaLaD on January 04, 2008, 02:13:24 AM
Hey, I'm currently using the navigation script, created by IchBin (It rocks :P) however, I created a new category and 3 new forums and they didn't appear on the Navigation. Well, at least the forums didn't, the category does appear..

Link: RoC.Blizzcraft.Org
Title: Re: Navigation Script - By IchBin
Post by: IchBin on January 04, 2008, 02:39:41 AM
For support you should post in the topic where the code is posted. Which forums do not appear? I just looked at your list and it shows the same boards that I can see in the list.
Title: Re: Navigation Script - By IchBin
Post by: G6Cad on January 04, 2008, 08:16:38 AM
This topic is also posted in the wrong section of the forum.
I will lock this and refer to IchBins thread in the block code and snippets board.
Title: Re: Navigation Script - By IchBin
Post by: GiL-GaLaD on January 04, 2008, 05:12:13 PM
The category "lord of the rings" doesn't show its forums.
Title: Re: Article Category Menu list
Post by: IchBin on January 04, 2008, 11:28:36 PM
Are you talking about this section?

See attachment.
Title: Re: Article Category Menu list
Post by: Tracy Beyer on January 08, 2008, 09:42:27 PM
Forgive my ignorance, which type of Box would you put this code into? Scriptbox?  PHPBOX?
Title: Re: Article Category Menu list
Post by: IchBin on January 09, 2008, 12:19:58 AM
phpbox block type.
Title: Re: Article Category Menu list
Post by: Stardust on July 18, 2008, 08:03:27 PM
Has anyone ever tweaked this snippet to include the articles themselves?

(I've searched and searched and searched, so please forgive me if I missed where this might have already been posted)
Title: Re: Article Category Menu list
Post by: IchBin on July 19, 2008, 12:42:02 AM
If it had been done I'm sure it would have been posted in this topic. Why would you want a menu style of articles with full body text? 
Title: Re: Article Category Menu list
Post by: Stardust on July 19, 2008, 04:52:32 PM
I'm sorry, I wasn't clear.

This script does almost everything I need.  I'd like one extra layer to nest the article names.

e.g.

Category
--Article Name
--Article Name
----SubCategory
------Article Name
----SubCategory
------Article Name
Category
--Article Name
Title: Re: Article Category Menu list
Post by: IchBin on July 22, 2008, 01:24:50 AM
Sorry, but the multiple sub categories and articles is a little much on my PHP/MySQL knowledge. This requires some tables joins as well as determining what is a category vs article, and what is a parent vs child. Its pretty difficult. At least for me it is... I'll keep stabbing at it though to see if I can figure it out as I get time.
Title: Re: Article Category Menu list
Post by: Stardust on July 22, 2008, 01:50:29 AM
Well, I appreciate it.  It's certainly beyond my abilities (which are next to none ;) )

At any rate, I understand that most everyone on here has enough to do with their real lives without adding to the work pile for all us needy folks.  Good luck :)
Title: Re: Article Category Menu list
Post by: Stardust on August 06, 2008, 04:36:49 PM
I've been toying around with snippits from a variety of posts here and came up with this:

global $db_prefix, $scripturl;

$articles = db_query("
        SELECT art.subject, art.id, var.value1 AS category_name
        FROM {$db_prefix}tp_articles AS art
                LEFT JOIN {$db_prefix}tp_variables AS var ON (var.id = art.category) ORDER BY value1
        LIMIT 100", __FILE__,__LINE__);


echo '
<table cellspacing="0" cellpadding="0" width="100%" style="margin-top: 4px; margin-bottom: 4px;" border="0" class="tborder">
<tr>
<td>
<ul style="list-style-type: none; margin-left: 0; padding-left: 0;">';

$category = '';

if (mysql_fetch_assoc($articles) == 0)
        echo '
No Reviews Available';
else
{
        while ($row = mysql_fetch_assoc($articles)){
$category2 = $row['category_name'];
if($category != $category2){
echo'
<li><b><i><u>', $category2 ,'</u></i></b></li>';
$category = $category2;
}
$category3 = $row['category_name'];
if($category3 == $category2){
        echo '
<li style="margin-left: 7px"><font size="1"><a href="', $scripturl ,'?page=', $row['id'], '"><b>', $row['subject'], '</b></a></font></li>';
}

}

}
echo '
</ul>
</td>
</tr>
</table>';

mysql_free_result($articles);


It doesn't indent subcategories, but for as few articles as I have, there's not really a need.  I apologize if this isn't the appropriate thread to put this in, but it was where I had made my initial inquiry. 
Title: Re: Article Category Menu list
Post by: IchBin on August 06, 2008, 06:52:20 PM
Looks nice. I'll link  to it in the first post. Thanks!
Title: Re: Article Category Menu list
Post by: Ken. on August 06, 2008, 07:52:19 PM
Just tried it and it works very well.  :up:
... had to turn it off though, mine was showing over 50 articles!
Title: Re: [Block] Article Category Menu list
Post by: [chrisB] on January 07, 2023, 12:38:57 AM
Does this still work?

I addded it as a PHPblock but nothing shows, this said I don't have any articles just yet but I would like to show all the categories I intend to post articles too.

Any help would be great appreciated.
Title: Re: [Block] Article Category Menu list
Post by: @rjen on January 15, 2023, 04:27:18 PM
Wow, that code was seriously outdated. I assume that was from the SMF1 era, did not work in 2.0 or in 2.1.

I updated the code so it works in SMF2.1. Find the corrected PHP code below.

Warning this code produces a list of articles per category, but it does NOT check to see if the article is published, or if the user actually has permissions to see them.

It may be wise to use this code with caution...

global $smcFunc, $scripturl, $modSettings, $settings, $txt;

// Retrieve the articles.
$request = $smcFunc['db_query']('', '
        SELECT art.subject, art.id, var.value1 AS category_name
FROM ({db_prefix}tp_articles AS art)
LEFT JOIN {db_prefix}tp_variables AS var ON (var.id = art.category) ORDER BY value1',);

$articles = array();

while ($row = $smcFunc['db_fetch_assoc']($request)) {
$articles[] = array(
'subject' => $row['subject'],
'id' => $row['id'],
'category_name' => $row['category_name'],
);
}

$smcFunc['db_free_result']($request);

echo '
<ul style="list-style-type: none; margin-left: 0; padding-left: 0;">';

if (empty($articles)) {
        echo '
No articles Available';
}
else

$category = '';
foreach ($articles as $article)
{
$category2 = $article['category_name'];
if($category != $category2){
echo'
<li><b><i><u>', $category2 ,'</u></i></b></li>';
$category = $category2;
}
$category3 = $article['category_name'];
if($category3 == $category2){
        echo '
<li style="margin-left: 7px"><font size="1"><a href="', $scripturl ,'?page=', $article['id'], '"><b>', $article['subject'], '</b></a></font></li>';
}
}

echo '
</ul>';
Title: Re: [Block] Article Category Menu list
Post by: @rjen on January 15, 2023, 05:04:23 PM
Added a select so only active and approved articles are listed, and also a sort in the articles on position (ascending) and date (descending, so newer on top)

global $smcFunc, $scripturl, $modSettings, $settings, $txt;

// Retrieve the articles.
$request = $smcFunc['db_query']('', '
        SELECT art.subject, art.id, art.parse, art.date, var.value1 AS category_name
FROM ({db_prefix}tp_articles AS art)
LEFT JOIN {db_prefix}tp_variables AS var ON (var.id = art.category)
WHERE art.approved = 1 and art.off = 0
ORDER BY value1, parse, date DESC',);

$articles = array();

while ($row = $smcFunc['db_fetch_assoc']($request)) {
$articles[] = array(
'subject' => $row['subject'],
'id' => $row['id'],
'category_name' => $row['category_name'],
);
}

$smcFunc['db_free_result']($request);

echo '
<ul style="list-style-type: none; margin-left: 0; padding-left: 0;">';

if (empty($articles)) {
        echo '
No articles Available';
}
else

$category = '';
foreach ($articles as $article)
{
$category2 = $article['category_name'];
if($category != $category2){
echo'
<li><b><i><u>', $category2 ,'</u></i></b></li>';
$category = $category2;
}
$category3 = $article['category_name'];
if($category3 == $category2){
        echo '
<li style="margin-left: 7px"><font size="1"><a href="', $scripturl ,'?page=', $article['id'], '"><b>', $article['subject'], '</b></a></font></li>';
}
}

echo '
</ul>';
Title: Re: [Block] Article Category Menu list
Post by: [chrisB] on January 16, 2023, 01:37:52 AM
You're a star rjen!

I found the inbuilt menu code a few days ago, it takes me a bit of time to navigate and understand bits. These is a nice addition though. 🙇‍♂️