0
<?php
$query = $_GET['query'];
// gets value sent over search form

$min_length = 6;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM cwnational WHERE (`postcode` = '$query') OR (`structure` LIKE '%".$query."%')") or die(mysql_error());

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following


while($results = mysql_fetch_array($raw_results)){

echo "<p><h3>".$results['postcode']."</h3>".$results['structure']."</p>";



}

while($results = mysql_fetch_array($raw_results)){

if($results['structure'] = "National") {

echo "print national_table";

} else {
echo "print local_table";



}



}
else{ // if there is no matching rows do following
echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }


?>
</body>

我现在完全被难住了。。

当我成功匹配 $query 时,我想使用数组的第二部分,它应该是一个名为结构的列,并将其用作从数据库中打印 table_local 或 table_national 的开关。

在掌握了正确的方法后重新编写了它,

if (empty($searchTerm)) {
                echo "<h1>Empty search term</h1>";
                $sqlQuery = false;
            } elseif (strlen($searchTerm) < $minQueryLength) {
                echo "<h1>Search term must be at least ".$minQueryLength." characters long.";
                $sqlQuery = false;
            } else {
                if (strlen($firstPart) + strlen($secondPart) == 7) {
                    $sqlQuery = $mysqli->query("SELECT `postcode`, `structure` FROM `cwnational` WHERE `postcode` LIKE '".$firstPart." ".$secondPart."'");
                } else {
                    $sqlQuery = $mysqli->query("SELECT `postcode`, `structure` FROM `cwnational` WHERE REPLACE(`postcode`, ' ', '') LIKE '".$searchTerm."%'");
                }
            }

            if (is_object($sqlQuery) && $sqlQuery->num_rows >= 1) {
                $resultArr = array();
                while($row = $sqlQuery->fetch_assoc()) {
                    $resultArr[$row['postcode']] = array();
                    $priceQuery = $mysqli->query("SELECT `base_rate`, `commit_mbps` FROM `pricing` WHERE `structure` = '".$row['structure']."'");
                    if ($priceQuery->num_rows >= 1) {
                        while ($price = $priceQuery->fetch_assoc()) {
                            $resultArr[$row['postcode']][$price['commit_mbps']] = ((float)$price['base_rate'] + $transit[$price['commit_mbps']] ) + ( ( $transit[$price['commit_mbps']] + (float)$price['base_rate'] ) * ((float)$apiUser['margin']/100)  )   ;
                        }
                    }
                }
4

2 回答 2

2

您正在读取结果集两次。这将适用于第一个循环,但不会执行第二个循环,因为您已经到达终点。如果您需要两次读取结果,请使用以下命令:

mysql_data_seek ($raw_results , 0 )

在第二个循环之前立即将指针再次设置为开头。

当然,你应该使用mysqli ...

于 2013-07-02T22:55:36.503 回答
1

现在出了什么问题?我看到你需要在这里添加一个等号:

if($results['structure'] == "National") {

注意:双等于 php 条件

看起来您在“一段时间”中有一个“其他”,这是行不通的。你做了这个“while”循环两次,我将它们组合成一个“While”:
while($results = mysql_fetch_array($raw_results)){

于 2013-07-02T22:47:57.720 回答