I'm creating a class book list - there's a master list of books, and a list of the books that the class can be assigned. I need to create an array of the master list, remove the books that have already been assigned to the class, then create a select list of the remaining books. I have most of it working, but I'm stuck on a small part. First, I load the array (the book_id is the key, the book_title is the value):
while ($master_book = mysql_fetch_array($big_book_list)) {
$b_id = $master_book['master_book_list_id'];
$book_list[$b_id] = $master_book['book_title'];
}
then I remove the class's primary book (it's already been set):
unset($book_list[$primary_book]);
now I load an array of the secondary books that have already been assigned (primary and secondary books come from the same list):
$secondary_query = "select * from class_books cb, master_book_list mbl where class_id ='$class_id' and cb.book_id = mbl.master_book_list_id;";
$secondary_list = mysql_query($secondary_query);
Now I remove the books from $secondary_list from the main list:
while ($secondary_book = mysql_fetch_array($secondary_list)) {
$b_id = $secondary_book['book_id'];
unset($book_list[$b_id]);
}
at this point, print_r shows my array correctly. but this code:
foreach ($book_list as $book2) {
$the_key = key($book_list);
echo $the_key . ' and book2 is '. $book2 . '<br/>';
}
it will iterate the predicted number of times, but the key stays at the same value (which is '2', the first value in the array.
Can anyone see what I'm doing wrong?