News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Request certain boards only on function

Started by Pipke, December 02, 2014, 02:10:03 PM

Previous topic - Next topic

Pipke

i could do in the foreach loop this

if ($post['board']['id'] != 1)
        continue;
and
if ($post['board']['id'] != 2)
        continue;

but thats not a nice way to do it i think

so instead i'm trying something using this --> $_REQUEST['boards'] = array(1,2); so that is shows only topics from board 1 and 2
getting errors in adminlog:

The database value you're trying to insert does not exist: boards
Function: rft_mainbasic
and
Invalid argument supplied for foreach()

and
explode() expects parameter 2 to be string, array given

who can help me out, my function sofar:
function rft_mainbasic()
{
    global $txt, $settings, $scripturl, $user_info, $context, $modSettings, $sourcedir, $board, $boarddir, $smcFunc;
   
    $rft_per_page = !empty($modSettings['rft_numbertopics']) ? $modSettings['rft_numbertopics'] : 10;
   
    $_REQUEST['boards'] = array(1,2);   
   
    if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95)
        $_REQUEST['start'] = 95;

    $query_parameters = array();
    if (!empty($_REQUEST['c']) && empty($board))
    {
        $_REQUEST['c'] = explode(',', $_REQUEST['c']);
        foreach ($_REQUEST['c'] as $i => $c)
            $_REQUEST['c'][$i] = (int) $c;

        if (count($_REQUEST['c']) == 1)
        {
            $request = $smcFunc['db_query']('', '
                SELECT name
                FROM {db_prefix}categories
                WHERE id_cat = {int:id_cat}
                LIMIT 1',
                array(
                    'id_cat' => $_REQUEST['c'][0],
                )
            );
            list ($name) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);

            if (empty($name))
                fatal_lang_error('no_access', false);

            $context['linktree'][] = array(
                'url' => $scripturl . '#c' . (int) $_REQUEST['c'],
                'name' => $name
            );
        }

        $request = $smcFunc['db_query']('', '
            SELECT b.id_board, b.num_topics
            FROM {db_prefix}boards AS b
            WHERE b.id_cat IN ({array_int:category_list})
                AND {query_see_board}',
            array(
                'category_list' => $_REQUEST['c'],
            )
        );
        $total_cat_posts = 0;
        $boards = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $boards[] = $row['id_board'];
            $total_cat_posts += $row['num_topics'];
        }
        $smcFunc['db_free_result']($request);

        if (empty($boards))
            fatal_lang_error('error_no_boards_selected');

        $query_this_board = 'b.id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;
        // If this category has a significant number of posts in it...
        if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
        {
            $query_this_board .= '
                    AND m.id_msg >= {int:max_id_msg}';
            $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7);
        }

        $context['page_index'] = constructPageIndex($scripturl . '?action=forum;c=' . implode(',', $_REQUEST['c']), $_REQUEST['start'], min(100, $total_cat_posts), $rft_per_page, false);
    }
    elseif (!empty($_REQUEST['boards']))
    {
        $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
        foreach ($_REQUEST['boards'] as $i => $b)
            $_REQUEST['boards'][$i] = (int) $b;

        $request = $smcFunc['db_query']('', '
            SELECT b.id_board, b.num_topics
            FROM {db_prefix}boards AS b
            WHERE b.id_board IN ({array_int:boards})
                AND {query_see_board}
            LIMIT {int:limit}',
            array(
                'board_list' => $_REQUEST['boards'],
                'limit' => count($_REQUEST['boards']),
            )
        );
        $total_posts = 0;
        $boards = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $boards[] = $row['id_board'];
            $total_posts += $row['num_topics'];
        }
        $smcFunc['db_free_result']($request);

        if (empty($boards))
            fatal_lang_error('error_no_boards_selected');

        $query_this_board = 'b.id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;

        // If these boards have a significant number of posts in them...
        if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
        {
            $query_this_board .= '
                    AND m.id_msg >= {int:max_id_msg}';
            $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9);
        }

        $context['page_index'] = constructPageIndex($scripturl . '?action=forum;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), $rft_per_page, false);
    }
    elseif (!empty($board))
    {
        $request = $smcFunc['db_query']('', '
            SELECT num_topics
            FROM {db_prefix}boards
            WHERE id_board = {int:current_board}
            LIMIT 1',
            array(
                'current_board' => $board,
            )
        );
        list ($total_posts) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);

        $query_this_board = 'b.id_board = {int:board}';
        $query_parameters['board'] = $board;

        // If this board has a significant number of posts in it...
        if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
        {
            $query_this_board .= '
                    AND m.id_msg >= {int:max_id_msg}';
            $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10);
        }

        $context['page_index'] = constructPageIndex($scripturl . '?action=forum;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $total_posts), $rft_per_page, true);
    }
    else
    {
        $query_this_board = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
                    AND b.id_board != {int:recycle_board}' : ''). '
                    AND m.id_msg >= {int:max_id_msg}';
        $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6);
        $query_parameters['recycle_board'] = $modSettings['recycle_board'];

        $context['page_index'] = constructPageIndex($scripturl . '?action=forum', $_REQUEST['start'], min(100, $modSettings['totalTopics']), $rft_per_page, false);
    }

    $key = 'recent-' . $user_info['id'] . '-' . md5(serialize(array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start'];
   
            if (($messages = cache_get_data($key, 120)) == false)
    {
        $done = false;
        while (!$done)
        {
            // Find the ?? most recent messages they can *view*.
            // !!!SLOW This query is really slow still, probably?
            $request = $smcFunc['db_query']('', '
                SELECT m.id_msg
                FROM {db_prefix}messages AS m
                    INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
                    INNER JOIN {db_prefix}topics AS t ON (t.' . (!empty($modSettings['rft_subject_topics']) && $modSettings['rft_subject_topics'] == 'last' ? 'id_last_msg' : 'id_first_msg') . ' = m.id_msg)
                    WHERE ' . $query_this_board . '
                    AND m.approved = {int:is_approved}
                ORDER BY m.id_msg DESC
                LIMIT {int:offset}, {int:limit}',
                array_merge($query_parameters, array(
                    'is_approved' => 1,
                    'offset' => $_REQUEST['start'],
                    'limit' => $rft_per_page,
                ))
            );
           
            // If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result.
            if (isset($query_parameters['max_id_msg']) && $smcFunc['db_num_rows']($request) < 7)
            {
                $smcFunc['db_free_result']($request);
                $query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board);
                $cache_results = true;
                unset($query_parameters['max_id_msg']);
            }
            else
                $done = true;
        }
        $messages = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
            $messages[] = $row['id_msg'];
        $smcFunc['db_free_result']($request);
        if (!empty($cache_results))
            cache_put_data($key, $messages, 120);
    }

    // Nothing here... Or at least, nothing you can see...
    if (empty($messages))
    {
        $context['posts'] = array();
        return;
    }
   
   
    // Check if Column 'is_solved' exists from the Topic Solved Mod
    $post['is_solved'] = false; // to be sure
    $tableName = 'topics';
    $columnName = 'is_solved';
    $solved_exists = false;
    $solved_exists = rft_checkColumn($tableName, $columnName);
   
    // Get all the most recent posts
    $request = $smcFunc['db_query']('', '
        SELECT
            m.id_msg, m.id_msg_modified, m.subject, m.smileys_enabled, m.poster_time AS first_poster_time , m.body, m.id_topic, t.id_board, b.id_cat,
            b.name AS bname, c.name AS cname, t.num_replies, t.num_views, m.id_member, m2.id_member AS id_first_member,' . ($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') . ',
            m2.poster_time AS last_poster_time, m.icon AS first_icon,
            t.approved, t.unapproved_posts, t.locked, t.is_sticky, t.id_poll,' . ($solved_exists ? ' t.is_solved,' : '') . '
            IFNULL(mem2.real_name, m2.poster_name) AS first_poster_name, t.id_first_msg,
            IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_last_msg
        FROM {db_prefix}messages AS m
            INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
            INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
            INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_first_msg)
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
            LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)' . (!$user_info['is_guest'] ? '
            LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
            LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
        WHERE m.id_msg IN ({array_int:message_list})
        ORDER BY m.id_msg DESC
        LIMIT ' . count($messages),
    array(
        'message_list' => $messages,
        'current_member' => $user_info['id'],
    )
);
$counter = $_REQUEST['start'] + 1;
$context['posts'] = array();
$board_ids = array('own' => array(), 'any' => array());
$topics = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{

    // Censor everything.
    censorText($row['subject']);

    // BBC-atize the message.
    $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

    $topics[] = $row['id_topic'];
   
    // And build the array.
    $context['posts'][$row['id_msg']] = array(
        'id' => $row['id_msg'],
        'counter' => $counter++,
        'alternate' => $counter % 2,
        'category' => array(
            'id' => $row['id_cat'],
            'name' => $row['cname'],
            'href' => $scripturl . '#c' . $row['id_cat'],
            'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
        ),
        'board' => array(
            'id' => $row['id_board'],
            'name' => $row['bname'],
            'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
            'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
        ),
        'topic' => $row['id_topic'],
        'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
        'linklast' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
        'linkfirst' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0" rel="nofollow">' . $row['subject'] . '</a>',
        'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new',
        'start' => $row['num_replies'],
        'subject' => $row['subject'],
        'views' => comma_format($row['num_views']),
        'replies' => comma_format($row['num_replies']),
        'timefirst' => timeformat($row['first_poster_time']),
        'timelast' => timeformat($row['last_poster_time']),
        'first_poster' => array(
            'id' => $row['id_first_member'],
            'name' => $row['first_poster_name'],
            'href' => empty($row['id_first_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_first_member'],
            'link' => empty($row['id_first_member']) ? $row['first_poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '">' . $row['first_poster_name'] . '</a>'
        ),
        'poster' => array(
            'id' => $row['id_member'],
            'name' => $row['poster_name'],
            'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
            'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
        ),
        'is_own_post' => false,
        'new' => $row['new_from'] <= $row['id_msg_modified'],
        'new_from' => $row['new_from'],
        'newtime' => $row['new_from'],
        'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new" rel="nofollow"><img src="' . $settings['lang_images_url'] . '/new.gif" alt="' . $txt['new'],
        //'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
        'id_msg_modified' => $row['id_msg_modified'],
        'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']),
        'is_locked' => !empty($row['locked']),
        'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
        'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'],
        'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'],
        'icon' => $row['first_icon'],
    );
    if (!empty($row['is_solved']))
        $context['posts'][$row['id_msg']]['is_solved'] = $row['is_solved'];
    if ($user_info['id'] == $row['id_first_member'])
        $board_ids['own'][$row['id_board']][] = $row['id_msg'];
    $board_ids['any'][$row['id_board']][] = $row['id_msg'];
}

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

    $request = $smcFunc['db_query']('', '
        SELECT DISTINCT id_topic
        FROM {db_prefix}messages
        WHERE id_topic IN ({array_int:topics})
        AND id_member = {int:member}',
    array(
        'topics' => $topics,
        'member' => $user_info['id'],
        )
    );
    $context['posted_topics'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $context['posted_topics'][] = $row['id_topic'];
   
    $smcFunc['db_free_result']($request);

}
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Kindred

WHat are you actually trying to do?

Pull posts or threads form a specific board for use on a page outside of SMF?

Look at
ssi_recentPosts(); and/or ssi_recentTopics();
Сл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."

Pipke

Quote from: Kindred on December 02, 2014, 02:31:31 PM
WHat are you actually trying to do?


Pull topics form specific boards
with this
$_REQUEST['boards'] = array(1,2);
wich goes to this code

elseif (!empty($_REQUEST['boards']))
    {
        $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
        foreach ($_REQUEST['boards'] as $i => $b)
            $_REQUEST['boards'][$i] = (int) $b;

        $request = $smcFunc['db_query']('', '
            SELECT b.id_board, b.num_topics
            FROM {db_prefix}boards AS b
            WHERE b.id_board IN ({array_int:boards})
                AND {query_see_board}
            LIMIT {int:limit}',
            array(
                'board_list' => $_REQUEST['boards'],
                'limit' => count($_REQUEST['boards']),
            )
        );


but dont know what i do wrong, see the errors above wich i posted.
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Kindred

no....   don't do that.   Don't assume that you know the best way to code something...


Tell us what you are actually trying to DO...   as in - what do you want the final outcome to be/display?

Is it what I suggested?
You are trying to pull in the most recent posts from a specific board (or boards) onto a non-smf page?
Сл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."

Pipke

got it working with this

$boards = array();
      $boards[] = 1;
      $boards[] = 2;
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Advertisement: