2013-12-08 242 views
0

我想用count方法只回顯一個div,一旦mysql查詢顯示8個結果,否則如果顯示7或更少不顯示此div。只顯示div,如果mysql結果顯示8個結果?

繼承人什麼我嘗試做有人可以告訴我在哪裏,我錯了感謝:

<?php 
//PHP CODE STARTS HERE 

if(isset($_GET['dosearch'])){ 

// Change the fields below as per the requirements 
$db_host="*********"; 
$db_username="******"; 
$db_password="******"; 
$db_name="*****"; 
$db_tb_atr_name="******"; 

//Now we are going to write a script that will do search task 
// leave the below fields as it is except while loop, which will display results on screen 

mysql_connect("$db_host","$db_username","$db_password"); 
mysql_select_db("$db_name"); 

$query=mysql_real_escape_string($_GET['query']); 


$query_for_result=mysql_query("SELECT *,MATCH(display_name, user_location, sexual_orientation, user_ethnicity, preferred_role, local_station, user_size, weight_st, weight_lb, height_ft, height_in, user_build, user_age) AGAINST ('$query' IN BOOLEAN MODE) AS relevance FROM ptb_stats, ptb_users WHERE ptb_stats.user_id=ptb_users.user_id AND ptb_users.account_type=\"user\" AND MATCH(display_name, user_location, sexual_orientation, user_ethnicity, preferred_role, local_station, user_size, weight_st, weight_lb, height_ft, height_in, user_build, user_age) AGAINST('$query' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT 8"); 
echo "<div class=\"search-results\"><div id=\"content2_header\">Members Matching Your Search</div>"; 
$platinum_count = mysql_num_rows($query_for_result); 
while($data_fetch=mysql_fetch_array($query_for_result)) 

{ 

    echo"<div class=\"image_case\"><a href=\"profile.php?id={$data_fetch['user_id']}\"><img width=93px height=93px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\"></a> 
<div id=\"mod_newest_text12\">{$data_fetch['display_name']}</div> 
<div id=\"mod_newest_text14\">{$data_fetch['user_location']}</div></div> 
"; 


} 
// only if there are more than 8 users do we want to show our div 
    if($platinum_count > 7){ 
     // how many default spaces do we need? 
     $default_profiles_needed = 7 - $platinum_count;   
     for($i = 1; $i <= $default_profiles_needed; $i++){ 
      echo "<div class=\"morebutton-search21\"><a href=\"search_results.php?query=$query\" class=\"more\">+ view more results</a></div>"; 
     } 
    } 


mysql_close(); 
} 

?> 

回答

0

首先,不要使用mysql_ *函數,因爲它們是deprecated

考慮使用PDO(最好)或mysqli來代替。有些原因參見this,以及一些代碼示例。

現在讓我們來看看你的代碼,以PDO重新想象:

if(isset($_GET['dosearch'])){ 

    // Change the fields below as per the requirements 
    $db_host="*********"; 
    $db_username="******"; 
    $db_password="******"; 
    $db_name="*****"; 
    $db_tb_atr_name="******"; // what is this? 

    $dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_password); 

    $query = //I can't read your query, but you should convert it to a prepared statement with ? or :id formats 

    $stmt = $dbh->prepare($query); 
    $result = $stmt->execute($_GET["query"]); // PDO handles escaping in prepared statement; put your data into the prepared statement's parameters 

擷取結果,並計算邏輯:

if($row = $result->fetchAll()) { 
     // do stuff if results 

     if(count($row) >= 8){ 
      // do stuff if 8 or more results 
     } 
    } 
?> 

這僅僅是PDO一些粗糙的代碼,鏈接給一些更多的例子,如連接try/catch塊和捕獲錯誤。