<?php 

include "../db.php";

session_start();



if (isset($_POST['start'])) {

    $start= mysqli_real_escape_string($con,$_POST['start']);

    $AndWhere ='';



    if (!empty($_POST['FirstFilterValue']) && empty($_POST['SecondFilterValue']) ) {

        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$_POST[FirstFilterValue]'");

        if($state_row = mysqli_fetch_assoc($state_sql)){

            $AndWhere = "WHERE stateID = '$state_row[id]'";

        }

    }

    if (empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {

        $AndWhere = "WHERE distName = '$_POST[SecondFilterValue]'";

    }

    if (!empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {

        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$_POST[FirstFilterValue]'");

        if($state_row = mysqli_fetch_assoc($state_sql)){

            $AndWhere = "WHERE stateID = '$state_row[id]' AND distName = '$_POST[SecondFilterValue]' ";

        }

    }



    $sql = "SELECT * FROM dist $AndWhere ORDER BY id DESC LIMIT $start,10";



    $res = mysqli_query($con,$sql);

    if ($total_rows=mysqli_num_rows($res)>0) {

        $s_output='';

        $c = $start+1;

        while ($rows=mysqli_fetch_assoc($res)) {

            $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$rows[stateId]'");

            if($state_row = mysqli_fetch_assoc($state_sql)){

                $stateName = $state_row['name'];

            }

            ?>

<li class='clearfix' style='float: none;'>

    <div class="single-dis mb-2">

        <div class='row align-items-center'>

            <div class='col-md-10 col-9 text-white'>

                <b>District : </b> <?php echo $rows["distName"];?>

                <br>

                <b>State : </b> <?php echo $stateName;?>

            </div>

            <div class='col-md-2 col-3 text-end'>

                <a href='district.php?edit_id=<?php echo $rows["id"];?>' class='btn btn-success'

                    title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i> </a>

                <a href='district.php?del_id=<?php echo $rows["id"];?>' class='delete btn btn-danger'

                    title='Click to Delete'><i class='fa fa-trash' aria-hidden='true'></i> </a>

            </div>

        </div>

    </div>

</li>

<?php

$c++;

}

mysqli_close($con);

}

}

?>