Members: 186  ?  Posts: 8930  ?  Topics: 751  ?  Please welcome clone, our newest member.
Please login or register.

Login with username, password and session length
 

News:

Welcome To Graphics Mayhem

collapse

Author Topic: Loop through posts from any given Board at a Set Interval  (Read 35 times)

0 Members and 1 Guest are viewing this topic.

SoLoGHoST

  • SoLoGHoSt
  • Connoisseur
  • Offline
  • Posts: 1741
  • SMF MOD Expert!
  • OS:
  • Windows Vista/Server 2008
  • Browser:
  • MS Internet Explorer 8.0
Loop through posts from any given Board at a Set Interval
« on: September 10, 2009, 10:30:26 AM »
    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:

    Code: [Select]
    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 
    '
    var daEntries = new Array();
    '
    ;

    foreach (
    $entry as $key => $value)
    echo '
    daEntries[' 
    $key '] = \'' addslashes($value) . '\';';

    echo 
    '

    var maxEntries = \'' 
    . (count($entry)-1) . '\';
    maxEntries = parseInt(maxEntries);
    var minEntries = 0;

    function doFunc()
    {
    var randNum = Math.random()*(maxEntries-minEntries);
    randNum = Math.round(minEntries+randNum);

    document.getElementById("message").innerHTML = \'\';
    document.getElementById("message").innerHTML = daEntries[randNum];
    }

    function doIt()
    {
    doFunc();
    window.setInterval("doFunc()",3000);
    }
    '
    ;

    ?>



     



    For SMF 1.1.x Versions:
    Code: [Select]
    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 
    '
    var daEntries = new Array();
    '
    ;

    foreach (
    $entry as $key => $value)
    echo '
    daEntries[' 
    $key '] = \'' addslashes($value) . '\';';

    echo 
    '

    var maxEntries = \'' 
    . (count($entry)-1) . '\';
    maxEntries = parseInt(maxEntries);
    var minEntries = 0;

    function doFunc()
    {
    var randNum = Math.random()*(maxEntries-minEntries);
    randNum = Math.round(minEntries+randNum);

    document.getElementById("message").innerHTML = \'\';
    document.getElementById("message").innerHTML = daEntries[randNum];
    }

    function doIt()
    {
    doFunc();
    window.setInterval("doFunc()",3000);
    }
    '
    ;

    ?>



     



    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 :)

    « Last Edit: September 10, 2009, 04:00:09 PM by SoLoGHoST »
     


    Search


    Latest Activity.
    Help with header area and logo? by Shortie
    [Today at 02:41:21 PM]


    September's Logo Challenge "Up Top" (2009)... by Shortie
    [Today at 12:10:28 PM]


    Assistance With Button by IBRRORG
    [Yesterday at 06:15:43 PM]


    website links for PSP Pro tubes by NerdBoy
    [Yesterday at 06:01:01 PM]


    Error on Upgrade to 2.0 by SoLoGHoST
    [Yesterday at 05:40:10 PM]


    GM Wall by SoLoGHoST
    [Yesterday at 05:37:34 PM]


    Some landscapes I have taken with a camera by Shortie
    [Yesterday at 04:30:13 PM]


    SoLoSMiLeYS by SoLoGHoST
    [Yesterday at 09:53:03 AM]


    Need something Unique by SoLoGHoST
    [Yesterday at 09:26:08 AM]


    CSS Help needed by SoLoGHoST
    [Yesterday at 09:23:42 AM]


    Help Needed.. by Shortie
    [September 12, 2009, 01:52:00 PM]


    The PlayGround Ultimate Support Thread by Shortie
    [September 12, 2009, 08:46:15 AM]


    A Warm TN Hello by SoLoGHoST
    [September 12, 2009, 07:02:05 AM]


    Typo by Shortie
    [September 12, 2009, 01:19:57 AM]


    Graphics Mayhem Wallpaper Collection September 2009 by Witcher
    [September 11, 2009, 09:52:35 PM]



    Page created in 0.155 seconds with 25 queries.