This is just some code I threw together that will loop through all posts in a specific board(s) that you specify with the board id value(s). Just figured that this code could be useful for some of you. To see this code working you can check it out here =>
EXAMPLE of THIS FEATURE.
Basically what this does in the
example page is it randomly selects a post body (linking to the actual post when clicked on) from board id = 1 (That is the 1st board) within the newforum SMF Forum.
Benefits of the code I am about to display that I will post up for both SMF 2.0.x and SMF 1.1.x versions:
- No need to refresh the page since this uses the setInterval Javascript function to write to the document using innerHTML.
- Compatible in ALL Major Browsers
- Easy to configure and customize for your own personal forum
Possible Setbacks:
- The more posts you have within the board(s), the longer the query will take thus the page could take a bit of time to load since it will load every post inside that board into an array and then copy that array over to a javascript array of values
- Would be less strain on the server to get the subject of the post and link to the actual message instead of getting the entire message body of the post, which can be done the same way.
License Information - All I ask is that you don't try to claim that you created this code! You may modify it however you like, and use it in any of your projects and release it, whatever suits you. Just that if you use the code in any of your projects, please post a link to graphicsmayhem.com somewhere. Otherwise if you use it for personal use or whatever, no need for this.
Ok, enough of that, so now I will get on with it. This code assumes that you have the PHP file (whatever name you decide to call it) within the same directory as SSI.php (that is your SMF Root Directory). You do not need to require the SSI.php file if you are using a template file within the Themes directory to display it. However, since we are doing it all within 1 PHP file, it is within this code.
For SMF 2.0.x Versions:
include ('SSI.php');
?>
TESTING 1 2 3
$boards = array(1); // or whatever
$query = $smcFunc['db_query']('', '
SELECT id_topic, id_msg, body
FROM {db_prefix}messages
WHERE id_board IN ({int:id_board})
ORDER BY NULL',
array(
'id_board' => implode(', ', $boards),
)
);
$entry = array();
while ($row = mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
$entry[] = '. $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" style="text-decoration: none">' . $row['body'] . '';
}
mysql_free_result($query);
echo '';
?>
For SMF 1.1.x Versions:
include ('SSI.php');
?>
TESTING 1 2 3
$boards = array(1); // or whatever
$query = db_query("
SELECT ID_TOPIC, ID_MSG, body
FROM {$db_prefix}messages
WHERE ID_BOARD IN (" . implode(', ', $boards) . ")
ORDER BY NULL
", __FILE__, __LINE__);
$entry = array();
while ($row = mysql_fetch_assoc($query)) {
$row['body'] = parse_bbc($row['body']);
$entry[] = '. $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#msg' . $row['ID_MSG'] . '" style="text-decoration: none">' . $row['body'] . '';
}
mysql_free_result($query);
echo '';
?>
Ok, now to explain a little bit on what is going on here. A php array gets created called $entry and fills all array values with the body of each post within that board, in this case board 1. Than we take all values from the $entry array and transfer them into the Javascript daEntries array which we defined. Than we want to get a random index for that array which we have the maximum bounds for the array to the minimum (which will always be 0). So we use the Math.random Javascript function to do this within the
doFunc() Function which gets called once upon loading of the page and every 3 seconds thereafter (window.setInterval"doFunc()",3000). This function writes to the div with an id="message" every 3 seconds updating with the content from the daEntries array. If you want to delay the post a bit longer, you can change 3000 to 4000, or more (ofcourse anyone who has done anything in JS will already know that this value is in Milliseconds so adding 1000 to it, will actually add 1 second onto the time).
Well, that's all Folks. And hopefully this code is a bit useful to someone. With the
Example Webpage, all I did was use a bit of CSS and outer DIVs to create the appearance that it has. This can easilly be done way better than it is now, but just there for displaying purposes only.
ENJOY