In a widget where I show just the last two or three gallery photos added to the database I use the lightbox functionality where you can indeed show the last two or three results (based on the query offcource). and the other results are actually hidden using the data-remote functionality. So when you click on one of the results shown, the gallery opens and all images are present… So I need basically two functions The first one with the two or three last photos which I have allready:
public function get_recente_photos()
{
$sql = "SELECT *
FROM `gallery_photos`
ORDER BY `photo_id` DESC
LIMIT 3";
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll();
}
So what I now basically need is a second function with the rest results from the same table but where the id’s are not in the other function. Something like:
public function get_other_photos()
{
$sql = "SELECT *
FROM `gallery_photos`
WHERE `photo_id` NOT IN **?**;
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll();
}
But I am stucked at the question mark. How do I declare that the photo_id should not be the same as the id’s from the function get_recente_photos()
It would make more sense for you to have one function/method that retrieves all of the photo ids, then modify get_recente_photos() to retrieve from key 0 to 2 and modify get_other_photos() to retrieve keys >= 3, if any.
Perhaps use array_slice($data, 0, 3) for the recent ids and array_slice($data, 3) for the rest, where $data is your returned array of contents from the gallery_photos table.
If you implode() the three photo_id values that you get from the first query into a comma-separated string, could you use that value for the ? in your second query?
@droopsnoot Excuse my ignorance. My PHP knowledge is not that good that I understand this. Again sorry.How would that look like in the example I gave before?
That will give you a comma-separated string containing the ids of the photos that the first query returns, which you can use in the second query either by passing it into the second function as a parameter, or if it’s in a class you could probably access $leave_out directly.
@droopsnoot. Thank you for the responce. Sometimes the mind is not going as fast as I would like to. Both functions are within the same class (class Model_Page). I have the feeling that I understand what you say but can not bring it into practise Which is very frustrating. Is something like this is what you mean?
public function get_recente_photos()
{
$sql = "SELECT *
FROM `gallery_photos`
ORDER BY `photo_id` DESC
LIMIT 3";
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll();
}
public function get_other_photos()
{
$photos = get_recente_photos();
$pids = array();
foreach ($photos as $photo) {
$pids[] = $photo['photo_id'];
}
$leave_out = implode(",",$pids);
$sql = "SELECT *
FROM `gallery_photos`
WHERE `photo_id` NOT IN $leave_out;
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll();
}
That would probably do it, as long as you close the quotes on the end of the query. I’m not that experienced in PHP classes, so I wasn’t sure where you were calling the functions from. I’m also not sure off the top of my head whether you need to surround the list of values in your “NOT IN” clause with brackets () to make it work.
@TeNDoLLA. Thanks for the additional info. I noticed that Thank you for the input. @droopsnoot and @TeNDoLLA . It is working great now… Thank you both for the input. Very much appreciated