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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵