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

Recent

Welcome to TinyPortal. Please login or sign up.

March 29, 2024, 04:47:34 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,105
  • Total Topics: 21,213
  • Online today: 292
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 296
  • Total: 296

[Block] Articles, simple index

Started by Freddy, September 19, 2010, 04:03:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Freddy

Name of Snippet: Articles, Simple Index
SMF/TP versions tested:
SMF1/TinyPortal v1.0.5 beta 1
SMF1/TinyPortal 1.0 beta 5-2
SMF 2.0 RC3/TinyPortal 1.0 beta 5.2
Block Type: php block or php article
Author: Freddy
Link to Discussion: http://www.tinyportal.net/index.php?topic=31110.0
Other Requirements: none
Description:

This will simply list the articles in the categories by date.  There's no pagination or anything so if you have hundreds of articles then you'd probably want to try something else - I believe there are other blocks that do this, so try searching.

I use this in a PHP article and simply link to the page from a menu.

You could change the SQL 'ORDER BY date DESC' to 'ORDER BY subject' and make it alphabetical if you want that instead.

Freddy

Here's the code for SMF 1, just customise the settings and text as you wish.


// Configuration

// Specify your categories, comma separated if more than one category.
$categories = array(3,10);

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">
<h3>Articles Index</h3>
<p>A short introduction can go in here...maybe an image too.</p>
</div>
<br />';

// Now for the code...
global $scripturl, $db_prefix;

$cats = implode(', ', $categories);

$request = db_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) AND off = 0 ORDER BY date DESC", __FILE__, __LINE__);

echo '
<table class="tborder" width="100%">
<tr class="titlebg">
<td>Subject</td>
<td>Date</td>
<td align="center">Views</td>
</tr>';

while ($row = mysql_fetch_assoc($request))
{
if ($row['shortname'])
{
$pageid = $row['shortname'];
}
else
{
$pageid = $row['id'];
}

echo '
<tr class="windowbg">
<td style="padding: 3px 0px 3px 10px"><strong><a href="' , $scripturl, '?page=' , $pageid , '">' , $row['subject'] , '</a></strong></td>
<td style="padding: 3px 0px 3px 10px">' , date('jS, F Y', $row['date']) , '</td>
<td align="center" style="padding: 3px 0px 3px 15px">' , $row['views'] , '</td>
</tr>
';
}

echo '
</table>';

mysql_free_result($request);

Freddy

Here's the code for SMF 2, just customise the settings and text as you wish.


// Configuration

// Specify your categories, comma separated if more than one category.
$categories = array(3,10);

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">
<h3>Articles Index</h3>
<p>A short introduction can go in here...maybe an image too.</p>
</div>
<br />';

// Now for the code...
global $scripturl, $db_prefix;

$cats = implode(', ', $categories);

$request = tpdb_query("SELECT id, date, subject, views, shortname FROM {$db_prefix}tp_articles WHERE category IN ($cats) AND off = 0 ORDER BY date DESC", __FILE__, __LINE__);

echo '
<table class="tborder" width="100%">
<tr class="titlebg">
<td>Subject</td>
<td>Date</td>
<td align="center">Views</td>
</tr>';

while ($row = mysql_fetch_assoc($request))
{
if ($row['shortname'])
{
$pageid = $row['shortname'];
}
else
{
$pageid = $row['id'];
}

echo '
<tr class="windowbg">
<td style="padding: 3px 0px 3px 10px"><strong><a href="' , $scripturl, '?page=' , $pageid , '">' , $row['subject'] , '</a></strong></td>
<td style="padding: 3px 0px 3px 10px">' , date('jS, F Y', $row['date']) , '</td>
<td align="center" style="padding: 3px 0px 3px 15px">' , $row['views'] , '</td>
</tr>
';
}

echo '
</table>';

mysql_free_result($request);

Freddy

Thanks to IcBin and Lurkalot here is the code for SMF2 & TP1

No demo but image is attached below...


// Configuration

// Specify your categories, comma separated if more than one category.
$categories = array(1,3,10);

// The heading area.

echo '
<div style="text-align: center; border: 1px solid #242526; ">   
   <h3>Articles Index</h3>
   <p>This is a list of articles, submitted by our members.</p>
</div>
<br />';

// Now for the code...
global $scripturl, $db_prefix, $smcFunc;

$request = $smcFunc['db_query']('', '
SELECT id, date, subject, views, shortname
FROM {db_prefix}tp_articles
WHERE category IN ({array_int:cats})
AND off = 0
ORDER BY date DESC',
array('cats' => $categories)
);

echo '
<table class="tborder" width="100%">
   <tr class="titlebg">
      <td>Subject</td>
      <td>Date</td>
      <td align="center">Views</td>
   </tr>';

while ($row = $smcFunc['db_fetch_assoc']($request))
{
   if ($row['shortname'])
   {
      $pageid = $row['shortname'];
   }
   else
   {
      $pageid = $row['id'];
   }

   echo '
   <tr class="windowbg">
      <td style="padding: 3px 0px 3px 10px"><strong><a href="' , $scripturl, '?page=' , $pageid , '">' , $row['subject'] , '</a></strong></td>
      <td style="padding: 3px 0px 3px 10px">' , date('jS, F Y', $row['date']) , '</td>
      <td align="center" style="padding: 3px 0px 3px 15px">' , $row['views'] , '</td>
   </tr>
';
}

echo '
</table>';

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