TinyPortal

Development => Block Codes => Latest block snippets => Topic started by: Freddy on September 19, 2010, 04:03:57 PM

Title: [Block] Articles, simple index
Post by: Freddy on September 19, 2010, 04:03:57 PM
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.
Title: Re: [Block] Articles, simple index
Post by: Freddy on September 19, 2010, 04:05:37 PM
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);
Title: Re: [Block] Articles, simple index
Post by: Freddy on September 19, 2010, 04:06:06 PM
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);
Title: Re: [Block] Articles, simple index
Post by: Freddy on October 27, 2011, 04:45:04 PM
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);