Members: 278  •  Posts: 12610  •  Topics: 864  •  Please welcome bosanci28, 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 124 times)

0 Members and 1 Guest are viewing this topic.

SoLoGHoST

  • SMF MOD Expert!
  • SoLoGHoSt
  • Connoisseur
  • Offline
  • Posts: 2143
  • 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 »

    SoLoGHoST

    • SMF MOD Expert!
    • SoLoGHoSt
    • Connoisseur
    • Offline
    • Posts: 2143
    • OS:
    • Windows Vista/Server 2008
    • Browser:
    • Firefox 3.0.14
    Re: Loop through posts from any given Board at a Set Interval
    « Reply #1 on: September 17, 2009, 02:48:19 AM »
      Ok, there just added the ability for you to set your own interval for posts transitions, and also made it so that each post transition never gets repeated over again.  So each transition will be different from the previous one.  Text area only allows numeric characters.  Just having a bit more fun with this.  Check it out at the TEST PAGE

      Cheers
      « Last Edit: September 17, 2009, 02:50:29 AM by SoLoGHoST »
       


      Search


      Latest Activity.
      Need some badges... please by Green CoW
      [Today at 04:19:17 AM]


      Icon For Top To Link To Wiki by Shortie
      [Today at 02:42:14 AM]


      Windows 7 Walls by NerdBoy
      [Yesterday at 07:51:35 PM]


      Novembers Graphics Mayhem Wallpaper by DMHolt57
      [Yesterday at 03:28:47 PM]


      More Distinct For SMF RC2 by the yicker
      [Yesterday at 11:40:32 AM]


      November's Logo Challenge "Up Top" (2009)... by NerdBoy
      [Yesterday at 05:41:46 AM]


      What's the deal with SMF? by NerdBoy
      [Yesterday at 05:40:12 AM]


      Deep Red 2 For SMF RC2 by Shortie
      [Yesterday at 04:22:26 AM]


      help in a photo by SoLoGHoST
      [November 18, 2009, 08:34:10 PM]


      Topic Count in Profiles by live627
      [November 18, 2009, 06:14:20 PM]


      ATI Catalyst™ version 9.11 released by Nukegaming
      [November 18, 2009, 03:44:16 PM]


      themed logo challenge by Shortie
      [November 17, 2009, 02:29:20 PM]



      Page created in 0.097 seconds with 22 queries.