1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

vbulletin last x posts in php

Discussion in 'PHP' started by leeds1, Jan 26, 2005.

  1. #1
    Hi

    I have found some php code to provide the last x post on my vb forum via the "external.php" / RSS file

    It all works OK but I have a couple of questions:

    1: How do I limit it to (say) 5 posts - it seems to pull everything at the moment
    2: In the description it pulls the forum, the poster and a very long date - how can I split this field up so I just get the poster?

    Thanks


    <?php 
    $_item = array(); 
    $_depth = array(); 
    $_tags = array("dummy"); 
    
    function initArray()
    { 
    global $_item; 
    
    $_item = array ("TITLE"=>"", "LINK"=>"", 
    "DESCRIPTION"=>"", "URL"=>""); 
    }
    
    function startElement($parser, $name){ 
    global $_depth, $_tags, $_item; 
    
    if (($name=="ITEM") || 
    ($name=="CHANNEL") 
    || ($name=="IMAGE")) { 
    initArray(); 
    } 
    @$_depth[$parser]++; 
    array_push($_tags, $name); 
    } 
    
    function endElement($parser, $name){ 
    global $_depth, $_tags, $_item; 
    
    array_pop($_tags); 
    $_depth[$parser]--; 
    switch ($name) { 
    case "ITEM": 
    echo "<li><a href=\"{$_item['LINK']}\">" . "{$_item['TITLE']}</a><BR><BR>\n"; 
    
    
    // {$_item['DESCRIPTION']}</li><BR><BR>\n"; 
    initArray(); 
    break; 
    } 
    }
    
    function parseData($parser, $text){ 
    global $_depth, $_tags, $_item; 
    
    $crap = preg_replace ("/\s/", " ", $text); 
    /* is the data just whitespace?
    if so, we don't want it! */ 
    
    if ($crap) { 
    $text = preg_replace ("/^\s+/", " ", $text); 
    /* get rid of leading whitespace */ 
    if (@$_item[$_tags[$_depth[$parser]]]) { 
    $_item[$_tags[$_depth[$parser]]] .= 
    $text; 
    } else { 
    $_item[$_tags[$_depth[$parser]]] = 
    $text; 
    } 
    } 
    } 
    
    function parseRDF($file){ 
    global $_depth, $_tags, $_item; 
    
    $xml_parser = xml_parser_create(); 
    initArray(); 
    
    /* Set up event handlers */ 
    xml_set_element_handler
    ($xml_parser, "startElement", "endElement"); 
    xml_set_character_data_handler
    ($xml_parser, "parseData"); 
    
    /* Open up the file */ 
    $fp = fopen ($file, "r") or die ("Could not 
    open $file for input"); 
    
    while ($data = fread ($fp, 4096)) { 
    if (!xml_parse($xml_parser, $data, feof
    ($fp))) { 
    die (sprintf("XML error: %s at line %d", 
    xml_error_string(xml_get_error_code 
    ($xml_parser)), 
    xml_get_current_line_number
    ($xml_parser))); 
    } 
    } 
    
    fclose($fp); 
    xml_parser_free($xml_parser); 
    }
    
    parseRDF
    ("http://www. mydomain/forums/external.php");
    ?> 
    
    
    Code (markup):
     
    leeds1, Jan 26, 2005 IP
  2. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #2
    I can't help you with your question but here is a link to some other scripts that you might be able to work with.
     
    ResaleBroker, Jan 26, 2005 IP
  3. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Here you go. I found this at vbwebmasters.com

    In external.php find:

    WHERE 1=1
    $forumchoice
    AND thread.visible = 1
    AND open <> 10
    AND deletionlog.primaryid IS NULL
    ORDER BY thread.dateline DESC
    LIMIT 15 
    PHP:
    Change 15 to the number of your choice.
     
    ResaleBroker, Jan 27, 2005 IP
  4. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Here's my code:

    I would recommend running this a few times a day and outputting it to a static html file. That way you don't run this query every time you load a page with the code included.
    
    <?
    $server = 'server';
    $login = 'loginname';
    $pass = 'dbpassword';
    $dbname = 'databasename';
    $forumdomain = 'http://forums.yourdomain.com';
    $ext = 'php'; 
    $results = '5';
    
    $link = mysql_connect($server, $login, $pass)
       or die('Error with Forum ' . mysql_error());
    mysql_select_db($dbname) or die('Could not select database');
    
    // Performing SQL query
     $query = 'SELECT title, threadid'
    		. ' FROM `thread` '
    		. ' ORDER BY lastpost DESC '
    		. ' LIMIT '.$results;
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    
    // Printing results in HTML
    echo "<h4>Discussion Topics</h4>\n";
    echo "<ul>\n";
    $alter = "odd"; 
    while ($row = mysql_fetch_assoc($result)) {
    echo "<li class=\"".$alter."\"><a href=\"".$forumdomain."/showthread.".$ext."?t=".$row['threadid']."\" target=_NEW>".$row['title']."</a></li>";
       if($alter == "odd") $alter = "even"; else $alter = "odd"; 
    }
    echo "</ul>\n";
    
    // Free resultset
    mysql_free_result($result);
    
    // Closing connection
    mysql_close($link);
    ?> 
    
    
    PHP:
     
    nevetS, Jan 27, 2005 IP
    ResaleBroker likes this.
  5. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #5
    That's awesome. Thanks! :D
     
    ResaleBroker, Jan 27, 2005 IP