Hi Guys,
Been working on a php to view Recent posts on my main page. I'm pretty new to php, so this is pretty easy for you guys...
I started with this of course:
<?php
require_once('/absolute/path/to/SSI.php');
ssi_recentTopics();
?>
After updating the path accordingly, it does work, no problem.
So, I need to customize the output a bit, and after scouring the docs and this forum, I find this:
<?php ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo'); ?>
Seems grand, however, I cant get it to do what I want, lol. I only want to display recent topics from 2 specific subforums, and display that information as text-only (not links). My syntax is apparently incorrect, and I cannot find working examples of similar situations.
Any guidance would be appreciated.
Thanks,
Griffy
Can't you just use the url for recent topics and add "board=ID;" for the desired board (with commas separating multiple boards) or "c=ID;" for categories?
Edit: Just noticed you want them displayed on Main page. Thinking again, maybe it's not that easy. :P
No... The ssi for recent topics has it built in to do exactly what you are asking.
Include_boards...
Array(3,5) where the 3 and 5 are the board ids
As for text no links...
Output = block
So....
$boardlist = array(3,5);
$my_recent = ssi_recentTopics(8, NULL, $boardlist, 'block');
For each $topic in $my_recent
{
Read out the contents that you want from the array... Don't include links if you don't want
}
Thanks Kindred, that helps. My initial syntax was definitely wrong. Using your advice, I got this far...
<?php
$boardlist=Array(5,10);
require_once('/data/www/XXXXXXXX.com/forums/SSI.php');
$recentposts = ssi_recentTopics($num_recent = 10, $exclude_boards = null, $include_boards = $boardlist, $output_method = 'block');
foreach $topic in $recentposts
?>
I've got it partially working, but you lost me a little at reading the contents of the array. I'm attempting a foreach/in, but I havent been able to produce the array elements for subject, new, poster, time. I assume this can be done with print_r ? The various iterations I've tried have not worked, but that is due to my syntax ignorance.
Sorry for the lame questions, but I am very new to php. This kind of got dumped on me, so I'm fixing this last thing on this particular website.
The assist is much appreciated :)
well, first things first...
although it doesn't hurt, you don't really need to define the variables in every call for the function
($num_recent = 10, $exclude_boards = null, $include_boards = $boardlist, $output_method = 'block')
can just be written
(10, null, $boardlist, 'block')
then, on to the coding...
Although I am not near a computer where I can call it out specifically for you right now, I will try to explain.
If you look in SSI.php, you will note the function ssi_recentTopics actually calls out a whole bunch of stuff, near the bottom of the function.
Each of the variables which was read out of the database can be sent as output with code similar to
$topic['subject']
ok.... just got to a real computer.
<?php
require_once('/data/www/XXXXXXXX.com/forums/SSI.php');
global $txt;
$boardlist=Array(5,10);
$recentposts = ssi_recentTopics(10, null, $boardlist, 'block');
echo '
<div class="ssi_table">';
foreach $topic in $recentposts
echo '
<div style="text-align:right;vertical-align:top;">
', $topic ['subject'], '
', $txt['by'], ' ', $topic ['poster']['name'], '
', $txt['at'], ' ', $topic ['time'], '
', $txt['in'], ' ', $post['board']['name'], '
</div>';
echo '
</div>';
here's the whole possible array
// Build the array.
$posts[] = array(
'board' => array(
'id' => '...',
'name' => '...',
'href' => '...',
'link' => '...'
),
'topic' => '...',
'poster' => array(
'id' => '...',
'name' => '...',
'href' => '...',
'link' => '...'
),
'subject' => '...',
'replies' => '...',
'views' => '...',
'short_subject' => '...',
'preview' => '...',
'time' => '...',
'timestamp' => '...',
'href' => '...',
'link' => '...',
// Retained for compatibility - is technically incorrect!
'new' => '...',
'is_new' => '...',
'new_from' => '...',
'icon' => '...',
);
Oh geez... I see what you mean. You actually have to "build" the entire output the way you want it, using just the array elements you want.
Thanks Kindred, that was a very valuable lesson. I would have not figured that out on my own, at least not quickly.
Thank you. Much appreciated my friend :)
Well, if its not one thing, its another.... lol
Wondering if I have a permissions issue on the new php file I created. Or is there another mechanism withim SMF that would prevent this from working?
"Parse error: syntax error, unexpected '$topic' (T_VARIABLE), expecting '(' in blah/blah/blah..... "
what code are you using?
not a permissions issue... :)
just a malformed php statement -- probably within the echo.
As Illori said, please provide the exact code that you are now using?
Oops, sorry guys. Its based on what Kindred gave me earlier. I currently have a .php file of this in the ../forums dir, just for testing. The forum is on 2.0.11. My idea was to call this file in an iframe on the main page.
<?php
require_once('/data/www/xxxxxxxxxxxxx/forums/SSI.php');
global $txt;
$boardlist=Array(5,10);
$recentposts = ssi_recentTopics(10, null, $boardlist, 'block');
echo '
<div class="ssi_table">';
foreach $topic in $recentposts
echo '
<div style="text-align:right;vertical-align:top;">
', $topic ['subject'], '
', $txt['by'], ' ', $topic ['poster']['name'], '
', $txt['at'], ' ', $topic ['time'], '
', $txt['in'], ' ', $post['board']['name'], '
</div>';
echo '
</div>';
Change the "foreach $topic in $recentposts" as follows:
<?php
require_once('/data/www/xxxxxxxxxxxxx/forums/SSI.php');
global $txt;
$boardlist=Array(5,10);
$recentposts = ssi_recentTopics(10, null, $boardlist, 'block');
echo '
<div class="ssi_table">';
foreach ($recentposts as $topic)
echo '
<div style="text-align:right;vertical-align:top;">
', $topic ['subject'], '
', $txt['by'], ' ', $topic ['poster']['name'], '
', $txt['at'], ' ', $topic ['time'], '
', $txt['in'], ' ', $post['board']['name'], '
</div>';
echo '
</div>';
oops,
yeah, the foreach needs parens... forgot about that
also noticed that I used the old notation in the foreach (post instead of topic)
<?php
require_once('/data/www/xxxxxxxxxxxxx/forums/SSI.php');
global $txt;
$boardlist=Array(5,10);
$recentposts = ssi_recentTopics(10, null, $boardlist, 'block');
echo '
<div class="ssi_table">';
foreach ($recentposts as $topic)
echo '
<div style="text-align:right;vertical-align:top;">
', $topic ['subject'], '
', $txt['by'], ' ', $topic ['poster']['name'], '
', $txt['at'], ' ', $topic ['time'], '
', $txt['in'], ' ', $topic ['board']['name'], '
</div>';
echo '
</div>';
Thanks Kindred, and everyone else who commented. It makes more sense to me now. Slowly but surely, I'm starting to see how the logic works.
Its working now, as intended. Really appreciate the help! Hopefully this will help others too :)