在php数组中搜索元素

I've got

  • a users table named "members"
  • a rooms table named "rooms"
  • a table that associates the user id to the ids of the rooms "membersRooms"

I should write a loop that prints a dropdown for each user with all the rooms, but that adds the attribute "selected" to rooms associated with the user

What's wrong with this loop?

$members = mysql_query("SELECT * FROM members ");
$rooms = mysql_query("SELECT * FROM rooms");

while($member = mysql_fetch_array($members)){ 
    echo("<select>");
    $roomsOfUser = mysql_query("SELECT roomID FROM membersRooms WHERE userID=".$member["id"]);
    $cuArray = mysql_fetch_array($roomsOfUser); 

    while($room = mysql_fetch_array($rooms)){
        if(in_array($room["id"],$cuArray,true))
            echo("<option selected='selected'>".$room["roomName"]."</option>");
        else
            echo("<option>".$class["roomName"]."</option>");
    }
    echo("</select>");
}

To make this a little easier on you, you could try utilizing left and right joins on your database. This would significantly reduce your server load and still allow you to do the same functionality.

I believe, if I'm reading your database structure right, that you'ld want something along the lines of:

 SELECT members.id as memberID, rooms.id as roomID, rooms.roomName, membersRooms.roomID as memberRoom
 FROM members
 LEFT JOIN membersRooms
 ON members.id = membersRooms.userID
 RIGHT JOIN rooms
 ON membersRooms.roomID = rooms.id

Then in PHP you should be able to just keep track of when your memberID changes, and when it does, start a new select. If I didn't totally bungle that SQL (which I might have) then the resulting rows should look something like:

memberID    |   roomID  |  roomName  |  memberRoom
    1             1          foo             1
    1             2          bar             1
    2             1          foo             1
    2             2          bar             1

So on your loop iteration you would use roomID and roomName to build your select, and if RoomID matched memberRoom then you would select that row.

$rooms query while is dead while runs once time in while

put this $rooms = mysql_query("SELECT * FROM rooms"); query line in first while

OK, so you need information from 3 tables - members, rooms, and membersRooms. The rows from members and membersRooms line up 1:1, so we can get both of those with 1 query.

This method will minimize the number of queries needed - if you ever see yourself querying the database in a loop, ask yourself if there's a better way.

$member_query = mysql_query("SELECT * FROM members LEFT JOIN membersRooms ON (members.id = membersRooms.userID)");

$room_query = mysql_query("SELECT * FROM rooms");
$rooms = array();
while ($room = mysql_fetch_assoc($room_query))
  $rooms[] = $room;

while ($member = mysql_fetch_assoc($member_query)) {
  echo '<select>';
  foreach($rooms as $room) {
    echo "<option value='{$room['roomID']}' ";
    if ($member['roomID'] == $room['id'])
      echo 'selected="selected"';
    echo ">{$room['roomName']}</option>";
  }
  echo '</select>';
}

It's worth noting that if members:rooms is a 1:many relation, you don't need to use a third table to join them - just add a roomId to members, and you're fine.