TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,973
  • Latest: NonStop
Stats
  • Total Posts: 196,231
  • Total Topics: 21,353
  • Online today: 606
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 228
  • Total: 228

Category box listing articles by alphabetical order?

Started by animecosmo, February 10, 2007, 11:22:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

animecosmo

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

JPDeni

#11

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!

softtouch_ph

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.

JPDeni

Post the code that displays an empty box, please. Let's see if we can figure out what the problem is.

softtouch_ph

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>';

JPDeni

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?

softtouch_ph

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.

JPDeni

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.

softtouch_ph

Yeah. Thanks for your support, JPDeni, very much appreciated!

Thurnok

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). ;)
Thurnok
The one, the only!

This website is proudly hosted on Crocweb Cloud Website Hosting.