【发布时间】:2019-11-26 16:57:23
【问题描述】:
我有一个简单的部分,我在其中显示来自数据库的数据,我的数据库看起来像这样。
现在我有四个按钮,看起来像这样
当用户单击上述按钮之一时,它会显示此
所以现在当用户选择 construction 并接下来选择 Egypt' in the console and clicks buttonconfirmdisplays [855,599075], user can select multiple countries, this works as expected forconstruction ,power,oil`,
现在我想如果用户例如单击这四个按钮中的All available industries 按钮,然后选择例如Egypt 并单击confirm 它应该显示
埃及建筑、石油、电力部门总项目的总和855+337+406 =1598 和两个部门的总预算总和1136173
这是我的解决方案
HTML
<div id="interactive-layers">
<div buttonid="43" class="video-btns">
<span class="label">Construction</span></div>
<div buttonid="44" class="video-btns">
<span class="label">Power</span></div>
<div buttonid="45" class="video-btns">
<span class="label">Oil</span></div>
<div buttonid="103" class="video-btns">
<span class="label">All available industries</span>
</div>
</div>
这里是js ajax
$("#interactive-layers").on("click", ".video-btns", function(){
if( $(e.target).find("span.label").html()=="Confirm" ) {
var selectedCountries = [];
$('.video-btns .selected').each(function () {
selectedCountries.push( $(this).parent().find("span.label").html() ) ;
});
if( selectedCountries.length>0 ) {
if(selectedCountries.indexOf("All available countries")>-1) {
selectedCountries = [];
}
} else {
return;
}
var ajaxurl = "";
if(selectedCountries.length>0) {
ajaxurl = "data.php";
} else {
ajaxurl = "dataall.php";
}
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
countries: selectedCountries.join(","),
sector: selectedSector
},
success: function(result){
console.log(result);
result = JSON.parse(result);
$(".video-btns").each(function () {
var getBtn = $(this).attr('buttonid');
if (getBtn == 106) {
var totalProjects = $("<span class='totalprojects'>"+ result[0] + "</span>");
$(this).append(totalProjects)
}else if(getBtn ==107){
var resultBudget = result[1]
var totalBudgets = $("<span class='totalbudget'>"+ '$m' +" " + resultBudget +"</span>");
$(this).append( totalBudgets)
}
});
return;
}
});
}
});
这里是获取所有dataall.php的php
$selectedSectorByUser = $_POST['sector'];
$conn = mysqli_connect("localhost", "root", "", "love");
$result = mysqli_query($conn, "SELECT * FROM meed");
$data = array();
$wynik = [];
$totalProjects = 0;
$totalBudget = 0;
while ($row = mysqli_fetch_array($result))
{
if($row['Sector']==$selectedSectorByUser ) {
$totalProjects+= $row['SumofNoOfProjects'];
$totalBudget+= $row['SumofTotalBudgetValue'];
}
}
echo json_encode([ $totalProjects, $totalBudget ] );
exit();
?>
这里是data.php
<?php
$selectedSectorByUser = $_POST['sector'];
$countries = explode(",", $_POST['countries']);
//var_dump($countries);
$conn = mysqli_connect("localhost", "root", "", "meedadb");
$result = mysqli_query($conn, "SELECT * FROM meed");
$data = array();
$wynik = [];
$totalProjects = 0;
$totalBudget = 0;
while ($row = mysqli_fetch_array($result))
{
if($row['Sector']==$selectedSectorByUser && in_array($row['Countries'],$countries ) ) {
// array_push($data, $row);
$totalProjects+= $row['SumofNoOfProjects'];
$totalBudget+= $row['SumofTotalBudgetValue'];
}
}
// array_push($wynik, $row);
echo json_encode([ $totalProjects, $totalBudget ] );
//echo json_encode($data);
exit();
?>
现在当用户点击All available industries btn 并选择一个国家时,我会在控制台上看到[0,0]。
我需要改变什么才能得到我想要的?任何帮助或建议将不胜感激,
【问题讨论】:
-
phpMyAdmin 是数据库前端,而不是数据库。您不会从中获取数据。
-
注意:object-oriented interface to
mysqli明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的mysql_query接口混淆,因为缺少单个i会导致麻烦。示例:$db = new mysqli(…)和$db->prepare("…")过程接口很大程度上是 PHP 4 时代引入mysqliAPI 时的产物,不应在新代码中使用。 -
如果要从多个表中获取数据,请执行多个查询或使用 JOIN。请注意,在与国家打交道时,通常最好使用数字标识符或标准化代码以避免歧义或拼写错误。 ISO-3166 指定每个国家/地区的代码。
-
更新您的问题并添加预期结果..(使用文本..不是图像)
-
用 bount 100 更新了问题检查
标签: javascript php jquery mysql ajax