if (isset($_GET['author_id'])) {
    $title_text[] = " by " . get_author_name($_GET['author_id']);
    $sub_header_text[] = 'author: ' . get_author_name($_GET['author_id']);
}
// tag
if (isset($_GET['tag_id'])) {
    $title_text[] = " tagged " . get_tag_title($_GET['tag_id']);
    $sub_header_text[] = 'tag: ' . get_tag_title($_GET['tag_id']);
}
// postdated
if (isset($_GET['postdated'])) {
    $status = ' postdated';
}
// other statuses
if (isset($_GET['status'])) {
    $status_list = define_article_status();
    $status = ' ' . $status_list[$_GET['status']];
}
// any content generation
$left_header = $total_articles . ' articles found';
$right_header = '<a href="' . $_SERVER["PHP_SELF"] . '?page_name=write">/Write a new article</a>';
$page_header = show_page_header($left_header, $right_header);
// get aside content
// any functions
$status_list = get_articles_stats(1);
$author_list = get_authors_admin(1);
$category_list = get_categories_admin(1);
$tags_list = get_tags_admin(1);
// any content generation
// output main content - into $main_content variable
$main_content = $page_header;
/**
 * get_articles_admin
 * 
 * 
 * 
 * 
 * 
 * 
 */
function get_articles_admin()
{
    // get layout config from database
    $per_page = !isset($_GET['per_page']) || empty($_GET['per_page']) ? 15 : (int) $_GET['per_page'];
    $conn = reader_connect();
    // set up pagination
    $page_no = isset($_GET['page']) ? (int) $_GET['page'] : '1';
    // calculate lower query limit value
    $from = $page_no * $per_page - $per_page;
    $query = "SELECT\n\t\t\t\t\tarticles.id,\n\t\t\t\t\tarticles.status,\n\t\t\t\t \tarticles.title, \n\t\t\t\t\tarticles.url, \n\t\t\t\t\tarticles.date_uploaded,\n\t\t\t\t\tarticles.category_id, \n\t\t\t\t\tcategories.title AS category_title, \n\t\t\t\t\tcategories.url AS category_url,\n\t\t\t\t\tarticles.author_id,\n\t\t\t\t\tauthors.name AS author_name, \n\t\t\t\t\tauthors.url AS author_url,\n\t\t\t\t\tarticles.view_count,\n\t\t\t\t\tarticles.visit_count,\n\t\t\t\t\t(SELECT COUNT(id) \n\t\t\t\t\t\tFROM comments \n\t\t\t\t\t\tWHERE article_id = articles.id) AS comment_count\n\t\t\t\tFROM articles \n\t\t\t\t\tLEFT JOIN authors ON articles.author_id = authors.id \n\t\t\t\t\tLEFT JOIN categories ON articles.category_id = categories.id ";
    $where = array();
    // article status
    if (isset($_GET['status'])) {
        $where[] = " articles.status = '" . $conn->real_escape_string($_GET['status']) . "'";
    }
    // postdated
    if (isset($_GET['postdated'])) {
        $where[] = " articles.date_uploaded >= NOW()";
    }
    // author id
    if (isset($_GET['author_id'])) {
        $where[] = " articles.author_id = " . (int) $_GET['author_id'];
    }
    // category url
    if (isset($_GET['category_id'])) {
        $where[] = " articles.category_id = " . (int) $_GET['category_id'];
    }
    // tag
    if (isset($_GET['tag_id'])) {
        $query .= " LEFT JOIN tags_map ON tags_map.article_id = articles.id";
        $where[] = " tags_map.tag_id = " . (int) $_GET['tag_id'];
    }
    // year
    if (isset($_GET['year'])) {
        $where[] = " YEAR(date_uploaded) = " . (int) $_GET['year'];
    }
    // month
    if (isset($_GET['month'])) {
        $where[] = " MONTH(date_uploaded) = " . (int) $_GET['month'];
    }
    // day
    if (isset($_GET['day'])) {
        $where[] = " DAY(date_uploaded) = " . (int) $_GET['day'];
    }
    // compile where clause
    if (!empty($where)) {
        $query .= " WHERE";
        $query .= implode(' AND ', $where);
        // compile WHERE array into select statement
    }
    // sort order
    $query .= " ORDER BY date_uploaded DESC";
    // add pagination
    $query_paginated = $query . " LIMIT " . (int) $from . ", " . (int) $per_page;
    $result = $conn->query($query_paginated);
    // get total results
    $total_result = $conn->query($query);
    $total_articles = $total_result->num_rows;
    $total_pages = ceil($total_articles / $per_page);
    $status = define_article_status();
    $data = array();
    while ($row = $result->fetch_assoc()) {
        $row = stripslashes_deep($row);
        // adjust times to local timezone if necessary
        $ts = strtotime($row['date_uploaded']);
        $offset = date('Z');
        $row['date_ts'] = $ts + $offset;
        $row['date_uploaded'] = date('Y-m-d H:i:s', $row['date_ts']);
        // add status and style
        $row['style'] = $row['date_ts'] > time() ? 'postdated' : $status[$row['status']];
        // add page counts
        $row['total_pages'] = $total_pages;
        $row['total_found'] = $total_articles;
        $data[] = $row;
    }
    $result->close();
    $total_result->close();
    return $data;
}