将数据加载到html类中

时间:2018-06-21 06:03:19

标签: php jquery

我正在从mysql检索数据,需要在特定的类中显示结果。我正在使用jquery每10秒更新一次,这工作正常。我遇到的麻烦是将这些数据放入特定的类中:actions

如果有人能指出我正确的方向,我将不胜感激? PHP或jQuery的将是可以接受的。非常感谢

$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'"); 
  $rows = mysqli_fetch_assoc($sql);
  $num = $rows['total'];
  $ni = $num;

  if($ni < 1) {
    $ni = '0';
  } else {
    echo $ni; <--- NEED TO LOAD RESULT IN ACTIONS CLASS
  }

header.php中的html示例

<li>
  <a href="javascript:void(0);">Boxes <span class="drop-icon">▸</span> <label class="drop-icon" for="sm4" title="Toggle Drop-down">▾</label></a>
  <input id="sm4" type="checkbox">
  <ul class="sub-menu">
    <li>
      <a href="domain/admin/newintake.php" title="Add">New Intake <span style="float: right;" class="notification ni"><?php echo $ni_num; ?></span></a>
    </li>
    <li>
      <a href="domain/admin/bretrieval.php" title="Retrievals">Retrievals <span style="float: right;" class="notification retrievals"><?php echo $brtv_num; ?></span></a>
    </li>
    <li>
      <a href="domain/admin/breturn.php" title="Returns">Returns <span style="float: right;" class="notification returns"><?php echo $brtn_num; ?></span></a>
    </li>
    <li>
      <a href="domain/admin/bdestruct.php" title="Destructions">Destructions <span style="float: right;" class="notification destructions"><?php echo $bdstr_num; ?></span></a>
    </li>
    <li>
      <a href="domain/admin/bpretrieval.php" title="Permanent Box Retrieval">Permanent Retrieval <span style="float: right;" class="notification pretrieval"><?php echo $prtv_num; ?></span></a>
    </li>
  </ul>
</li>

loadActions.php中的示例

$sql= mysqli_query($conn,"SELECT count(*) as total FROM act WHERE new = '1'"); 
  $rows = mysqli_fetch_assoc($sql);
  $num = $rows['total'];
  $ni = $num;

  if($ni < 1) {
    $ni = '0';
  } echo $ni;

  $nisql= mysqli_query($conn,"SELECT count(*) as intake FROM act WHERE activity='New Intake' AND new = '1'"); // provide db connection object as first parameter
  $ni_row = mysqli_fetch_assoc($nisql);
  $ninum = $ni_row['intake'];
  //echo $num;
  $ni_num = $ninum;

  if($ni_num < 1) {
    $ni_num = '0';
  } echo $ni_num;

  $brtvsql= mysqli_query($conn,"SELECT count(*) as brtv FROM act WHERE activity='Box Retrieval' AND new = '1'"); // provide db connection object as first parameter
  $brtv_row = mysqli_fetch_assoc($brtvsql);
  $brtvnum = $brtv_row['brtv'];
  //echo $num;
  $brtv_num = $brtvnum;

  if($brtv_num < 1) {
    $brtv_num = '0';
  } echo $brtv_num;

  $brtnsql= mysqli_query($conn,"SELECT count(*) as brtn FROM act WHERE activity='Box Return' AND new = '1'"); // provide db connection object as first parameter
  $brtn_row = mysqli_fetch_assoc($brtnsql);
  $brtnnum = $brtn_row['brtn'];
  //echo $num;
  $brtn_num = $brtnnum;

  if($brtn_num < 1) {
    $brtn_num = '0';
  } echo $brtn_num;

1 个答案:

答案 0 :(得分:1)

请注意,我不是PHP程序员,所以您必须自己做一些工作 在下面作为伪代码,然后问另一个PHP问题,如果PHP没有意义。

  • 有一个请求

filterrific(
default_filter_params: {},
available_filters: [
  :with_mother_tongue,
  :with_locality,
  :with_start_time_gte
]
)

scope :with_mother_tongue, -> (search_string) { joins(:users).where("users.mother_tongue LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}

scope :with_locality, -> (search_string) { joins(:users).where("users.locality LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}

scope :with_start_time_gte, -> (ref_date) { joins(:availabilities).where('availabilities.start_time >= ?', ref_date) }

结果应为

select activity,
    count(*) total,
    sum(activity = 'New Intake') intakeCount,
    sum(activity = 'Box Retrieval') boxCount,
    ...
from act WHERE new = '1'

if ($rec["total"] == 0) {
  echo '{ "total" : 0 }';
  die 0;
}
$res = array(
  "total"      => $rec["total"], 
  "ni"         => $rec["intakeCount"],
  "retrievals" => $rec["boxCount"],
  ...
);
echo json_encode($res);

那你就可以做

{ "total"      : 24,
  "ni"         : 14,
  "retrievals" : 9,
  .... 
}
function getBoxes() {
  $.get('/domain/admin/loadActions.php', function(data) {
    processData(data);
    setTimeout(getBoxes, 15000);
  });
}
function processData(data) {
  for (key in data) {
    console.log(key,data[key])
    $("." + key).text(data[key]);
  }
}
$(function() { // page load

  // testing - remove this when running the getBoxes():
  processData({
    "total": 24,
    "ni": 14,
    "retrievals": 9
  });

  // getBoxes(); // remove comment when tested

});