douqipi9704 2015-12-20 16:10
浏览 92
已采纳

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

I have product table in database. there are few categories such as product title, description, feature etc

For Example, i want to display $obj_p->description in list form like

SKU: B00YO9QCSW 5th Generation Processor !!

Intel Core i5 5th Gen 2.2 / 2.7 Ghz Processor 13.3" QHD Screen

  1. Intel Integrated Graphics 4 GB RAM,
  2. 128 GB SSD No Optical Drive
  3. Webcam
  4. Bluetooth
  5. WiFi
  6. BackLit K/B
  7. Windows 8.1 Pro

Stored data of description appeared in database as below format!

SKU: B00YO9QCSW

5th Generation Processor !!

Intel Core i5 5th Gen 2.2 / 2.7 Ghz Processor,

13.3" QHD Screen, Intel Integrated Graphics,

4 GB RAM, 128 GB SSD, No Optical Drive,

Webcam, Bluetooth, WiFi, BackLit K/B, Windows 8.1 Pro

I have done code like this!

 <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>

Here are Screenshots of table database and column.

Table preview column

Table preview

  • 写回答

1条回答 默认 最新

  • douye5949 2015-12-20 17:14
    关注

    If I'm understanding your question correctly, you want to display data from your database in HTML text? If so...

    Have you tried using AJAX to return the stored data from your database?

    Your HTML would look something like this:

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

    Your JavaScript would look something like this:

    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();
    }
    

    And finally your PHP / SQL code would look something like this:

    <?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']);
    }
    ?>
    

    Hopefully this is what you're looking for...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失