<?php
    // code
    error_reporting(E_ALL);    // set error reporting to all
    $self = basename($_SERVER['SCRIPT_NAME']);
    $step = isset($_POST['step']) ? $_POST['step'] : 0;
    $search_field = isset($_POST['search_field']) ? $_POST['search_field'] : null;
    $search_text = isset($_POST['search_text']) ? $_POST['search_text'] : null;
    // these are populated by config file
    $con = '';
    $db_name = '';
    include('../includes/open-db.php');        //====== open dB
    // Pull the field names directly from the db table
    // this is not necessary IF you know the names of the fields.
    // However, if you want to write code that adapts to a changing table
    // then this is one way to do it.
    //
    // NOTE, this populates only the search menu items
//    $field = array();
//    $field_text = array();
//
//    $query = "SHOW COLUMNS FROM kjv FROM $db_name";
//    $result = mysqli_query($con, $query) or die(report($query, __LINE__, __FILE__));
//
//    while ($row = mysqli_fetch_array($result))
//        {
//        $a = $row[0];    // pull the actual fields
//        $field[] = $a;    // store raw field
//        $a = str_replace('_', ' ', $a);        // remove underlines from raw field
//        $field_text[] = ucwords($a);            // uppercase first letter of each word
//        }
    include('../includes/close-db.php');        //====== close dB
    include('../includes/header1.php');
?>
    <h2>Search KJV Bible</h2>
    <div class="clear">
         
    </div>
    <form action="<?php echo($self); ?>" method="post">
        <table class="full">
            <tr>
                <td> </td>
                <td> </td>
            </tr>
<!--            <tr>-->
<!--                <td class="right leftside">Search in Verse:</td>-->
<!--                <td class="left rightside">-->
<!---->
<!--                    <select name="search_field">-->
<!--                        --><?php
//
//                            foreach ($field_text as $key => $value)
//                                {
//                                $selected = '';
//                                if ($field[$key] == $search_field)
//                                    {
//                                    $selected = 'selected';
//                                    }
//                                echo("<option value=\"$field[$key]\" $selected >$value");
//                                }
//                        ?>
<!---->
<!--                    </select>-->
<!--                </td>-->
<!--            </tr>-->
            <tr>
                <td class="right leftside">Search Text:</td>
                <td class="left rightside">
                    <input type="text" size="43" id="search_text" name="search_text"
                           value="<?php echo($search_text); ?>">
                </td>
            </tr>
            <tr>
                <td> </td>
                <td> </td>
            </tr>
            <tr>
                <td class="controls">
                    <input type="hidden" name="step" value=1>
                        <input type="submit" class="button" name="submit" value="Search">
                </td>
                <td> </td>
            </tr>
        </table>
    </form>
    <div class="clear">
         
    </div>
<?php // only provide Search Results IF a Search was done
    if ($step == 1)
        {
        include('../includes/open-db.php');    //====== open dB
        $field = cleanForDB($con, $search_field);
        $text = cleanForDB($con, $search_text);
        // get count
        $field = 'vtext';
        $query = "SELECT COUNT(*) as total FROM kjv WHERE $field LIKE '%$text%' ";
        $result = mysqli_query($con, $query) or die(report($query, __LINE__, __FILE__));
        $row = mysqli_fetch_array($result);
        $total = $row['total'];
        ?>
            <h2>
                Results of Search --  <?php echo($total);?> Records
            </h2>
            <table class="full">
                <tr class="header2">
                    <th>ID</th>
                    <th>Testament</th>
                    <th>Book</th>
                    <th>Num</th>
                    <th>Chap</th>
                    <th>Num</th>
                    <th>Verse</th>
                </tr>
                <?php
                    // start clock
                    $starttime = microtime();
                    $startarray = explode(" ", $starttime);
                    $starttime = $startarray[1] + $startarray[0];
                    // get records that match the search -- limit the return of records to 100
                    $query = "SELECT * FROM kjv WHERE $field LIKE '%$text%' ORDER BY $field LIMIT 100";
                    $result = mysqli_query($con, $query) or die(report($query, __LINE__, __FILE__));
                    // end clock
                    $endtime = microtime();
                    $endarray = explode(" ", $endtime);
                    $endtime = $endarray[1] + $endarray[0];
                    $totaltime = $endtime - $starttime;
                    $totaltime = round($totaltime, 5);
                    include('../includes/close-db.php');    //====== close dB
                    if (mysqli_num_rows($result))    // if a search was sucessful (num_rows > 0)
                        {
                        $i = 0;
                        while ($row = mysqli_fetch_array($result))        // pull data from result and display
                            {
                            $id = $row['id'];
                            $testiment = $row['bsect'];
                            $book = $row['bname'];
                            $book_num = $row['bnum'];
                            $chap_num = $row['cnum'];
                            $v_num = $row['vnum'];
                            $v_text = $row['vtext'];
                            if ($testiment == 'O')
                                {
                                $testiment = 'Old';
                                }
                            else
                                {
                                $testiment = 'New';
                                }
                            // considering this form is giving the user the choice of ALL fields
                            // the following script will highlight the search text
                            if ($field == 'id') // search on id
                                {
                                $id = red($id, $text);
                                }
                            if ($field == 'bsect')  // search via testament
                                {
                                $testiment = red($testiment, $text);
                                }
                            if ($field == 'bname')  // search via book name
                                {
                                $book = red($book, $text);
                                }
                            if ($field == 'bnum')    // search via book number
                                {
                                $book_num = red($book_num, $text);
                                }
                            if ($field == 'cnum')   // search via chapter number
                                {
                                $chap_num = red($chap_num, $text);
                                }
                            if ($field == 'vnum')    // search via verse  umber
                                {
                                $v_num = red($v_num, $text);
                                }
                            if ($field == 'vtext')   // search via verse
                                {
                                $v_text = red($v_text, $text);
                                }
                            ?>
                                <tr class="row<?php echo($i++ & 1); ?>">
                                    <td>
                                        <?php echo($id); ?>
                                    </td>
                                    <td>
                                        <?php echo($testiment); ?>
                                    </td>
                                    <td>
                                        <?php echo($book); ?>
                                    </td>
                                    <td>
                                        <?php echo($book_num); ?>
                                    </td>
                                    <td>
                                        <?php echo($chap_num); ?>
                                    </td>
                                    <td>
                                        <?php echo($v_num); ?>
                                    </td>
                                    <td>
                                        <?php echo($v_text); ?>
                                    </td>
                                </tr>
                                <?php
                            $i++;
                            }
                        }
                    else    // no results
                        {
                        echo('<tr><td class="red center" colspan="7">NO RECORDS FOUND!</td</tr>');
                        }
                ?>
            </table>
            <?php
        echo("<p class=\"tiny center\">$total total records returned - Search took: $totaltime seconds</p>");
        }
?>
    <div class="clear">
        <hr>
    </div>
<?php
    include('../includes/footer.php');
    //--------------  show db errors --------------
    // this function reports mysql errors with query, line and script name
    function report($query, $line, $file)
        {
        echo($query . '<br>' . $line . '<br>' . $file . '<br>');
        }
    //--------------  clean data for input into db  --------------
    // this function cleans all text for inserting into db
    // in other words, this funtcion returns SQL injection-proof strings
    function cleanForDB($con, $str)
        {
        $str = stripslashes($str);
        $str = trim($str);
        $str = mysqli_real_escape_string($con, $str);
        return $str;
        }
    //-------------- highlight search text--------------
    // this function accepts a string and a search phrase
    // and places search phrase between a span to highlight
    // the search phrase with a yellow background and red text
    // see css for standout class.
    function red($str, $search_text)
        {
        $search_text = strtoupper($search_text);
        $start = "<span class=\"standout\">";
        $end = "</span>";
        $phrase = $str;
        $good = $start . $search_text . $end;
        $bad = $search_text;
        $str = str_ireplace($bad, $good, $phrase);
        return $str;
        }
?>
Last modified: December 07 2022