<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$maindb = "accmast_pe";
$formname = "Expense Head Print";
$file = "expensehead_print.php";
$grpcode = '022';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
// mysqli_select_db($conn, $zdb);

$name = $_POST['name'];

if (!empty($name)) {
    $namecond = " and `accode`='$name' ";
}

$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode='$grpcode' $namecond ORDER BY `name`") or die(mysqli_error($conn));

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php") ?>
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th>Sr</th>
                            <th>Name</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            echo "<tr style='background:#fffae5'>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[name]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr.,Name\n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $contents .= $ctr . ",";
        $contents .= $pmain['name'] . "\n";
        $ctr++;
    }
    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . date('d-m-Y') . ".csv");
    print $contents;
}
?>