Need help with a news/blog listing page

Discussion in 'Programming & Software Development' started by Revenger, May 23, 2018.

  1. Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    I am working on a blog that shows threads on a php page and hooks into a specific forum on my site.
    The code was just something given to me on the MyBB forum for hooking into a thread using MySQL.
    I don't know much of PHP or MySQL so I need someone willing to help finish this.

    Here is what I have already on a blog page.
    It consists of 2 files the bootstrap framework header/footer also used on the front page then a .inc file for the php code in the middle
    https://cajgo-support.com/blog.php

    Here is the .inc file code.
    There's some useless code in here for progress the original author used for himself which needs removing, I removed some but not all as I don't know what I can take out.
    PHP:
    <?php

    // your php file contents
    $mybbconnect=mysqli_connect('localhost','username','password','databse')or die(mysqli_error());
    $mybbconnect->set_charset("utf8");
    $forumid="23";
    $forumdir='https://community.cajgo-support.com'
    ?>

    <?php
    $last_post_q
    =mysqli_query($mybbconnect,"SELECT c.tid as ctid, e.pid as epid, b.fid as filid, e.displaystyle, c.dateline, b.dateline as bdate, c.subject, c.prefix, c.tid, b.pid, b.tid as btid, b.username, b.edittime FROM mybb_posts as b LEFT JOIN mybb_threads as c ON c.tid=b.tid LEFT JOIN mybb_threadprefixes as e ON e.pid=c.prefix LEFT JOIN mybb_forums as d ON b.tid=d.fid WHERE b.fid='$forumid' ORDER BY bdate DESC LIMIT 0, 5 ");
      while (
    $mpr $last_post_q->fetch_assoc()) {
      
    $mtitle=$mpr['subject'];
      
    $progres=$mpr['displaystyle'];
      
    $curper=$mpr['displaystyle'];
      
    $prevodach=$mpr['username'];
      
    $pid=$mpr['pid'];
      
    $ctid=$mpr['ctid'];
      
    $bdate=$mpr['bdate'];


     echo 
    '<div><p><a href="'.$forumdir.'/showthread.php?tid='.$ctid.'"> '.$mtitle.'</a>';
     echo 
    '<br>'.$prevodach.' <br>';
     echo 
    date('D/m/y - H:i'$bdate);
     echo 
    '</p></div>';

      }
     
    ?>
    Here is the forum I am pulling the info from:
    https://community.cajgo-support.com/forum-23.html

    Here is my current blogs in another forum as a example what I will be doing as well as showing some images in the posts etc.
    https://community.cajgo-support.com/thread-27.html

    The new format will be one blog per thread topic rather than post which will allow comments on the blogs.
    Similar to our OCAU news system.

    I want to show number of replies and some of the blog post text with a read more button.
    Also the above code needs changing to show all posts but with a page selector at the bottom.

    If anyone wants to help me finish this it would be appreciated.
     
    Last edited: May 23, 2018
  2. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    There is another set of code that hooks into MyBB.
    Would doing it this way be more suitable?
    This way is more complete and just needs some tweaking.
    Demo of the code below with some slight modification since posting the code here.
    https://cajgo-support.com/blog-1.php
    Tell me your thoughts.

    PHP:
    <?php

        $fid 
    2;
        
    $limit 5;
        
    $forumpath 'http://forums.yourdomain.com/';

        
    chdir('/your absolute path/forums');
        
    define("IN_MYBB"1);
        require(
    '/your absolute path/forums/global.php');
        require_once 
    MYBB_ROOT."inc/class_parser.php";
        require_once 
    MYBB_ROOT."inc/functions_post.php";
        require_once 
    MYBB_ROOT."inc/functions_user.php";
        
    $parser = new postParser;
        
    chdir('/your absolute path/your domain/index.php');
    ?>
    PHP:
    <?php
        $query 
    $db->simple_select('threads''*'"fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");
        if(
    $db->num_rows($query) > 0)
        {
            while(
    $row $db->fetch_array($query))
            {
                
    $query2 $db->simple_select('posts''*'"pid='{$row['firstpost']}'");
                
    $row2 $db->fetch_array($query2);
            
                
    $date my_date($mybb->settings['dateformat'], $row2['dateline'], ""1);
                
    $time my_date($mybb->settings['timeformat'], $row2['dateline'], ""1);

                
    $options = array(
                            
    "allow_html" => 1,
                
    "allow_mycode" => 1,
                
    "allow_smilies" => 1,
                
    "allow_imgcode" => 1,
                
    "allow_videocode" => 1,
                
    "filter_badwords" => 1
                                
    );
                
    $message $parser->parse_message($row2['message'], $options);
            
                echo(
    "<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a> -
                    Posted: 
    {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br />");
                echo(
    "{$message}<br /><br />");
                echo(
    "Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br />");
            }
        }
        else
        {
            echo 
    'Nothing to display.';
        }

    ?>
     
    Last edited: May 23, 2018
  3. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    Working on the second code but cant figure out a read more tag.
    Any advice and help?

    Update:
    I have worked around the issue.
    I did a readmore tag in MyCode which used the bootstrap collapse div tag so that only activates on bootstrap pages being the front page.
    Then in the blog include I put a read more button to link back to the thead.
    I'll try and add a .... tag in also for long posts that shows on the front page only.

    I have a "*** Click 'Read More' to read the rest of this article ***" line now showing if there's more to the article.
     
    Last edited: May 23, 2018
  4. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    Last night it managed to get something decent going here with the second set of code I posted above.
    https://cajgo-support.com/blog.php

    Now I'm looking for all the advice I can get and any improvements on styling etc.
    I already have s list of stuff here but looking for anything to make this pretty and fully functional as a premiere blogs and article list.
    https://community.cajgo-support.com/thread-45.html

    For the more advanced features like multi page I need someone who can modify the code with that as that is above my skills.

    I'd be happy to swing a game your way or something for helping.
     
    Last edited: May 24, 2018
  5. GumbyNoTalent

    GumbyNoTalent Member

    Joined:
    Jan 8, 2003
    Messages:
    5,302
    Location:
    Briz Vegas
    PHP:
    $limit 5;
    Limits the select statement to 5 records.
     
  6. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    Know about limit.
    The thing is I don't know how to show all blogs / threads then have a multipage with a limit like our news posts or threads etc per page here.
    Limit is great for showing for the most recent blogs on the sidebar though.
     
  7. GumbyNoTalent

    GumbyNoTalent Member

    Joined:
    Jan 8, 2003
    Messages:
    5,302
    Location:
    Briz Vegas
    https://www.w3schools.com/php/php_mysql_select_limit.asp

    [​IMG]
    source: http://www.mysqltutorial.org/mysql-limit.aspx

    So in the example you have a limit set to 5, so this will return the first 5 records, if you wanted the second lot of 5 (second page) you would off set the LIMIT by 5.

    So usually your off set is ((page.number -1)*LIMIT).

    Then you need to workout your page navigation, for this you will need the total number of records that exist even with a LIMIT, luckily MySQL has FOUND-ROWS.
    https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_found-rows

    Hope the pointers help.
     
  8. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    Probably a bit beyond me at this stage.
    And I've chosen to use the second code the direct hook into the files as I don't know how to expand the first mysql stuff.

    I'll post what I have later if people are interested in helping to expand it.
     
  9. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    And my first version is complete.
    I need to fix timestamps to match the forum but don't know now.

    I want to do prefixes and filters but I don't the skills.
    Please let me know how this is and if you can help expand it.
     
    Last edited: May 24, 2018
  10. neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    2,527
    Location:
    Perth NOR
    This looks like a good little project to learn with. Just keep at it.

    Gumby' has introduced you to pagination. Usually you have to read some URL parameters to get the current page (eg mysite.com/blog/page/2, you need to get the 2), then use that parameter to get the relevant results.

    The second method of database access looks the better option. The first is directly connecting to the database, and I believe the way it is sending parameters is the way to get hacked via SQL-injection, so that's not good. The second method you posted looks to be using the forum software's helpers/shortcuts for accessing the database. This should be safer, and also more convenient. If you look into it, it might already provide easy methods for pagination.
     
  11. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    Cheers for the message, it's using the second code, I'm not too sure about doing pagination or getting the prefix field by linking the variable in the post row to the display span tag as I'm not a coder I just manipulate what I have.

    Pagination is one, showing the prefix with its span tag then creating a prefix filter as a kinda category is what I want to expand to.

    A lot of this for these extras are over my head, will research online but can use a hand so until that they will have to wait.
     

Share This Page