I have a CodeIgniter PHP application that shows two movie covers. Next to them is a “random movie” button in which AJAX is used to replace two films with a new set of films. You can continue to click on this button again and again and see how it continues to replace film cover images. The first two screens for display are set as default values, and they should not be displayed after the user clicked the random movie button. The problem is this: When you click the random movie button several times, it will take a lot of clicks to finally show a new cover. That is, the same cover will be returned several times in a row. Two different covers are selected from several different URLs, so they will rarely be interrupted at the same time. This allows me to know that it is refreshing, but the function returns the same movie several times. If I access a URL that is called via AJAX directly, I never see this happen because I used the Session class to store the last movie and excluded it from the SQL query (i.e. WHERE id NOT IN ($default_movie, $last_movie) ). Any idea why accessing the URL directly will work fine, but when calling through AJAX, do I see this behavior?
I know that this may not have been as clear as possible, so let me know if I can clarify something that doesn't make sense. I will add code if this helps. Thanks friends!
Request for a random movie:
SELECT * FROM (`movies`) WHERE `id` NOT IN (2, 10) ORDER BY RAND() LIMIT 1
Model Method:
public function getRandom($count = 1, $featured = FALSE, $series = FALSE, $exclude = 0, $last = 0) { $this->db->order_by('id', 'random'); $this->db->limit(1); $conditions = array(); if ($exclude > 0) { $conditions['id !='] = $exclude; } if ($last > 0) { if (!empty($conditions['id !='])) { $conditionsNotIn = "id NOT IN (" . $conditions['id !=']. ", $last)"; unset($conditions['id !=']); $this->db->where($conditionsNotIn); } else { $conditions['id !='] = $last; } } if ($featured) { $conditions['featured'] = 1; } if ($series) { $conditions['current_series'] = 1; } $movie = $this->db->get_where('movies', $conditions); $movie = $movie->row(); if (!is_null($movie)) { return $movie; } else { return FALSE; } }
source share