dongyan8896 2016-01-09 23:42
浏览 21
已采纳

使用Variable设置后台URL - 我做错了什么?

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!

  • 写回答

2条回答 默认 最新

  • drau67562 2016-01-10 00:04
    关注

    There are a few things wrong here.

    The </style> tag in <style="background:url('. $image .') "></style> will trigger a Stray end tag "style" warning in HTML source.

    You also need to use PHP tagging and echoing the variable.

    Sidenote: I doubt you want to use 2x <style> tags here, rather than using all styles set as inlined.

    <div id="featured-image" style="height: 267px; width: 292px; background:url('<?php echo $image;?>')">
    

    If this isn't what you're looking for, I'll simply delete my answer.

    Edit: As per OP's comment that they're inside PHP, use and as an example:

    $image = "image.jpg";
    
    echo '
    <div id="featured-image" style="height: 267px; width: 292px; background:url('.$image.')">
    </div>
    ';
    

    Edit #2: and some questions in here too, because I fear commenting will go on forever.

    You're using what looks to be 2 paths here:

    $image = get_rand_img('images/featured_images/csgo/');
    

    and in (/images/featured_images/<?php echo $image ?>) which by the way, you're already in PHP here, so drop those <?php echo - ?>.

    You need to use the get_rand_img() function's path here and just the $image variable, not both paths.

    Sidenote: It's hard to say which path you want to use here.

    Do you really have a path called

    images/featured_images/csgo/images/featured_images/ - ??

    • images/featured_images/csgo/ ?? (and no slash like the one below)?
    • /images/featured_images/ ??

    Only you know what that is. So I suggest you do/try either

    $image = get_rand_img('images/featured_images/csgo/');
    
    $image = get_rand_img('images/featured_images/');
    

    Depending on which folder you want to extract the images from.

    This

    echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?>
    <div id="featured-image" style="height: 267px; width: 292px; background:url(/images/featured_images/<?php echo $image ?>)"></div>;
    

    Should have thrown you an error. You have a ;?> after $row . '">'; and that is closing your statement and also the PHP.

    So just do this:

    echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">
    <div id="featured-image" style="height: 267px; width: 292px; background:url('.$image.');"></div>';
    

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗