<?php
include 'xs_config.php';
include 'xs_db.php';
include 'template.php';

global $dbhost, $dbuser, $dbpass, $dbname;
  
   
$conn  =  new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if(isset($_GET['s']) && !empty($_GET['s'])){
    $search = $_GET['s']; 
    $query =  "SELECT * from `ptests` where (
                            ReferenceID LIKE '%$search%')
                            or PageName LIKE '%$search%'
                            or PageSummary LIKE '%$search%'
                            or SectionTitle1 LIKE '%$search%'
                            or SectionText1 LIKE '%$search%'
                            or SectionTitle2 LIKE '%$search%'
                            or SectionText2 LIKE '%$search%'
                            or SectionTitle3 LIKE '%$search%'
                            or SectionText3 LIKE '%$search%'
                            or SectionTitle4 LIKE '%$search%'
                            or SectionText4 LIKE '%$search%'
                            or SectionTitle5 LIKE '%$search%'
                            or SectionText5 LIKE '%$search%'
                            or SectionTitle6 LIKE '%$search%'
                            or SectionText6 LIKE '%$search%'
                            or SectionTitle7 LIKE '%$search%'
                            or SectionText7 LIKE '%$search%'
                            or SectionTitle8 LIKE '%$search%'
                            or SectionText8 LIKE '%$search%'
                            or SectionTitle9 LIKE '%$search%'
                            or SectionText9 LIKE '%$search%'
                            or SectionTitle10 LIKE '%$search%'
                            or SectionText10 LIKE '%$search%'
                            or SectionTitle11 LIKE '%$search%'
                            or SectionText11 LIKE '%$search%'
                            or SectionTitle12 LIKE '%$search%'
                            or SectionText12 LIKE '%$search%'
                            or SectionTitle13 LIKE '%$search%'
                            or SectionText13 LIKE '%$search%'
                            or SectionTitle14 LIKE '%$search%'
                            or SectionText14 LIKE '%$search%'
                            or SectionTitle15 LIKE '%$search%'
                            or SectionText15 LIKE '%$search%'
                            ";
    
  }
  else{
    $query =  "SELECT * from `ptests` where ReferenceID = Null";
  }
    $result = mysqli_query($conn,"$query;")
    or die(mysqli_error());

    $selectQuery =  "SELECT * from `ptests` where 1";

    $selectResult = mysqli_query($conn,"$selectQuery;")
    or die(mysqli_error());

    $result1 = mysqli_query($conn,"$query;")
    or die(mysqli_error());

    $data = [];
    while ($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)) {
        $data = $row;
    }

// print_r($data);
// die;
show_header($title='Search')
?>

<!-- faq sectione -->
<?php if((mysqli_num_rows($result)) > 0) { ?>
<section class="second-test sec-pad">
        <div class="container">

            <div class="tests-section">
                <nav aria-label="breadcrumb">

                    <p class="breadcrumb-header">Search Result</p>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                        <li class="breadcrumb-item"><a href="#">Tests</a></li>
                    </ol>


                </nav>

                <?php $relateds = explode(', ', $data['Related']); ?>

                <div class="related-test">

                    <ul class="footer-social">
                        <h3>Related Test</h3>
                        <?php foreach($relateds as $rel){ ?>
                        <li><a href="#"><?= $rel; ?></a></li>
                        <?php } ?>

                    </ul>

                    <div class="input-box">
                        <p>Printer <br> Friendly</p>
                        <img src="./images/logo/Layer 35.png" alt="">
                    </div>
                </div>

            </div>
        </div>
    </section>
    <?php } ?>

<section class="organ-page centered">
        <div class="container">
        <?php if((mysqli_num_rows($result)) > 0) { 
                    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
                ?>
            <div class="row">                
                <div class="col-md-4 col-sm-6 col-xs-12 team-colmun">
                    <div class="single-item">
                        <div class="img-box">
                            <figure>
                                <a href="ptests.php?s=<?= $row['ReferenceID'] ?>">
                                    <img src="content/summaryimages/<?= $row['PageImage'] ?>" alt="">
                                </a>
                            </figure>

                        </div>
                        <div class="lower-content">
                            <p><?= ucfirst($row['PageName']) ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-8 col-sm-8 col-xs-12 team-colmun">
                    <h3 style="color:#0166ff; text-align:left;">
                        <a href="ptests.php?s=<?= $row['ReferenceID'] ?>" style="color:#0166ff; text-align:left; font-size:20px;"><?= ucfirst($row['PageName']) ?></a></h3>
                    <p style="color:black; text-align:justify; font-size:15px;"><?= $row['PageSummary'] ?></p>
                </div>
                    
            </div>
            <?php }}else{ ?>
                <div class="row">
                    <div class="col-md-6 col-sm-6 col-xs-12 team-colmun">
                        <div style="text-align:left;">
                            <p class="breadcrumb-header">Search Result not Found</p>
                            
                            <br>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>
    </section>
    <!-- faq sectione -->




<section id="test-below" class="main-organ" style="margin-top:-50px;">
        <div class="container">
            <div class="row">
                <h3>Search For Another Test</h3>


                <div class="col-md-6 col-sm-6 col-xs-12 footer-colmun search-another-text">
                    <div class="subscribe-widget footer-widget">

                        <div class="input-box dropdown">
                            <form action="ptests.php" method="get"> 
                                <div class="form-group">
                                    <select name="q" class="form-control" id="searchBySelect" style="padding:12px !important; border-radius:10px; background:#f2f2f7;">
                                        <option selected>Select One item</option>
                                        <?php while ($row = mysqli_fetch_array($selectResult,MYSQLI_ASSOC)) { ?>
                                            <option value="<?= $row['ReferenceID']; ?>" <?php echo $row['ReferenceID']== $_GET['q'] ? 'selected' : ''; ?>><?= ucfirst($row['ReferenceID']) ?></option>

                                        <?php } ?>    
                                    </select>
                                </div>
                                <button type="submit" class="btn btn-info" id="submitSelectValue" style="display:none;">Submit></button>
                            </form>
                        </div>

                    </div>
                    <p class="or">or</p>
                </div>
                <div class="col-md-6 col-sm-6 col-xs-12 footer-colmun">
                    <div class="subscribe-widget footer-widget">
                        <div class="input-box">
                            <form action="search.php" method="get">
                                <input type="search" placeholder="Search Via Keywords" name="s" value="<?= isset($_GET['s']) ? $_GET['s'] : '' ?>">
                                <button type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
                            </form>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </section>


<?php show_footer() ?>