PHP filtering result, need help

Discussion in 'Programming & Software Development' started by Revenger, Jun 14, 2018 at 7:00 PM.

  1. Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    I have a blog page that ties into the MyBB forum software to show threads in a forum.

    Blog Page
    Forum the threads are pulled from

    Now on the blogs I want to filter the results by prefix which can be done in the forum either by clicking the prefix or using the prefix select box at the bottom of the forum.
    This is the last thing bugging me about this blog page getting a filtering category system going.

    A simple dropdown I can have on the right of the blog that will filter the blog would be ideal and all that's really needed.
    Anything fancier like a sidebar list of topics etc I am open to but don't expect it.

    The content is as follows.

    Page layout inner template code (not full file); blog.php
    (The 'Recent Blogs' label should probably be in the right sidebar code and not in the html I can easilly change)
    HTML:
       <div class="article-list">
            <div class="container">
                <div class="row articles">
                    <div class="col-md-2 order-md-2">
                    <strong>Recent Blogs</strong>
                        <?php include 'blog-assets/blog-nav.inc';?>
                    </div>
                    <div class="col-md-10 order-md-1">
                    <!-- blog page -->
                        <?php include 'blog-assets/blog-content.inc';?>
                    </div>
                </div>
            </div>
        </div>
    Connect code: blog-connect.inc
    PHP:
    <?php
        $fid 
    23;
        
    $limit 10;
        
    $navlimit 5;
        
    $forumpath 'http://community.cajgo-support.com/';

        
    chdir('/home/cajgo-admin/domains/community/public_html');
        
    define("IN_MYBB"1);
        require(
    '/home/cajgo-admin/domains/community/public_html/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('/home/cajgo-admin/domains/community/public_html/index.php');
    ?>
    Left column: blog-content.inc
    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);
               

                
    $row['threadprefix'] = $rowprefix '';
                    if(
    $row['prefix'] != 0)
                        {
                            
    $rowprefix build_prefixes($row['prefix']);
                            if(!empty(
    $rowprefix))
                        {
                        
    $row['rowprefix'] = $rowprefix['displaystyle'].'&nbsp;';
                        }
                    }          
                     
                echo(
    "<h1><a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['rowprefix']}{$row['subject']}</a></h1>
                    <h2>Posted: 
    {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a></h2>");
                echo(
    "{$message}");
                echo(
    "<div class=\"replies\">Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)</div>");
                echo(
    "<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\" class=\"btn btn-info btn-sm\" role=\"button\">Read More</a><br><hr>");
            }
        }
        else
        {
            echo 
    'Nothing to display.';
        }

    ?>
    Right column: blog-nav.inc
    PHP:
    <?php
        $query 
    $db->simple_select('threads''*'"fid='{$fid}' ORDER BY tid DESC LIMIT {$navlimit}");
        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);
               
                
    $row['threadprefix'] = $rowprefix '';
                    if(
    $row['prefix'] != 0)
                        {
                            
    $rowprefix build_prefixes($row['prefix']);
                            if(!empty(
    $rowprefix))
                        {
                        
    $row['rowprefix'] = $rowprefix['displaystyle'].'<br>';
                        }
                    }
               
                echo(
    "<p><a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['rowprefix']}{$row['subject']}</a><br>
                    Posted: 
    {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br>");
                echo(
    "Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<hr></p>");
            }
        }
        else
        {
            echo 
    'Nothing to display.';
        }

    ?>
    I managed to this by copy pasting and debugging code from various places as I don't know how to code PHP but can modify things a little.

    So looking for a drop down category switcher for the blog that allows filtering of the blog categories.
     
    Last edited: Jun 14, 2018 at 7:03 PM
  2. mr camouflage

    mr camouflage Member

    Joined:
    May 25, 2012
    Messages:
    570
    Location:
    WA
    Without knowing exactly what variables/fields are in $row its all mostly guesswork (Assuming $row['prefix'] is the prefix id field, which is a bit of a guess. to find out you could var_dump($row) to see whats array keys are available to you), but should give you an idea of how to do it:

    The forum does it by adding prefix=<prefix_id> to the url.

    You would probably need to do the same, eg:

    https://cajgo-support.com/blog.php?prefix=4

    then fetch that from the request globals and add it into the query some how,

    eg:
    PHP:
    //you really should sanitise $prefix to prevent sql injection, or hope that simple_select does that for you, but I doubt it does.
    $prefix$_GET['prefix'];

    //should conditionally check if $_GET['prefix'] is set and then run this query,
    if(array_key_exists('prefix'$_GET ) ){
     
    $query $db->simple_select('threads''*'"fid='{$fid}'  AND prefix={$prefix} ORDER BY tid DESC LIMIT {$limit}");
    } else {
     
    $query $db->simple_select('threads''*'"fid='{$fid}'   ORDER BY tid DESC LIMIT {$limit}");
    }

    You would need to make the prefix a separate link, that links back to the same page, not the forum for the prefix

    eg:
    PHP:
     echo("<h1><a href=\"/blog.php?prefix={$row['prefix']}\">{$row['rowprefix']}</a> <a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a></h1>
                    <h2>Posted: 
    {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a></h2>");
     
  3. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,710
    Location:
    Armadale / Perth
    I'm trying to work out where your first prefix code may apply.

    Second is the h1 echo I can see.
    Happy to setup a testing area etc.
     
  4. mr camouflage

    mr camouflage Member

    Joined:
    May 25, 2012
    Messages:
    570
    Location:
    WA
    First block of code would go in blog-content.inc and replace the first line line $query = ...... etc



     

Share This Page