使用来自MYSQL

时间:2015-08-25 20:26:05

标签: php html mysql

我的数据库中有条目,我想在网站上显示。我想要实现的是:我有多个div(列表),其中应包含db的数据。每个列表都应包含db中下一个条目的数据。我是PHP的新手。

我现在拥有的是:

<?php
require_once 'dbconfig.php';

try {
 $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

 $sql = 'SELECT `id`,`slika`,`model`,`znamka`,`letnik`,`vozno`,`naslov_vozila`,`cena`, `trajanje`,`timestamp` FROM `oddaja` ORDER BY `timestamp` DESC';

 $q = $conn->query($sql);
 $q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe) {
 die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>

我在div中显示数据

<a class="inventory" href="inventory-listing.html"> 
<?php while ($r = $q->fetch()): ?>
      <div class="title"><?php echo htmlspecialchars($r['znamka'])?></div>  
<?php endwhile; ?>

div如何看待:

<div class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"> 
    <input type="checkbox" name="a" class="checkbox compare_vehicle" id="vehicle_2" /> 
    <label for="vehicle_2"></label>                                 
    <a class="inventory" href="inventory-listing.html"> 
    <div class="title">2009 Porsche Boxster Base Red Convertible</div>                                     
    <img src="images/car-2-200x150.jpg" class="preview" alt="preview">                                     
    <table class="options-primary">
        <tr> 
            <td class="option primary">Body Style:</td>
            <td class="spec">Convertible</td>
        </tr>                                         
        <tr> 
            <td class="option primary">Drivetrain:</td>
            <td class="spec">RWD</td>
        </tr>                                         
        <tr> 
            <td class="option primary">Engine:</td>
            <td class="spec">2.9L Mid-Engine V6</td>
        </tr>
        <tr> 
            <td class="option primary">Transmission:</td>
            <td class="spec">5-Speed Manual</td>
        </tr>
        <tr> 
            <td class="option primary">Mileage:</td>
            <td class="spec">26,273</td>
        </tr>
    </table>
    <table class="options-secondary"> 
        <tr> 
            <td class="option secondary">Exterior Color:</td>
            <td class="spec">Guards Red</td>
        </tr>
        <tr>
            <td class="option secondary">Interior Color:</td>
            <td class="spec">Platinum Grey</td>
        </tr>
        <tr> 
            <td class="option secondary">MPG:</td>
            <td class="spec">20 city / 30 hwy</td>
        </tr>
        <tr> 
            <td class="option secondary">Stock Number:</td>
            <td class="spec">590271</td>
        </tr>
        <tr> 
            <td class="option secondary">VIN Number:</td>
            <td class="spec">WP0AB2A74AL060306</td>   
        </tr>
    </table>
    <img src="images/carfax.png" alt="carfax" class="carfax" /> 
    <div class="price"> 
        <b>Price:</b><br> 
        <div class="figure">$34,995<br</div>
        <div class="tax">Plus Sales Tax</div>
    </div>
    <div class="view-details gradient_button"> 
        <i class='fa fa-plus-circle'></i> View Details 
    </div>
    <div class="clearfix"></div>                                     
    </a>                                 
    <div class="view-video gradient_button" data-youtube-id="3oh7PBc33dk">
       <i class="fa fa-video-camera"></i> View Video
    </div>   

我从db获取数据,但是当我想显示标题时,我从db获取每个条目。 我认为我应该做的事情: - 在查询上生成div的HTML并循环每一行。我如何为db中的每个ID执行此操作?有样品吗?

1 个答案:

答案 0 :(得分:1)

你需要回应整个div。

而不是:

<?php while ($r = $q->fetch()): ?>
<div class="title"><?php echo htmlspecialchars($r['znamka'])?></div>  
<?php endwhile; ?>

使用:

<?php while ($r = $q->fetch()): ?>
<?php echo '<div class="title">' . htmlspecialchars($r['znamka']) . '?></div>';  
<?php endwhile; ?>