简体   繁体   English

回声背景图像内联样式未显示

[英]echo background-image inline style not showing

why is echoing background-image url does not work? 为什么回显背景图像网址不起作用? I'm trying to make each div with the same class have different background image 我正在尝试使具有相同类的每个div具有不同的背景图像

<?php
echo "<div class='paket_title' style='background-image :url(\"img1.jpg\"); '>$r[head]</div>";
 ?>

here is the list of codes I've tried after searching for solutions : 这是我在寻找解决方案之后尝试过的代码列表:

echo "<div class='paket_title' style='background-image :url(\"img1.jpg\"); '>$r[head]</div>";

echo "<div class='paket_title' style='background-image :url(\'img1.jpg\');'>$r[head]</div>";

echo "<div class='paket_title' style='background-image :url(\"img1.jpg\");'>$r[head]</div>";

echo '<div class="paket_title" style="background-image:url(\"img1.jpg\");">$r[head]</div>';

echo '<div class="paket_title" style="background-image:url(\'img1.jpg\');">$r[head]</div>';

echo '<div class="paket_title" style="background-image:url('img1.jpg');">$r[head]</div>';

echo '<div class="paket_title" style="background-image:url('img1.jpg');">$r[head]</div>';

echo "<div class='paket_title' style='background-image:url('img1.jpg');'>$r[head]</div>";

echo "<div class='paket_title' style='background-image:url("img1.jpg");'>$r[head]</div>";

echo '<div class="paket_title" style="background-image:url(img1.jpg);">$r[head]</div>';

echo "<div class='paket_title' style='background-image:url(img1.jpg);'>$r[head]</div>";

echo "<div class='paket_title' style='background-image :url(\"img1.jpg\"); '>" . $r['head'] . "</div>";

none of the above code is showing the image. 以上代码均未显示该图像。 I put the image in the same directory with this php file, even copy pasted the image to every subdirectory of my website folder. 我将图像与此php文件放在同一目录中,甚至将图像复制粘贴到我的网站文件夹的每个子目录中。

here is the full code for this : 这是完整的代码:

<?php
 if ($_GET['module']=='paketusaha'){
 $paket = mysql_query("SELECT * FROM paket_usaha ORDER BY id_paket DESC LIMIT 5");
  while ($r=mysql_fetch_array($paket)){
  $id_panel=1;
  $bg=$r['bgcolor'];
switch ($bg) {
  case "red" :
  echo "<div class='paket_title' style='background-image :url(\"img1.jpg\"); '>$r[head]</div>";
  break;

  case "blue" :
  echo "<div class='paket_title' style='background-image :url(\"img2.jpg\"); '>$r[head]</div>";
  break;

  case "yellow" :
  echo "<div class='paket_title' style='background-image :url(\"img2.jpg\"); '>$r[head]</div>";
  break;


}


 echo "
 <img src='paket_usaha/$r[gambar]'></img>
  <div class='harga-pkt'>$r[judul]</div>
   <div id='container'>
    <div class='expandable-panel' id='cp-$id_panel'>
    <div class='expandable-panel-heading bold' style='background-color : $r[headcolor]; color : white;' >
        <h2>klik disini untuk info paket<span class='icon-close-open'></span></h2>
     </div>
    <div class='expandable-panel-content' >
        <p>$r[isi]</p>
    </div>
</div>

"; $id++;} ?>

everything is working fine except the background-image not showing, I even changed it to just background and still no luck 除了没有显示背景图片,其他一切都正常,我什至将其更改为仅背景,仍然没有运气

Do it like this: 像这样做:

echo "<div class='paket_title' style='background-image :url(\\"img1.jpg\\"); '>" . $r['head'] . "</div>";

you must have a single quote for your $r['head'] and also a separator for your string and variable echo. 您必须为$ r ['head']加上单引号,并为字符串和变量echo提供分隔符。

alright I got a trick to make this work 好吧,我有一个技巧来使这项工作

first I make a CSS named after color for example : 首先,我制作一个以颜色命名的CSS,例如:

.red {
   background-image : url("img1.jpg");
  }
 .blues {
   background-image : url("img2.jpg");
  }

now that the CSS is ready, just add the class to each div like this : 现在CSS已经准备好了,只需将类添加到每个div即可,如下所示:

  <?php 
     switch ($bg) {
     case "red" :
      echo "<div class='paket_title red' >$r[head]</div>";
      break;

     case "blue" :
       echo "<div class='paket_title blues'>$r[head]</div>";
     break;

      }

that's it, each div now has different background image because each has specific CSS class that sets the background-image. 就是这样,每个div现在都有不同的背景图片,因为每个div都有设置背景图片的特定CSS类。

  1. first thing separate your css and php. 首先把你的css和php分开。
  2. and use php in html like given below to avoid the errors of ' and " . 并使用如下所示的html格式的php来避免'"的错误。

     <div class='paket_title' style='background-image :url(\\"img1.jpg\\"); '><?php echo $r[head];?></div> 

and you can also use an if statement for this; 您还可以为此使用if语句; first make css as you said in your answer. 首先按照您在回答中说的那样制作CSS。

<?php
while ($r=mysql_fetch_array($paket)){
   if($r % 2 == 0)){
?>
       <div class='paket_title red' ><?php echo $r[head];?></div>
<?php
   }else{
 ?>  
       <div class='paket_title blues' ><?php echo $r[head];?></div>
<?php
   }
}
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM