如何以适当的格式html从数据库中检索数据

时间:2015-12-20 16:10:19

标签: php css database

我在数据库中有产品表。产品名称,描述,功能等几个类别

例如,我希望以列表形式显示$obj_p->description,如

SKU:B00YO9QCSW第五代处理器!!

英特尔酷睿i5第五代2.2 / 2.7 Ghz处理器13.3" QHD屏幕

  1. Intel Integrated Graphics 4 GB RAM,
  2. 128 GB SSD无光驱
  3. 网络摄像头
  4. 蓝牙
  5. WiFi
  6. BackLit K / B
  7. Windows 8.1 Pro
  8. 存储的描述数据在数据库中出现如下格式!

    SKU:B00YO9QCSW

    第五代处理器!!

    Intel Core i5 5th Gen 2.2 / 2.7 Ghz处理器,

    13.3" QHD屏幕,英特尔集成显卡,

    4 GB RAM,128 GB SSD,无光驱,

    网络摄像头,蓝牙,WiFi,BackLit K / B,Windows 8.1专业版

    我已经完成了这样的代码!

     <div class="span4">
       <div class="product">
           <div class="product-image full-width">
           <a href="<?php echo(BASE_URL); ?>products/product_detail.php?productID=<?php echo($obj_p->productID); ?>">
           <img src="<?php echo(BASE_URL); ?>products/catalog/<?php echo("$obj_p->product_name/$obj_p->product_image"); ?>" alt="<?php echo($obj_p->product_name); ?>" title="<?php echo($obj_p->product_name); ?>" /></a></div>
           <div class="view-count full-width"><?php echo($obj_p->view_count); ?> views</div>
           <div class="product-price full-width">$ <?php echo($obj_p->unit_price); ?></div>
           </div>  
       </div> 
     <div class="span4">   
       <label><?php echo($obj_p->product_name); ?></label>
        <div class="product-description"> <p> <?php echo($obj_p->description); ?></p></div>
        <div class="spacer11pxH full-width"></div>
     </div>
    

    以下是表数据库和列的屏幕截图。

    Table preview column

    Table preview

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您希望以HTML文本显示数据库中的数据吗?如果是的话......

您是否尝试过使用AJAX从数据库中返回存储的数据?

您的HTML看起来像这样:

<body onLoad="getInformation()">
<div class="span4">
<div class="product">
<div class="product-image full-width">
<span id="productInformation"></span>
</div>
</div>
</div>
</body>

您的JavaScript看起来像这样:

function getInformation(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
     document.getElementById("productInformation").innerHTML = xhttp.responseText;
    }
}
var url = "http://yourPHPURL.com/getInformation.php";
xhttp.open("GET", url, true);
xhttp.send();
}

最后你的PHP / SQL代码看起来像这样:

<?php
$host = "localhost";
$user = "root";
$password = "";
//Connects to the database
$dbc = mysql_pconnect($host,$user, $password);

//Selects the database to use and opens it
$dbname = "DBname";
@mysql_select_db($dbname) OR die ('Cannot connect to database:'. mysql_error());

//Makes the required query to the database
$query = "SELECT * FROM database;";
$result = mysql_query($query);

//Returns the item searched for by the user to AJAX
while($row=mysql_fetch_array($result))
{ 
echo("Details:");
echo($row['product_name']);
echo(<br><br>);
echo($row['description']);
}
?>

希望这是你正在寻找的......