So in my code the $image is randomised like so:
$image = get_rand_img('images/featured_images/csgo/');
if I echo $image
, (bearing in mind I'm already inside PHP brackets) i.e:
echo'div id="featured-image">'
echo '<img src="/images/featured_images/dota/' . $image . '" alt="" />'
echo '</div>'
Then it works.
What I actually want to do, is assign this image to the div it's in
echo '<div id="featured-image" style="height: 267px; width: 292px">#
echo '<style="background:url('. $image .') "></style>'
echo '</div>'
This doesn't work. It draws the div but doesn't apply the background. I've tried various different things like "
What is wrong?
EDIT
The following code suggestions work.
<?php
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch (PDOException $e) {
echo $e->getMessage();
echo 'Could not establish a connection to the database.';
}
$query = $conn->prepare('SELECT `articleid`,`title` FROM `news_articles` WHERE featured = 1 ORDER BY RAND() LIMIT 1');
$array = array(
'N'
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_COLUMN, 0);
foreach ($results as $row) {
}
$image = get_rand_img('images/featured_images/csgo/');
$title = $result['title'];
echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?>
<div id="featured-image" style="height: 267px; width: 292px; background:url(/images/featured_images/csgo/<?php echo $image ?>)"></div>;
<?php
echo '</a>';
?>
Thanks guys!