TinyPortal

Development => Support => Topic started by: animecosmo on February 10, 2007, 11:22:11 PM

Title: Category box listing articles by alphabetical order?
Post by: animecosmo on February 10, 2007, 11:22:11 PM
I was wondering if there was a way to make the category box (or a scriptbox) automatically list articles in a certain category by alphabetical order rather than by date published? I run a role playing game, and I'd like to have the characters listed alphabetically by last name for ease in finding the profiles.

Thanks in advance! TP people respond much faster, and with much better responses to all my questions than people in the regular SMF forum do. You guys are awesome!
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 10, 2007, 11:34:45 PM
You could write a php block or article that would list the articles any way you wanted. It wouldn't be the built-in category box, but you could easily have your own category box set up however you wanted.
Title: Re: Category box listing articles by alphabetical order?
Post by: animecosmo on February 10, 2007, 11:41:28 PM
Quote from: JPDeni on February 10, 2007, 11:34:45 PM
You could write a php block or article that would list the articles any way you wanted. It wouldn't be the built-in category box, but you could easily have your own category box set up however you wanted.

I know I can do that, but I am hoping to make it automatic so that I don't have to add a new link each time a new character is made. Firstly, I'm likely to forget to add it (LOL), and secondly, it is a lot of extra work that I'd rather not have to do, if possible. :)

I'm assuming (I'm still a PHP infant, so bear with me, please) there must be a way to use the code that makes a category box and just change it to list by article name rather than by date posted.
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 10, 2007, 11:51:27 PM
You can do it automatically if you have a php article or block. It would be like a category box, but a specialized one that you would (or I would, I guess :) ) program to behave exactly the way you want it to.

I'm not talking about an html box that you have to add to yourself. It would update itself.
Title: Re: Category box listing articles by alphabetical order?
Post by: animecosmo on February 10, 2007, 11:53:06 PM
Oooh, how would I go about doing that? Also, I think I should know this, but how do you get the authour to not show up in said box (or any particular category box?).
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 11, 2007, 12:05:23 AM
QuoteOooh, how would I go about doing that?
You ask me really nice and I'll write the php code for you. ;)

QuoteAlso, I think I should know this, but how do you get the authour to not show up in said box (or any particular category box?).
If you program it yourself (or myself :) ), you can put whatever you want there. Or not put whatever you don't want. So....

You want all the article titles in category number... ? (You'll need to find out what the category number is that you want to include.) You want the titles to be clickable links to the full article. You want the titles to be in alphabetical order.

Anything else? Do you want a bulleted list?

Once you see it, if you take a look at the code, you should be able to figure out how to change the formatting, if you should want to later on.
Title: Re: Category box listing articles by alphabetical order?
Post by: animecosmo on February 11, 2007, 01:11:56 AM
Would it pretty please be possible for you to write the php code for me? *Big grin* I'd love to learn what exactly it means, too, as I am trying hard to learn PHP on my own. However, there are no classes at my college for it. >_>

I finally figured out what an echo was and how to use it via reading the posts on this site. I was so excited when I wrote my first echo.   ;D (Yes, I'm pathetic. I know.)

I want all the articles in category number um...good question (how do I find that out?), clickable links to the full article, author not shown, titles in alphabetical order, bullets not needed.

You can see how I have everything right now at http://www.animecosmo.com/Forum

Look for the "Magical Tidings" block, and then look just below that. I set the two boxes in question to be viable by guests (normally only viewable by people who are active in the RP) so you can see it.

I feel so embarassed. I've no clue what I am doing, but I am trying very hard to learn. LOL.
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 11, 2007, 02:20:01 AM
The way to learn is to look at things and work with them and study them. One thing at a time. I didn't take any classes, either. It's just a puzzle, really. Figuring out how it's done.

Okay... Do you have any articles in the category now? If not, create the category and create an article and assign it the category so you'll have something to access. It won't be happy if there's nothing there.

It would be nice if the category number were visible, since that's how they're referenced in the database. Do you have access to the database, like through phpMyAdmin? If so, you'll be able to find the category number through there. Look for the table smf_tp_articles and find the article you made for this. Look for the field called "category" and see what number is in that field for the article. If this makes no sense to you after giving it a try, I'll try to give you more explicit instructions.

Anyway, once you have the number, put this in a php block:


global $db_prefix, $scripturl;

$category = 36; // this is where you should put your category number. Make sure you leave in the semi-colon

$query = db_query(
    "SELECT id, subject
     FROM {$db_prefix}tp_articles
     WHERE category = $category
     AND off = 0
     AND approved = 1
     ORDER BY subject", __FILE__, __LINE__);
echo '<div style="height: 150px; overflow: auto; width: 100%;">';
while ($row = mysql_fetch_assoc($query))
  echo '<a href="' . $scripturl . '?page=' . $row['id'] . '">' . $row['subject'] . '</a><br />'; 
echo '</div>';


I tested it and it works fine if you have the category number.

While you're at it, take a look at the code and see what's going on. All it does is say "Go into the database table, pick all the rows that have the category I've selected and put them in alphabetical order by subject. Then go through each one and print it out."
Title: Re: Category box listing articles by alphabetical order?
Post by: animecosmo on February 11, 2007, 08:56:12 PM
Thank you once again. :)

Is there a way to make it scroll like the category box does, after a certain amount of articles?
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 11, 2007, 09:01:58 PM
I'm sure there is a way to make it scroll, but I don't know how to do it. I think that's a css thing that I've just not used before. I'll ask the other team members in case they're not reading this topic.
Title: Re: Category box listing articles by alphabetical order?
Post by: animecosmo on February 11, 2007, 09:14:21 PM
Also, I'm trying to piece this together so I can learn. Bear in mind that I really know very, very little about PHP and MySQL. I'm learning as I go, but mostly I just know how to do includes. It's a work in progress. LOL.


global $db_prefix, $scripturl;


Is this just pointing it to the right database?


$category = 36; // this is where you should put your category number. Make sure you leave in the semi-colon


Obviously this is the category number, and telling it what category to access.


$query = db_query(
    "SELECT id, subject
     FROM {$db_prefix}tp_articles
     WHERE category = $category
     ORDER BY subject", __FILE__, __LINE__);


This is where I get a little more confused. Select ID. Is that selecting the article ID or the category ID? From is saying to pull it from the tp_articles table, right? Where is telling it to pull from the category defined above. And order by subject is ordering by alphabetical order, I am guessing. What is the __FILE__ and __LINE__ ?

while ($row = mysql_fetch_assoc($query))
  echo '<a href="' . $scripturl . '?page=' . $row['id'] . '">' . $row['subject'] . '</a><br />'; 


I'm guessing this is just allowing it to actually show each article, but I'm not quite clear how it does that. $row = mysql_fetc_assoc - I've no idea what that is. I'm taking a wild guess and assuming it defines "$row" to mean that it fetches the article or subject in question.

a href would be linking it to the article, I am guessing. I'm not exactly sure what .scripturl is, now ?page or $row['id']. Those I am guessing it is saying to put one per row and $row[id] is saying to make a row containing whichever article id goes in that spot. $row['subject'] would make it
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 11, 2007, 09:39:16 PM

global $db_prefix, $scripturl;

These are variables that are used in various parts of SMF and TP. You have to tell the script that you want to keep the same values that are used elsewhere for the variables. Otherwise, it thinks you want to use them for some new value.

The $query thing is based on the table in the database table where the articles are kept. id (not ID -- letter case matters!!) is the name of the field that gives a unique value for each article. We're selecting that field so that we can use it later in the link to be able to access the article.

QuoteWhat is the __FILE__ and __LINE__ ?
Those are internal things. I could probably figure out what they are, but I haven't bothered. :) Basically what I did was look at a number of different places where the program accessed the database and followed the same pattern. This is the pattern they used. __FILE__ and __LINE__ are environment variables defined within php that I've never worried about.

The "while" line -- Take a look at http://us2.php.net/mysql_fetch_assoc which may be way over your head, but explains it a little bit better than I can.

Think of the data returned from the query as being a table. Each row of the table is one record -- in this case, the information we want from each article in a category. Each column in the the table is one of the fields that we said we wanted, which is just the number and the title (subject). What the "while" line means, as a whole, is

"Go through each row in the table returned from the query, and assign the values to an associative array called $row"

Since there are only two values being returned, for each row, that array has just two values:
$row['id']
$row['subject']

$scripturl has been defined elsewhere in the script -- actually in the Settings.php file, I think -- so you don't have to worry about it. As long as you mention it in your global statement, you can use it as many times as you want. The advantage of using the variable instead of your actual url, is that, if you ever move your site to a different url, you don't have to change your script. All you have to do is set your Settings.php file once and it'll be taken care of.

Does this help?

BTW, I added a div statement to the original code to give you a scrollbar. Thanks to Lesmond for pointing me in the right direction!
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 17, 2007, 04:01:12 AM
I would like this too.
I have a category 54, with 2 test articles.
But this code just display an empty box here.
I verified the article category in the database, all is correct.
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 17, 2007, 01:08:38 PM
Post the code that displays an empty box, please. Let's see if we can figure out what the problem is.
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 17, 2007, 01:10:35 PM
I used the code from page 1 in this topic:

global $db_prefix, $scripturl;

$category = 54; // this is where you should put your category number. Make sure you leave in the semi-colon

$query = db_query(
    "SELECT id, subject
     FROM {$db_prefix}tp_articles
     WHERE category = $category
     ORDER BY subject", __FILE__, __LINE__);
echo '<div style="height: 150px; overflow: auto; width: 100%;">';
while ($row = mysql_fetch_assoc($query))
  echo '<a href="' . $scripturl . '?page=' . $row['id'] . '">' . $row['subject'] . '</a><br />'; 
echo '</div>';
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 17, 2007, 01:38:47 PM
I was just making sure. :)

If you have that code in a php box and you have articles in category number 54, they should be listed in alphabetical order.

Can I see the page with the block, please?
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 17, 2007, 02:01:25 PM
NOW I don't understand anymore. I had disabled the block because it did not work. Now I enabled it to be able to show it to you, and guess what, NOW the articles are listed... WHY???? I only had disabled and re-enabled the block, no more, no less.
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 17, 2007, 02:05:09 PM
Never ask why something works. :) It could be that there was some little glitch in your server before or a dozen other little things. The biggest lesson from this is that you should try something several times before you give up on it. Maybe even just disabling and re-enabling the block will cause it to work correctly.
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 17, 2007, 02:12:15 PM
Yeah. Thanks for your support, JPDeni, very much appreciated!
Title: Re: Category box listing articles by alphabetical order?
Post by: Thurnok on February 18, 2007, 07:14:25 PM
Sorry.. never saw this post originally until it was linked from another post I just read.. LOL

Looks like everything was covered.  Just an FYI ---  __FILE__ and __LINE__ are variables that the php processor holds the filename of the current script executing and the current line in that script.  The db_query() function is an SMF function that is used as a "wrapper" function to do a query on the database.

By passing the __FILE__ and __LINE__ variables to the db_query() function, if the query fails, SMF can write to the log where the error occurred (in what __FILE__ and at what __LINE__ number). ;)
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 19, 2007, 02:04:44 AM
One more thing: If an article is set to NOT active, it should not be listed... How can I check if the article is active or not to filter it out from the list?
Title: Re: Category box listing articles by alphabetical order?
Post by: JPDeni on February 19, 2007, 02:15:30 AM
I changed the original code by adding


AND off = 0
AND approved = 1


to the query.

That should do it for you. I also added a check to see if the article is approved.
Title: Re: Category box listing articles by alphabetical order?
Post by: softtouch_ph on February 19, 2007, 02:57:54 AM
Great, thanks. Works PERFECT now!
Title: Re: Category box listing articles by alphabetical order?
Post by: Varment on April 28, 2007, 10:30:22 PM
This is a nice script, it really saved me some time, thanks. I would like to point one thing out that might help some people; the category number is in the url for your article category. ie: Your Forum URL/index.php?cat=5. I also added an OR statement to get multiple categories to show up.
Nice work :D
V
Title: Re: Category box listing articles by alphabetical order?
Post by: fangweile on August 06, 2007, 04:15:02 AM
I am looking for this also, thanks a lot JPDeni, it works in my forum