2012-05-01 60 views
0

我試圖生成一個數組,但不知道如何去做。從while循環創建一個ID數組

我目前得到我的數據,像這樣:

$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE '[email protected]'"); 
$row = mysql_fetch_array($query); 

$query1 = mysql_query("SELECT * FROM categories"); 
while($row1 = mysql_fetch_array($query1)){ 

    $query2 = mysql_query("SELECT * FROM usersettings WHERE userId = ".$row['userId']." AND usersettingCategory".$row1['categoryId']." LIKE 'y'"); 
    $isyes = mysql_num_rows($query2); 

    if($isyes > 0){ 

     $cat1 = mysql_query("SELECT * FROM shops WHERE shopstateId = 1 AND (categoryId1 = ".$row1['categoryId']." OR categoryId2 = ".$row1['categoryId']." OR categoryId3 = ".$row1['categoryId'].")"); 
     $cat1match = mysql_num_rows($cat1); 

     if($cat1match > 0){ 
      while($cat1shop = mysql_fetch_array($cat1)){ 
       $cat1msg = mysql_query("SELECT * FROM messages WHERE shopId = ".$cat1shop['shopId']." and messagestateId = 1"); 
       while($cat1msgrow = mysql_fetch_array($cat1msg)){ 

        echo $cat1msgrow['messageContent']." - ".$cat1msgrow['messageCode']; 
        $cat1img = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$cat1shop['shopimageId']); 
        $imgpath = mysql_fetch_array($cat1img); 
        echo " - ".$imgpath['shopimagePath']."<br/>"; 
       } 
      } 
     } 
    } 
} 

但是當用戶有一個商店類別自己的喜好挑選所有3這可能會導致重複。我試圖找到一種方法,只是將消息ID取出而不是整個東西,並將其放入數組中,例如:

1,3,5,7,1,3,5,2 ,4,7,8

然後,我可以運行一個單獨的查詢來說讓我所有的消息,其中的ID是在數組中,但我不確定最有建設性的方式來構建這樣一個數組和數組的例子從一個while循環我看到似乎並不是我正在尋找。

有沒有人能把我推向正確的方向?

+0

爲什麼不使用! '內部連接',並且可以顯示錶格的結構.. – Rafee

回答

0

我已經找到了一個更簡單的方法來創建所需結果。我覺得在一個艱難的夜晚編程之後的早上6點,我的大腦被炸了,並且讓事情變得比我需要的複雜得多。我的問題的一個簡單的解決方案如下:

$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE '[email protected]'"); 
$row = mysql_fetch_array($query); 

$categories = "("; 

$query1 = mysql_query("SELECT * FROM categories"); 
while($row1 = mysql_fetch_array($query1)){ 

    $query2 = mysql_query("SELECT usersettingCategory".$row1['categoryId']." FROM usersettings WHERE userId = ".$row['userId']); 
    $row2 = mysql_fetch_array($query2); 

    if($row2['usersettingCategory'.$row1['categoryId']] == y){ 

     $categories .= $row1['categoryId'].","; 
    } 

} 

$categories = substr_replace($categories ,")",-1); 

echo $categories."<br />"; 

$query3 = mysql_query("SELECT * FROM shops,messages WHERE shops.shopId = messages.shopId AND messages.messagestateId = 1 AND (shops.categoryId1 IN $categories OR shops.categoryId2 IN $categories OR shops.categoryId3 IN $categories)"); 

while($row3 = mysql_fetch_array($query3)){ 

    $query4 = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$row3['shopimageId']); 
    $row4 = mysql_fetch_array($query4); 

    echo $row3['messageContent']." - ".$row3['messageCode']." - ".$row4['shopimagePath']."<br />"; 
} 
0

無法使用此代碼。但是,如果你想從沒有重複的結果的查詢一個數組,你可以用「SELECT DISTINCT(ID)」,在查詢或瞭解更多簡單的解決方案:

$id_arr = array(); 
$sql = mysql_query("select id from id_table"); 
while ($id_result = mysql_fetch_array($sql) { 
    $id = $id_result['id']; 
    if (!in_array($id, $id_arr)) { 
    $id_arr[] = $id; 
    } 
}