dongyangben6144 2014-06-25 20:36
浏览 4
已采纳

too long

at first I apologise for creating this topic. I did try to search the answer but I couldnt find the right solution.

I am reading data from mysql with ajax and everything is working with one div. The thing I can't get working is to load each variable into separate div.

I use this api.php for fetching the data from mysql.

<?php 
$host = "localhost";
$user = "root";
$pass = "";

$databaseName = "skuska";
$tableName = "hodnoty";

//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------

$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);

//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query('SELECT t.hodnota FROM hodnoty t ORDER BY t.id DESC LIMIT 1') or die('Invalid query: ' . mysql_error());         //query

while ($row = mysql_fetch_assoc($result)) {
    echo $row['hodnota'];
}

?>

This is the ajax script for updating the data.

$(document).ready(function() {
     $("#gettable").load("api.php");
   var refreshId = setInterval(function() {
      $("#gettable").load('api.php?randval='+ Math.random());
   }, 9000);
   $.ajaxSetup({ cache: false });
});

Then in html I am using div for showing the data

 <div id="gettable"></div>

I would like to use this but with more variables like data1, data2, data3

and then used div for each data so I could use more divs. For example:

<div id="data1"></div> 
<div id="data2"></div>

I understand html, a little bit of php but I am totally new in java.

Thank you for your help.

  • 写回答

2条回答 默认 最新

  • drw85135 2014-08-06 10:14
    关注

    do this take as many variables as you have div and store content in variables inside loops

    /******* a short example **************/
    $div1cnt="";
    $div2cnt="";
    while($SRC=mysqlii_fetch_object($link)){
    $divid=$SRC->id;
    if($divid==1){
    $div1cnt.="add more stuff that you want here";
    }
    else if($divid==2){
    $div2cnt.="add stuff to second div";
    }
    echo "<div id=\"div1\">{$div1cnt}</div>";
    /************and so on ******************/
    

    There is no JS requirement means it is mobile friendly and no double connection required to load second page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)