2013-07-02 57 views
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> 

我完全难倒现在..添加其他条件到MySQL循环

当我成功匹配$查询,我想用数组的第二部分应该是一列称为结构和使用它作为开关从数据库打印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) ) ; 
         } 
        } 
       } 
+0

作为一个小白这不是来不及躲避学习的坏习惯,你应该mysqli的 –

+0

喜乔启动,就感谢投入,我带你去考虑到这一点,你能否指出我在这个练习的正确方向? –

回答

1

什么错呢?我看你需要添加一个等号这里:

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

注:双等于为PHP条件

它也像你有一个“其他”来了“而”,即将无法工作。你做了这个“while”循环两次,我将它们合并成一个“While”:
while($results = mysql_fetch_array($raw_results)){

2

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

mysql_data_seek ($raw_results , 0) 

紧接在第二个循环之前将指针再次设置为开始。

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