News:

Wondering if this will always be free?  See why free is better.

Main Menu

Calling Up Boards On SMF Forum With PHP Code

Started by corpersforum, August 26, 2018, 02:27:15 PM

Previous topic - Next topic

corpersforum

Hello everyone, I need help customizing my SMF forum and I hope am not posting this on the wrong board.

I want to call up topics from several boards with PHP code, but the first board is the only one working. I don't know if there is something am missing or getting wrong. let me show the code below:

Here is the PHP code am using to call topics from ONE board


global $smcFunc, $scripturl, $modSettings;
[size=10pt]$id_board = 1;

$request = $smcFunc['db_query']('', '
   SELECT t.id_topic, m.subject
   FROM {db_prefix}topics AS t
      INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
   WHERE t.id_board = {int:board}' . ($modSettings['postmod_active'] ? '
      AND t.approved = {int:approved}
      AND m.approved = {int:approved}' : '') . '
   ORDER BY t.id_topic DESC
   LIMIT {int:limit}',
   array(
      'board' => $id_board=66.0,
      'approved' => 1,
      'limit' => 18,
   )
);
$topics = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
   $topics[$row['id_topic']] = $row['subject'];
$smcFunc['db_free_result']($request);

echo '
<center><ul>';

if (empty($topics))
{
   echo '
   <li>There aren\'t any topics.</li>';
}
else
{
   foreach ($topics as $id => $subject)
      echo '
   <li><a href="', $scripturl, '?topic=', $id, '.0">', $subject, '</a></li>';
}

echo '
</ul></center>';


Here is the code that specified the board and the number of topics (from above code)


[size=10pt]   array(
      'board' => $id_board=66.0,
      'approved' => 1,
      'limit' => 18,[/size]


For me to add more boards, i think am supposed to just add the board's ID to the above like this below:


[size=10pt]   array(
      'board' => $id_board=66.0, [b]31.0, 40.0, 3.0,[/b]
      'approved' => 1,
      'limit' => 18,[/size]


But I realized after adding the board's ID like above, the topics are still called up from the first board. And i want the topics to be called from several boards from the forum.

Please, I will be happy if the genius here can show me what am missing.

Thanks in advance!

SychO

who told you you could do this
Quote'board' => $id_board=66.0, 31.0, 40.0, 3.0,

There's a number of ways you could achieve this, I'd use the IN operator

<?php

$id_board 
= array(66.031.040.03.0);

$request $smcFunc['db_query']('''
   SELECT t.id_topic, m.subject
   FROM {db_prefix}topics AS t
      INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
   WHERE t.id_board IN ('
.implode(','$id_board).')' . ($modSettings['postmod_active'] ? '
      AND t.approved = {int:approved}
      AND m.approved = {int:approved}' 
'') . '
   ORDER BY t.id_topic DESC
   LIMIT {int:limit}'
,
   array(
      
'approved' => 1,
      
'limit' => 18,
   )
);
$topics = array();
while (
$row $smcFunc['db_fetch_assoc']($request))
   
$topics[$row['id_topic']] = $row['subject'];
$smcFunc['db_free_result']($request);

?>
Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

corpersforum

Quote from: SychO on August 26, 2018, 02:46:55 PM
who told you you could do this
Quote'board' => $id_board=66.0, 31.0, 40.0, 3.0,

There's a number of ways you could achieve this, I'd use the IN operator

<?php

$id_board 
= array(66.031.040.03.0);

$request $smcFunc['db_query']('''
   SELECT t.id_topic, m.subject
   FROM {db_prefix}topics AS t
      INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
   WHERE t.id_board IN ('
.implode(','$id_board).')' . ($modSettings['postmod_active'] ? '
      AND t.approved = {int:approved}
      AND m.approved = {int:approved}' 
'') . '
   ORDER BY t.id_topic DESC
   LIMIT {int:limit}'
,
   array(
      
'approved' => 1,
      
'limit' => 18,
   )
);
$topics = array();
while (
$row $smcFunc['db_fetch_assoc']($request))
   
$topics[$row['id_topic']] = $row['subject'];
$smcFunc['db_free_result']($request);

?>


really appreciate the reply. this is what am getting below:

Function name must be a string

SychO

Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

corpersforum


corpersforum


SychO

Where are you putting the code ?
Can you paste the whole code ?
Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

Kindred

why not just use the SSI functions?

There are already existing functions that do exactly this...

<?php ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo'); ?>


so...


<?php 
require_once('SSI.php');

$board_list = array(6631403);

ssi_recentTopics(18,null,$board_list,'echo'); 
?>




job done.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

corpersforum

Quote from: Kindred on August 26, 2018, 05:08:41 PM
why not just use the SSI functions?

There are already existing functions that do exactly this...

<?php ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo'); ?>


so...


<?php 
require_once('SSI.php');

$board_list = array(6631403);

ssi_recentTopics(18,null,$board_list,'echo'); 
?>




job done.
I really appreciate your contribution. But the way the topics looks like is not really what i want. example below:
Quote[News & Updates]   Gernot Rohr: I Rejected 'Good Offers' To Continue As Nigeria Coach by apeiron24   Today at 06:05:32 PM
[Jobs & Vacancies]   Territory Sales Assistant by Belteshazzar  New   Today at 11:00:24 AM
[Jobs & Vacancies]   Cashier Needed by Belteshazzar  New   Today at 10:58:39 AM
[News & Updates]   Masquerade Beats Okada Man Into Coma In Ondo Community Over N1000 Gift by apeiron24   August 25, 2018, 02:57:30 PM
[Jobs & Vacancies]   Jumia Nigeria Job Recruitment (4 Positions) by apeiron24   August 25, 2018, 01:21:06 AM
[Jobs & Vacancies]   Director of Sales & Partnership - West Africa at LEAD Enterprise Support Company by apeiron24   August 25, 2018, 01:02:53 AM

i will love to remove the name of the board and the date. I will love to display it with list style. Please i still need help

corpersforum

Quote from: SychO on August 26, 2018, 03:36:57 PM
Where are you putting the code ?
Can you paste the whole code ?

Whole code is saying "Function name must be a string"

Kindred

That can still be done with the ssi function.

Seriously, read the online manual (link in the top menu) which discusses how to use ssi functions to return an array of data which can then be output in any format that you want
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Mick.


corpersforum

Quote from: Kindred on August 26, 2018, 09:06:02 PM
That can still be done with the ssi function.

Seriously, read the online manual (link in the top menu) which discusses how to use ssi functions to return an array of data which can then be output in any format that you want

Dear Kindred, I am grateful for your contribution. Am a newbie and after reading the whole 9 pages I saw about SSI in the online manual, I still don't get how to fix this. Please help me.

corpersforum

Quote from: Mick. on August 26, 2018, 10:35:04 PM
Is this you? https://www.corpersforum.com/forum/ If so, this is what you're looking for https://simpleportal.net/index.php?topic=9138.0

Hello Mick, thanks for the support. I don't think that's what am looking for. I am looking for a ways to do this attachment below but with topics from different boards.

corpersforum


SychO

Open SSI.php look for ssi_recentTopics() function, edit the html

use the function

Quote from: Kindred on August 26, 2018, 05:08:41 PM
why not just use the SSI functions?

There are already existing functions that do exactly this...

<?php ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo'); ?>


so...


<?php 
require_once('SSI.php');

$board_list = array(6631403);

ssi_recentTopics(18,null,$board_list,'echo'); 
?>




job done.
Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

Mick.

#16
Quote from: corpersforum on August 27, 2018, 02:29:47 AM
Quote from: Mick. on August 26, 2018, 10:35:04 PM
Is this you? https://www.corpersforum.com/forum/ If so, this is what you're looking for https://simpleportal.net/index.php?topic=9138.0

Hello Mick, thanks for the support. I don't think that's what am looking for. I am looking for a ways to do this attachment below but with topics from different boards.

...You want to show the lat 10 topics of board A, the last 10 topics of board B, the last 10 topics of board C, correct? No poster name, no date and time correct? If so, copy and paste the code in a php block in either top or bottom php block. For board A, exclude all boards but board A. Create another php block paste code and exclude all boards but B and do it again for C,D,F etc.  I've modified this code to show it like that.


(bad code removed)


Replace the boards 1 thru 10 $exclude_boards = array(1,2,3,4,5,6,7,8,9,10); with the board numbers you dont want to show in that block

Mick.

I can't modify my posts so i'm posting the code again since i noticed i left a div in the code. Here it is fixed.

/*
Block: Recent posts with post preview
Author: Blue @ Simple Portal.net
Version: 1.6
*/

/* [SETUP WHAT YOU WANT HERE] */

$topics_posts = 1; // TOPICS - 1 | MESSAGES - 2
$limit = 5; // How many recent posts do you want to output?
$number = 70; // How many characters do you want to output?
$exclude_boards = array(1,2,3,4,5,6,7,8,9,10); // IF null guests can see all boards | IF you want to hide some boards use: array( ID1, ID2, ID3) where ID is the board's ID
$htmlspecialcharacters = false; // Do you need to show html special characters like Greek characters?

// Scroll Enable? Height and Speed?
$scrolling = 2;       // ENABLE - 1 | DISABLE - 2
$speed = 3;           // SLOW - 1 | MEDIUM - 10 | FAST - 20
$height = "150px";
$scrollbar = 0;       // ENABLE - 1 | DISABLE - 2


// Do you want to translate it to your own language? :P
$text['sportal_false'] = 'Simple Portal not found';
$text['board'] = 'in';
$text['who'] = 'by';

/* [STOP!] - THIS IS THE END OF SETUP */

//Only code from now on ;)
global $smcFunc, $scripturl, $sourcedir, $modSettings, $user_info, $settings, $context;

// Lets see if you are using Simple Portal. If not...well...go get them :D
if (!file_exists($sourcedir . '/PortalBlocks.php'))
{
echo $text['sportal_false'];
return;
}

// Let's grab some database results
if ($topics_posts == 1) {

if (is_array($exclude_boards) || (int) $exclude_boards === $exclude_boards)
{
$exclude_boards = is_array($exclude_boards) ? $exclude_boards : array($exclude_boards);
}
elseif ($exclude_boards != null)
{
$output_method = $exclude_boards;
$exclude_boards = array();
}

$posts_result = $smcFunc['db_query']('', '
   SELECT m.poster_name, m.poster_time, m.id_msg, t.id_member_updated, m.subject, m.body, m.id_topic, b.name, t.id_last_msg, u.avatar, g.online_color,' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . '
   FROM {db_prefix}topics AS t
   LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
   LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
   LEFT JOIN {db_prefix}members AS u ON (t.id_member_updated = u.id_member)
   LEFT JOIN {db_prefix}membergroups AS g ON (g.id_group = CASE WHEN u.id_group = 0 THEN u.id_post_group ELSE u.id_group END)
   ' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = ' . $user_info['id'] . ')
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = ' . $user_info['id'] . ')' : '') . '     
   WHERE m.approved=1'   . (empty($exclude_boards) ? '' : '
AND b.id_board NOT IN ({array_int:exclude_boards})') . ' AND {query_see_board}
   ORDER BY t.id_last_msg DESC
   LIMIT ' . $limit,
array(
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
)   
   );
$posts = array();
while ($row_posts = $smcFunc['db_fetch_assoc']($posts_result))
{

      global $memberContext;
      loadMemberData($row_posts['id_member_updated']);
      loadMemberContext($row_posts['id_member_updated']);

   $posts[] = array(
  'id' => $row_posts['id_member_updated'],
      'username' => '<a style=" href="' . $scripturl . '?action=profile;u=' . $row_posts['id_member_updated'] . '">' . $row_posts['poster_name'] . '</a>',
      'subject' => '<a style="font-weight: normal; margin-left: 1px; line-height: 25px;" title="' . $text['board'] . ' ' . $row_posts['name'] .'" href="' . $scripturl . '?topic=' . $row_posts['id_topic'] . '.msg' . $row_posts['id_last_msg'] . ';topicseen#new">' . $row_posts['subject'] . '</a>',
      'body' => $row_posts['body'],
  'avatar' => $row_posts['avatar'] == '' ? $memberContext[$row_posts['id_member_updated']]['avatar']['href'] : (stristr($row_posts['avatar'], 'http://') ? $row_posts['avatar'] : $modSettings['avatar_url'] . '/' . $row_posts['avatar']),
  'board' => $row_posts['name'],
  'time' => timeformat($row_posts['poster_time']),
  'new' => !empty($row_posts['is_read'])
   );
}
$smcFunc['db_free_result']($posts_result);

} else {

if (is_array($exclude_boards) || (int) $exclude_boards === $exclude_boards)
{
$exclude_boards = is_array($exclude_boards) ? $exclude_boards : array($exclude_boards);
}
elseif ($exclude_boards != null)
{
$output_method = $exclude_boards;
$exclude_boards = array();
}

$posts_result = $smcFunc['db_query']('', '
   SELECT m.poster_name, m.poster_time, m.id_msg, m.id_member, m.subject, m.body, m.id_topic, b.name, b.id_board, u.avatar, g.online_color,' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . '
   FROM {db_prefix}messages AS m
   LEFT JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
   LEFT JOIN {db_prefix}members AS u ON (m.id_member = u.id_member)
   LEFT JOIN {db_prefix}membergroups AS g ON (g.id_group = CASE WHEN u.id_group = 0 THEN u.id_post_group ELSE u.id_group END)
   ' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = ' . $user_info['id'] . ')
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = ' . $user_info['id'] . ')' : '') . '   
   WHERE m.approved=1'   . (empty($exclude_boards) ? '' : '
AND b.id_board NOT IN ({array_int:exclude_boards})') . ' AND {query_see_board}
   ORDER BY m.id_msg DESC
   LIMIT ' . $limit,
array(
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
)   
   );
$posts = array();
while ($row_posts = $smcFunc['db_fetch_assoc']($posts_result))
{

      global $memberContext;
      loadMemberData($row_posts['id_member']);
      loadMemberContext($row_posts['id_member']);

   $posts[] = array(
  'id' => $row_posts['id_member'],
      'username' => '<a style=" href="' . $scripturl . '?action=profile;u=' . $row_posts['id_member'] . '">' . $row_posts['poster_name'] . '</a>',
      'subject' => '<a style="font-weight: normal;" margin-left: 1px; line-height: 25px; title="' . $text['board'] . ' ' . $row_posts['name'] .'" href="' . $scripturl . '?topic=' . $row_posts['id_topic'] . '.msg' . $row_posts['id_msg'] . ';topicseen#new">' . $row_posts['subject'] . '</a>',
      'body' => $row_posts['body'],
  'avatar' => $row_posts['avatar'] == '' ? $memberContext[$row_posts['id_member']]['avatar']['href'] : (stristr($row_posts['avatar'], 'http://') ? $row_posts['avatar'] : $modSettings['avatar_url'] . '/' . $row_posts['avatar']),
  'board' => $row_posts['name'], 
  'time' => timeformat($row_posts['poster_time']),
  'new' => !empty($row_posts['is_read'])
   );
}
$smcFunc['db_free_result']($posts_result);

}


foreach ($posts as $post) {

// Lets fix the BBCode bug and Strip the Text
$content1 = str_replace("[", "<", $post['body']);
$content2 = str_replace("]", ">", $content1);
$content3 = strip_tags($content2);
$preview = substr ($content3,0,$number);

echo '

         <table>
<tr>
<td width="35%">' . str_ireplace('Re: ', '', $post['subject']);

echo'
</td>
</tr>
</table>';


}



Mick.

Here's a different approach and much easier using SSI.

$array = ssi_boardNews(15.0, 10, null, 250, 'array');

foreach ($array as $news)
{
echo '
<div align="center"><a href="', $news['href'], '">', $news['subject'], '</a></div>';


}


Where 15.0 change it to the board number you want to show. Change the 10 for how many posts you want to show. The 250 is the length of characters in a title.

If you want to show other boards,... well, copy and paste the code again with the different board number.


https://wiki.simplemachines.org/smf/SSI_FAQ_Advanced


Demo: https://www.idesignsmf.com/testing.php

Kindred

last one is best

there is no need to write new database calls and scripts.

SSI does it all.

and Sycho...  you should NEVER edit SSI like that. There is no need to do that because you can output the SSI data as an array and then display it as you like --  just like what Mick said in the last post.

the rest of Mick's posts should be ignored... :P   don't do that.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

SychO

Quote from: Kindred on August 27, 2018, 10:15:07 AM
last one is best

there is no need to write new database calls and scripts.

SSI does it all.

and Sycho...  you should NEVER edit SSI like that. There is no need to do that because you can output the SSI data as an array and then display it as you like --  just like what Mick said in the last post.

the rest of Mick's posts should be ignored... :P   don't do that.

yup I forgot about the output_method parameter, been a while since I last used ssi functions
Checkout My Themes:
-

Potato  •  Ackerman  •  SunRise  •  NightBreeze

Advertisement: