PHP filtering result, need help

Discussion in 'Programming & Software Development' started by Revenger, Jun 14, 2018.

  1. Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,738
    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
  2. mr camouflage

    mr camouflage Member

    Joined:
    May 25, 2012
    Messages:
    608
    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,738
    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:
    608
    Location:
    WA
    First block of code would go in blog-content.inc and replace the first line line $query = ...... etc



     
  5. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,738
    Location:
    Armadale / Perth
    Just changed the code that's great and works.
    Now how would I create a dropdown or filter on the right navbar in addition to clicking the prefixes at the start of the title on the left column?

    Is there anything I can do to thank you for this?

    I've done it manually adding this to the code.
    I need to figure out how to change the active option based on the page any idea?
    Here is the page.

    Code:
                            <select class="form-control" id="sel1" onchange="location = this.value;">
                                <option value="blog.php">All Articles</option>
                                <option value="blog.php?prefix=19">State of Address</option>
                                <option value="blog.php?prefix=21">Mental Health</option>
                                <!-- <option value="blog.php">Placeholder</option> -->
                            </select>
    
    This works great but anyway to automate it from the threads themselves?

    Also how I would I go about pagination at the bottom to show all posts in the thread paginmated and filtered.

    Also I want to split the php code.

    In both content and nav this code is common so anyway I can split it off? to a seperate file
    I tried but ended up with a error in my apache log.
    This is the functionality for the page and doesn't need duplication especially if I want to change it later with the mybb options allowed etc.

    Code:
        //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}");
                }
        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;';
                        }
                    }
    
     
    Last edited: Jun 22, 2018
  6. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,738
    Location:
    Armadale / Perth
    Mobile seems to have come good after a few hours, I'll keep a eye on things.
    I guess the cache wasnt updating properly earlier.
     
  7. mr camouflage

    mr camouflage Member

    Joined:
    May 25, 2012
    Messages:
    608
    Location:
    WA
    Code:
                            <select class="form-control" id="sel1" onchange="location = this.value;">
                                <option value="blog.php">All Articles</option>
                                <option value="blog.php?prefix=19" <?php if($_REQUEST['prefix'] ==  19  ){echo ' selected="selected" '; } ?> >State of Address</option>
                                <option value="blog.php?prefix=21"<?php if($_REQUEST['prefix'] ==  21  ){echo ' selected="selected" '; } ?> >Mental Health</option>
                                <!-- <option value="blog.php">Placeholder</option> -->
                            </select>
    
     
  8. OP
    OP
    Revenger

    Revenger Member

    Joined:
    Aug 7, 2002
    Messages:
    3,738
    Location:
    Armadale / Perth
    Just put in in on my mobile works great.

    If you want to help me expand with the rest of the features as mentioned above I would live to support you back in some way.
    Up to you ok.
    I'm happy to support other people for help.
     

Share This Page