PHP - 突出显示从解析的文本文件

时间:2015-05-20 17:24:53

标签: php html parsing select text

所以我有一组文本文件,当选择一个文本文件时,它会被解析并变成一个表格供查看。

我正在尝试突出显示与5个下拉列表中的任何一个中的选择或一组选择匹配的行或行。这些下拉列表是从表格中的列创建的。

我的问题是,一旦选中,如果一行或多行匹配,那些行将突出显示。我的PHP逻辑对于选择部分是完全有缺陷的;表的解析和显示以及dropdwn创建工作正常。

如何在创建表格和下拉列表后创建用于选择行的逻辑?

提前谢谢大家;我到目前为止的代码:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Results</title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <form action="results_choice.php" method="get">
                            <?php
                            $hc_choice = array(
                                'EAST MEADOW CLUB 10-300C Young Bird Race, 10/26/2014 ' => 'EMC2014YB10-300C-TD.txt',
                                'Islip Club 2014 10-500C Old Bird Race, 06/24/14' => 'ISL2014OB10-500C-TD.txt',
                                'NLI2014YB10-300C-TD' => 'NLI2014YB10-300C-TD.txt',
                                'SLI CLUB 10-500C Old Bird Race, 06/24/14' => 'SLI2014OB10-500C-TD.txt',
                                'SLI CLUB 10-300C Old Bird Race, 06/24/14' => 'SLI2014YB10-300C-TD.txt'
                            );
                            echo '<select class="form-control" name="hc" id="ic">';
                            foreach ($hc_choice as $key => $value) {
                                echo "<option value='$value'>$key</option>";
                            }
                            echo '</select>';
                            echo '<input type="submit" class="btn btn-primary" /><hr>';
                            ?>
                        </form>
                    </div>
                    <?php
                    if (isset($_GET['hc'])) {
                        $file_choice = $_GET['hc'];

                        $txt_file = file_get_contents($file_choice);
                        $rows = explode("\n", $txt_file);

                        foreach ($rows as $row => $data) {
                            $row_data = explode("\t", $data);
                            $info[$row]['POS'] = $row_data[0];
                            $info[$row]['NAME'] = $row_data[1];
                            $info[$row]['BAND'] = $row_data[2];
                            $info[$row]['CLUB'] = $row_data[3];
                            $info[$row]['YEAR'] = $row_data[4];
                            $info[$row]['SUFF'] = $row_data[5];
                            $info[$row]['CLR'] = $row_data[6];
                            $info[$row]['X'] = $row_data[7];
                            $info[$row]['ARRIVAL'] = $row_data[8];
                            $info[$row]['MILES'] = $row_data[9];
                            $info[$row]['TOWIN'] = $row_data[10];
                            $info[$row]['YPM'] = $row_data[11];
                            $info[$row]['PT'] = $row_data[12];
                        }
                        $arr = array_column($rows, $info[$row]['NAME']);
                        echo '<div class="row">';

                        echo '<div class="col-md-3"><select class="form-control" name="nc" id="nc">';
                        foreach (array_unique(array_column($info, 'NAME')) as $row_name) {
                            echo '<option>' . $row_name . '</option>';
                        }
                        echo '</select></div>';

                        echo '<div class="col-md-2"><select class="form-control" name="band" id="band">';
                        foreach (array_unique(array_column($info, 'BAND')) as $row_band) {
                            echo '<option>' . $row_band . '</option>';
                        }
                        echo '</select></div>';

                        echo '<div class="col-md-2"><select class="form-control" name="numb" id="numb">';
                        foreach (array_unique(array_column($info, 'CLUB')) as $row_club) {
                            echo '<option>' . $row_club . '</option>';
                        }
                        echo '</select></div>';

                        echo '<div class="col-md-2"><select class="form-control" name="year" id="year">';
                        foreach (array_unique(array_column($info, 'YEAR')) as $row_year) {
                            echo '<option>' . $row_year . '</option>';
                        }
                        echo '</select></div>';

                        echo '<div class="col-md-3"><select class="form-control" name="club" id="club">';
                        foreach (array_unique(array_column($info, 'SUFF')) as $row_suff) {
                            echo '<option>' . $row_suff . '</option>';
                        }
                        echo '</select></div>';


                        echo '</div>';
                        echo '<div class="row">';
                        echo '<div class="col-md-12"><input type="submit" id="search" value="Search" class="btn btn-primary" /></div>';

                        echo '</div>';
                        if (isset($_GET['nc']) && isset($_GET['band']) && isset($_GET['numb']) && isset($_GET['year']) && isset($_GET['club'])) {
                            $nc_name = $_GET['nc'];
                            $band = $_GET['band'];
                            $numb = $_GET['numb'];
                            $year = $_GET['year'];
                            $club = $_GET['club'];
                        } else {
                            $nc_name = "";
                            $band = "";
                            $numb = "";
                            $year = "";
                            $club = "";
                        }
                        echo '</div><hr>';
                        echo '<table class="table table-bordered table-striped">';

                        foreach ($rows as $row => $data) {
                            $row_data = explode("\t", $data);
                            $info[$row]['POS'] = $row_data[0];
                            $info[$row]['NAME'] = $row_data[1];
                            $info[$row]['BAND'] = $row_data[2];
                            $info[$row]['CLUB'] = $row_data[3];
                            $info[$row]['YEAR'] = $row_data[4];
                            $info[$row]['SUFF'] = $row_data[5];
                            $info[$row]['CLR'] = $row_data[6];
                            $info[$row]['X'] = $row_data[7];
                            $info[$row]['ARRIVAL'] = $row_data[8];
                            $info[$row]['MILES'] = $row_data[9];
                            $info[$row]['TOWIN'] = $row_data[10];
                            $info[$row]['YPM'] = $row_data[11];
                            $info[$row]['PT'] = $row_data[12];


                            echo '<tr>';

                            echo '<td>' . $info[$row]['POS'] . '</td>';
                            echo '<td>' . $info[$row]['NAME'] . '</td>';
                            echo '<td>' . $info[$row]['BAND'] . '</td>';
                            echo '<td>' . $info[$row]['CLUB'] . '</td>';
                            echo '<td>' . $info[$row]['YEAR'] . '</td>';
                            echo '<td>' . $info[$row]['SUFF'] . '</td>';
                            echo '<td>' . $info[$row]['CLR'] . '</td>';
                            echo '<td>' . $info[$row]['X'] . '</td>';
                            echo '<td>' . $info[$row]['ARRIVAL'] . '</td>';
                            echo '<td>' . $info[$row]['MILES'] . '</td>';
                            echo '<td>' . $info[$row]['TOWIN'] . '</td>';
                            echo '<td>' . $info[$row]['YPM'] . '</td>';
                            echo '<td>' . $info[$row]['PT'] . '</td>';

                            echo '</tr>';
                        }
                        echo '</table>';
                    }
                    ?>
                </div>
            </div>
        </div>



    </body>
</html>

1 个答案:

答案 0 :(得分:6)

以下代码可以帮助您开始使用,它不是完整且经过测试但会带您朝正确的方向发展......

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Results</title>
       <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <form action="results_choice.php" method="get">
                        <?php
                        $hc_choice = array(
                            'EAST MEADOW CLUB 10-300C Young Bird Race, 10/26/2014 ' => 'EMC2014YB10-300C-TD.txt',
                            'Islip Club 2014 10-500C Old Bird Race, 06/24/14' => 'ISL2014OB10-500C-TD.txt',
                            'NLI2014YB10-300C-TD' => 'NLI2014YB10-300C-TD.txt',
                            'SLI CLUB 10-500C Old Bird Race, 06/24/14' => 'SLI2014OB10-500C-TD.txt',
                            'SLI CLUB 10-300C Old Bird Race, 06/24/14' => 'SLI2014YB10-300C-TD.txt'
                        );
                        echo '<select class="form-control" name="hc" id="ic">';
                        foreach ($hc_choice as $key => $value) {
                            echo "<option value='$value'>$key</option>";
                        }
                        echo '</select>';
                        echo '<input type="submit" class="btn btn-primary" /><hr>';
                        ?>
                    </form>
                </div>
                <?php
                if (isset($_GET['hc'])) {
                    $file_choice = $_GET['hc'];

                    $txt_file = file_get_contents($file_choice);
                    $rows = explode("\n", $txt_file);

                    foreach ($rows as $row => $data) {
                        $row_data = explode("\t", $data);
                        $info[$row]['POS'] = $row_data[0];
                        $info[$row]['NAME'] = $row_data[1];
                        $info[$row]['BAND'] = $row_data[2];
                        $info[$row]['CLUB'] = $row_data[3];
                        $info[$row]['YEAR'] = $row_data[4];
                        $info[$row]['SUFF'] = $row_data[5];
                        $info[$row]['CLR'] = $row_data[6];
                        $info[$row]['X'] = $row_data[7];
                        $info[$row]['ARRIVAL'] = $row_data[8];
                        $info[$row]['MILES'] = $row_data[9];
                        $info[$row]['TOWIN'] = $row_data[10];
                        $info[$row]['YPM'] = $row_data[11];
                        $info[$row]['PT'] = $row_data[12];
                    }
                    $arr = array_column($rows, $info[$row]['NAME']);
                    echo '<div class="row">';

                    echo '<div class="col-md-3"><select class="form-control filter" name="nc" id="nc">';
                    foreach (array_unique(array_column($info, 'NAME')) as $row_name) {
                        echo '<option>' . $row_name . '</option>';
                    }
                    echo '</select></div>';

                    echo '<div class="col-md-2"><select class="form-control filter" name="band" id="band">';
                    foreach (array_unique(array_column($info, 'BAND')) as $row_band) {
                        echo '<option>' . $row_band . '</option>';
                    }
                    echo '</select></div>';

                    echo '<div class="col-md-2"><select class="form-control filter" name="numb" id="numb">';
                    foreach (array_unique(array_column($info, 'CLUB')) as $row_club) {
                        echo '<option>' . $row_club . '</option>';
                    }
                    echo '</select></div>';

                    echo '<div class="col-md-2"><select class="form-control filter" name="year" id="year">';
                    foreach (array_unique(array_column($info, 'YEAR')) as $row_year) {
                        echo '<option>' . $row_year . '</option>';
                    }
                    echo '</select></div>';

                    echo '<div class="col-md-3"><select class="form-control filter" name="club" id="club">';
                    foreach (array_unique(array_column($info, 'SUFF')) as $row_suff) {
                        echo '<option>' . $row_suff . '</option>';
                    }
                    echo '</select></div>';


                    echo '</div>';
                    echo '<div class="row">';
                    echo '<div class="col-md-12"><input type="submit" id="search" value="Search" class="btn btn-primary" /></div>';

                    echo '</div>';
                    if (isset($_GET['nc']) && isset($_GET['band']) && isset($_GET['numb']) && isset($_GET['year']) && isset($_GET['club'])) {
                        $nc_name = $_GET['nc'];
                        $band = $_GET['band'];
                        $numb = $_GET['numb'];
                        $year = $_GET['year'];
                        $club = $_GET['club'];
                    } else {
                        $nc_name = "";
                        $band = "";
                        $numb = "";
                        $year = "";
                        $club = "";
                    }
                    echo '</div><hr>';
                    echo '<table class="table table-bordered table-striped">';

                    foreach ($rows as $row => $data) {
                        $row_data = explode("\t", $data);
                        $info[$row]['POS'] = $row_data[0];
                        $info[$row]['NAME'] = $row_data[1];
                        $info[$row]['BAND'] = $row_data[2];
                        $info[$row]['CLUB'] = $row_data[3];
                        $info[$row]['YEAR'] = $row_data[4];
                        $info[$row]['SUFF'] = $row_data[5];
                        $info[$row]['CLR'] = $row_data[6];
                        $info[$row]['X'] = $row_data[7];
                        $info[$row]['ARRIVAL'] = $row_data[8];
                        $info[$row]['MILES'] = $row_data[9];
                        $info[$row]['TOWIN'] = $row_data[10];
                        $info[$row]['YPM'] = $row_data[11];
                        $info[$row]['PT'] = $row_data[12];


                        echo '<tr>';

                        echo '<td>' . $info[$row]['POS'] . '</td>';
                        echo '<td class="nc">' . $info[$row]['NAME'] . '</td>';
                        echo '<td class="band">' . $info[$row]['BAND'] . '</td>';
                        echo '<td class="numb">' . $info[$row]['CLUB'] . '</td>';
                        echo '<td class="year">' . $info[$row]['YEAR'] . '</td>';
                        echo '<td class="club">' . $info[$row]['SUFF'] . '</td>';
                        echo '<td>' . $info[$row]['CLR'] . '</td>';
                        echo '<td>' . $info[$row]['X'] . '</td>';
                        echo '<td>' . $info[$row]['ARRIVAL'] . '</td>';
                        echo '<td>' . $info[$row]['MILES'] . '</td>';
                        echo '<td>' . $info[$row]['TOWIN'] . '</td>';
                        echo '<td>' . $info[$row]['YPM'] . '</td>';
                        echo '<td>' . $info[$row]['PT'] . '</td>';

                        echo '</tr>';
                    }
                    echo '</table>';
                }
                ?>
            </div>
        </div>
    </div>

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".filter").on("change", function() {
            $("." + $(this).attr("id")).css("background-color", "");
            $("." + $(this).attr("id").":contains("+$(this).val()+")").closest("tr").css("background-color", "#FFFF99");
        });
    });
</script>
</body>