I am using the following query to display a background image for each page based on the page ID:
public function get_background($page_id)
{
$sql = "SELECT background
, description
FROM page_backgrounds
WHERE page_id = ?";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($page_id));
return $stmt->fetch();
}
That works well the only thing is for the index page (page_id = 1) I have mulptiple image in the database. I would like to display a random image for the index page, but still want to be able to use the same query for the other pages as well. So I need a ORDER BY RAND() clause that only will fire when page_id = 1. How do I do that?
The if looks good (mind you I’m not sure about the sql syntax )
but I don’t think the $pagina_id = NULL is a good idea, I think that’s why it isn’t working anymore.
Please try without, and if it still doesn’t work, post the modified code here and any sql error you might get.
Hi Guido. I tried it without the $pagina_id = NULL.Now the background from all pages is working, except the index page :(. This is the complete methode:
public function get_background($page_id)
{
$page_id;
$sql = "SELECT background
, description
FROM page_backgrounds
WHERE page_id = ?";
if ($page_id == 1)
{
$sql .= " ORDER BY RAND()
LIMIT 1
}
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($page_id));
return $stmt->fetch();
}
Hi Rudi. Here I am loosing you. maybe stupid but I don’t understand what you mean. Yes all other pages have just 1 image. The page_id’s are identified in the controller: