如何使用PHP和我的sql在循环中动态设置图像标记内的值

时间:2016-02-11 13:02:13

标签: php mysql

我需要一个帮助。我需要使用PHP在i.e-image,class元素内动态设置一些DB值(li)。我在下面解释我的代码。

<ul class="sequence-canvas">
  <li class="animate-in">
    <div class="intro">
      <h2>My Discount Pharmacy //set title from db</h2>
      <h3>
We are an end to end healthcare service provider where the doctors can enter their prescriptions, patients can upload their prescriptions. //set subtitle
      </h3>
    </div>
    <img class="iphone" src="img/seq-slider/iphone.png" alt="iPhone4" /> //set image1 from DB
    <img class="iphone-shadow" src="img/seq-slider/iphone-shadow.png" alt="" /> //set image2 from DB
  </li>
  <li>
    <img class="ipad" src="img/seq-slider/ipad.png" alt="iPad" />//set image1 from DB
    <div class="slide2">
      <h2>My Discount Pharmacy // set title from DB</h2>
      <p>
We are an end to end healthcare service provider where the doctors can enter their prescriptions, patients can upload their prescriptions. //Set subtitle from DB.
      </p>
    </div>
    <img class="ipad-shadow" src="img/seq-slider/ipad-shadow.png" alt="" />  //set image2 from DB
  </li>
</ul>

从上面的代码中我需要在循环中迭代每组li元素并动态设置一些值。我在下面解释我的php代码。

<?php 
$sql = mysql_query("select * from phr_news order by news_id desc");

while ($row=mysql_fetch_array($sql)) {
    $title = $row['title'];
    $subtitle = $row['sub_title'];
    $image1 = $row['image1'];
    $image2 = $row['image2'];
}
?>

这里我需要遍历循环中的每个li元素并在适当的位置设置上述值。请帮助我。

3 个答案:

答案 0 :(得分:0)

我认为此代码可以帮助您获得所需的输出

<?php 
 $sql=mysql_query("select * from phr_news order by news_id desc");
while($row=mysql_fetch_array($sql)){ ?><li class="animate-in">
        <div class="intro">
          <h2>
            <?php $row['title']; ?> //set title from db
          </h2>
          <h3>
            <?php $row['sub_title']; ?> //set subtitle
          </h3>
        </div>
        <img class="iphone" src="img/seq-slider/<?php $row['image1']; ?>" alt="iPhone4" /> //set image1 from DB
        <img class="iphone-shadow" src="img/seq-slider/<?php $row['image2']; ?>" alt="" /> //set image2 from DB
      </li>
      <li>
        <img class="ipad" src="img/seq-slider/<?php $row['image1']; ?>" alt="iPad" />//set image1 from DB
        <div class="slide2">
          <h2>
            <?php $row['title']; ?> // set title from DB
          </h2>
          <p>
            We are an end to end healthcare service provider where the doctors can enter their prescriptions, patients can upload their prescriptions. //Set subtitle from DB.
          </p>
        </div>
        <img class="ipad-shadow" src="img/seq-slider/<?php $row['image2']; ?>" alt="" />  //set image2 from DB
      </li>
<?php } ?>

答案 1 :(得分:0)

以下是一个如何做到这一点的例子(尽管未经过测试):

<?php
$sql=mysql_query("select * from phr_news order by news_id desc");

echo '<ul class="sequence-canvas">' . PHP_EOL;
while($row=mysql_fetch_array($sql)){ 
    echo '<li class="animate-in">
        <div class="intro">
          <h2>' . $row['title'] . '</h2>
          <h3>' . $row['sub_title'] . '</h3>
        </div>
        <img class="iphone" src="' . $row['image1'] . '" alt="iPhone4" />
        <img class="iphone-shadow" src="' . $row['image2'] . 'alt="" />
      </li>
';
    echo '<li>
        <img class="ipad" src="' . $row['image1'] . '" alt="iPad" />
        <div class="slide2">
          <h2>' . $row['sub_title'] . '</h2>
          <p>' . $row['sub_title'] . '</p>
        </div>
        <img class="ipad-shadow" src="' . $row['image2'] . ' alt="" />
      </li>
';
} // end while
echo '</ul>' . PHP_EOL;
?>

答案 2 :(得分:0)

如果我理解你的问题,你就应该这样做。

你的php代码

<?php 
     $phr_news = array();
     $sql=mysql_query("select * from phr_news order by news_id desc");
     while($row=mysql_fetch_array($sql)){ 
        array_push("title" => $row['title'], 
                   "subtitle" => $row['sub_title'], 
                   "image1" => $row['image1'], 
                   "image2" => $row['image2']
        );
     }
?>

这里是视图html

<ul class="sequence-canvas">
        <?php foreach ($phr_news  => $phr_new): ?>
            <li class="animate-in">
              <div class="intro">
                <h2>
                  <?php echo $phr_new['title'] ?>
                </h2>
                <h3>
                  <?php echo $phr_new['subtitle'] ?>
                </h3>
              </div>
              <img class="iphone" src="<?php echo $phr_new['image1'] ?>" alt="iPhone4" /> 
              <img class="iphone-shadow" src="<?php echo $phr_new['image2'] ?>" alt="" /> 
            </li>
        <?php endforeach ?>
    </ul>

希望它会有所帮助