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 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 MAC虚拟机(win11)USB插上后无串口com,无法烧录