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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。