2012-03-06 18 views
1

所以,我真的很困惑這裏。我不知道我在做什麼,但是,我試圖限制基於網絡的搜索是私有的還是公共的。第二次嘗試限制mySQL和PHP中的搜索

此代碼不起作用,但是,我試圖弄清楚如何使這項工作。我如何才能使私人網絡只出現在該網絡中的人????????????????

$query = mysql_query("SELECT * FROM requests ORDER BY dateposted DESC"); 

while ($row = mysql_fetch_assoc($query)){ 

    $network = mysql_query("SELECT * FROM network"); 

    while ($row2 = mysql_fetch_assoc($network)) { 
     $display = false; 
     $pos = strpos($row['network'], $search); 
     if ($row2['visible'] == "public" and $search == "Global") { 
      $display = true; 
     } else   
     if ($pos == true and 
     ($row2['visible'] == "public" or $row2['username'] == $username)) { 
      $display = true; 
     } else 
     if ($row2['visible'] == "private") { 
      $display = false; 
     } 
    } 
     echo $display; 
     if ($display == true){ 
      echo "<div id='".$row["id"]."' class='item'>"; 
      echo '<div style="width: 75%;">'; 
      echo "<h4 style='display: inline;'>".$row["user"]." requests:</h4><br/>"; 
      echo "<h2 style='display: inline;'>".$row["title"]." </h2>"; 
      echo '</div>'; 

      echo '<h3 id="button" style="border-radius: 10px; padding: 4px; color: white; float: right; position: relative; top: -50px; border: 2px solid grey; background-color: #C0504D;">Apply</h2>'; 
      echo "</div>"; 
     } 
} 

網絡架構:

network text utf8_general_ci  No         
username text utf8_general_ci  No         
visible text utf8_general_ci  No 

的請求模式:

id int(11)   No  auto_increment       
user text utf8_general_ci  No         
title text utf8_general_ci  No         
description text utf8_general_ci  No         
picture text utf8_general_ci  No         
price text utf8_general_ci  No         
premium datetime   No         
type text utf8_general_ci  No         
network text utf8_general_ci  No         
dateposted datetime   No 
+0

我無法發現一個明顯的錯誤,但是如果您在預期錯誤時得到真正的結果(或反之亦然),請檢查您在strpos($ row ['network'],$ search) )值得注意的是(儘管它並不適用於這種情況),如果strpos可以給出意想不到的結果不使用精確比較(===或!==),因爲當返回的0對於在位置0處找到的字符串確實是'真'結果時可以評估爲'假'。 – fred2 2012-03-06 02:37:50

回答

1

這是一個有點晚了這裏,所以我會盡量最好寫這個答案。

您需要打印所有公共網絡,並且只需要打印與用戶相關的專用網絡。此外,如果我瞭解,您的搜索可以是「全球」或具有網絡的部分名稱,是否正確?

所以,你的查詢可以是這樣的:

if($search == "Global") { 
    $sql = "select n.* 
       , r.* 
      from network n 
      inner join request r on n.network = r.network 
      where n.network = 'public' 
       or (n.network = 'private' 
      and n.username = '".$username."')"; 
} else { 
    $sql = "select n.* 
       , r.* 
       from network n 
      inner join request r on n.network = r.network 
      where n.network = 'public' 
       or (n.network = 'private' 
       and n.username = '".$username."') 
       and n.network like '%".mysql_real_escape_string($search)."%'"; 
} 

like這裏的意思是「選擇我在哪裏,網絡名稱包含搜索內容的所有數據,您可以看到更多的例子和解釋here,並在mysql docs

+0

因此,請等待,什麼是網絡n?我不明白。 n。意味着我可以一次從多個表中進行選擇? – CCates 2012-03-06 03:09:36

+0

不,在這種情況下n是表的別名。 – 2012-03-06 03:16:09

+0

哦,我明白了。好的,如果我這樣做了,我怎麼過濾私人網絡? – CCates 2012-03-06 03:26:32