发送搜索查询页面后变为空白

时间:2015-05-11 14:50:17

标签: php mysql

我正在开发PHP和PHP中的一个小搜索引擎。 MySQL的。昨天,我重新设置了(我只在HTML部分进行了更改,没有触及任何PHP代码)结果页面,之后我无法搜索一个术语。

新代码:

<?php
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
require_once("lucene.php");
?>

<?php
$row1 = [];
$row2 = [];
$search_word = false;
if(isset($_POST["submit"])) {
    //edit this with your credentials
    $con = mysqli_connect("localhost", "root", "pass", "db");

    if(mysqli_connect_error()) echo "Connection Fail";
    else {
        $search_word = true;
        $input = $_POST["s_input"];

        // tokenize input
        $tokens = tokenize($input);

        //compute weight of every token
        $token_weight = compute_weight($tokens, $con);

        $sql1 = "SELECT *, match(description) against('". $input ."') as score FROM web where match(description) against('".$input."') order by score desc";
        $sql2 = "SELECT *, match(description) against('". $input ."') as score FROM web where match(description) against('".$input."') order by score desc";

        $result1 = mysqli_query($con, $sql1);
        $result2 = mysqli_query($con, $sql2);

        $sql3 = "SELECT * FROM web";
        $numDocs = (mysqli_num_rows(mysqli_query($con, $sql3)));

        $maxOverlap = sizeof($tokens);
        $ctr2 = 0;
        while($ctr2 != $maxOverlap){
            //compute inverse_document_frequency of term
            $sql3 = "SELECT *, match(description) against('".$tokens[$ctr2]."') FROM web where match(description) against('".$tokens[$ctr2]."')";
            $docFreq = (mysqli_num_rows(mysqli_query($con, $sql3)));
            $idf[$tokens[$ctr2]] = idf($numDocs, $docFreq);
            $ctr2++;
        }

        $ctr = 0;
        while($r1 = mysqli_fetch_array($result1)){
            $row1[$ctr]['ID'] = $r1['ID'];
            $row1[$ctr]['title'] = $r1['title'];
            $row1[$ctr]['description'] = highlight_words($r1['description'], $tokens);
            $row1[$ctr]['credit'] = $r1['credit'];
            $row1[$ctr]['url'] = $r1['url'];

            $document = strtolower($row1[$ctr]['description']);
            $ctr2 = 0;
            $summation = 0;
            while ($ctr2 != sizeof($tokens)) {
                //compute term_frequency(term in document)
                $term_frequency = tf($tokens[$ctr2], $document);
                //get idf of term
                $inverse_document_frequency = $idf[$tokens[$ctr2]];
                //compute norm
                $norm = norm($document);
                $ctr2++;

                $summation += $term_frequency*$inverse_document_frequency*$inverse_document_frequency*$norm;
            }

            $queryNorm = queryNorm($idf, $tokens);
            $coord = coord($tokens, $document, $token_weight);

            $lucene_score = $summation * $coord * $queryNorm;

            $row1[$ctr]['score'] = $lucene_score;
            $ctr++;
        }
        $row1 = orderBy($row1);

        /*
            desc score = number of word ocurrences * weight + match_against weight
                            + 2(if exact words occur)
        */
        $ctr = 0;
        while($r2 = mysqli_fetch_array($result2)){
            $row2[$ctr]['ID'] = $r2['ID'];
            $row2[$ctr]['title'] = $r2['title'];
            $row2[$ctr]['description'] = highlight_words($r2['description'], $tokens);
            $row2[$ctr]['credit'] = $r2['credit'];
            $row2[$ctr]['url'] = $r2['url'];

            $desc = strtolower($row2[$ctr]['description']);
            $ctr2 = 0;
            $total_weight = 0;
            $flag = 0;
            while ($ctr2 != sizeof($tokens)) {
                $weight = substr_count($desc, $tokens[$ctr2]) * $token_weight[$tokens[$ctr2]];
                if ($weight == 0)
                    $flag++;
                $total_weight += $weight;
                $ctr2++;
            }

            //occurence of exact word * weight * 2
            $exact_word_weight = substr_count($desc, $input) * $total_weight * 5;

            $total_weight += $flag;
            $row2[$ctr]['score'] = $r2['score'] + $total_weight + $exact_word_weight;
            $ctr++;
        }
        $row2 = orderBy($row2);

    }
    mysqli_close($con);
}
?>

<!DOCTYPE>
<html>
<head>
    <TITLE>Search Engine</TITLE>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/search.css">

    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Solve Simplex JavaScript -->
    <script src="js/validation.js"></script>
</head>
<body>
<div id="wrapper">
    <nav>
        <form name="search_form" id="search_form" validate method="post">
            <div class="left">
                <a href="#"><img src="images/logo.png" attr="google logo"></a>
                <input type="text" class="textbox" id="obj_function" name="s_input">
                <button type="submit" id="search_button" name="submit">
        </form>
</div>
        <div class="right">
            <a href="#" id="box"><img src="images/button.png"></a>
            <a href="#" id="sign_in">Sign In</a>
        </div>
    </nav>
    <div class="nav2">
        <ul>
            <li class="active">Web</li>
            <li><a href="">Images</a> </li>
            <li><a href="">Videos</a></li>
            <li><a href="">More <span><img src="images/dropdown.png"></span></a></li>
            <li><a href="">Search Tools</a></li>
        </ul>
    </div>
    <div class="search_results">
        <ol>
            <li>
                <?php
                $ctr = 0;
                while ($ctr != count($row2)){
                    echo "<h1><a href=''#'> ".$row2[$ctr]['title']."</a></h1>";
                    echo "<div class='grey_green_links'>";
                    echo "<p class='green'>".$row2[$ctr]['url']."<span><img src='images/search_result_link_dropdown.png'></span></p>";
                    echo "<tr class = 'active'><td>"."<b>Description: </b>".($row2[$ctr]['description']."</td><td></td></tr>");
                    echo "<tr><td><br></br></td><td></td></tr>";
                    $ctr++;
                }

                ?>
            </li>
        </ol>
        <div class="page_nav">
            <img src="images/pagination_logo.png" alt="pagination page logo">
            <ol>
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
                <li><a href="#">6</a></li>
                <li><a href="#">7</a></li>
                <li><a href="#">8</a></li>
                <li><a href="#">9</a></li>
                <li id="next"><a href="#">10</a></li>
                <li><a href="#">Next</a></li>
            </ol>
        </div>
        <footer>
            <div class="top">
                <p>
                    <img src="images/your_location_dot.png">
                    <span class="light_grey">Mars, TX - From your Internet address -</span> Use precise lcoation - Learn more
                </p>
            </div>
            <div class="bottom">
                <ul>
                    <li>Help</li>
                    <li>Send feedback</li>
                    <li>Privacy</li>
                    <li>Terms</li>
                </ul>
            </div>
        </footer>
    </div>
</body>
</html>

我个人认为这段代码中存在错误,但我无法弄清楚它应该在哪里。有人可以看一下,也许可以帮帮忙吗?

<form name="search_form" id="search_form" validate method="post">
                <div class="left">
                    <a href="#"><img src="images/logo.png" attr="google logo"></a>
                    <input type="text" class="textbox" id="obj_function" name="s_input">
                    <button type="submit" id="search_button" name="submit">
            </form>

1 个答案:

答案 0 :(得分:0)

我重新做了一点点工作,感谢所有人的帮助。