【发布时间】:2019-07-21 16:08:28
【问题描述】:
我有一个简单的部分,我在其中显示来自数据库的数据,
如果用户点击例如建筑并选择例如阿尔及利亚,我将显示[251, 211,712] 如果用户点击例如电源并选择例如。埃及 我正在显示[406, 228,559] 等
现在我想如果用户单击按钮All available industries 并选择例如。阿尔及利亚 我想在 SQL 中以这样的简单方式显示这个[251+203+130, 211,712+179,877+154,946]
SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value FROM `meed` WHERE Countries = 'Algeria'
给我这个[611, 546535]
这是我的解决方案
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;
}
});
}
});
更新这里是更新的data.php
<?php
$selectedSectorByUser = $_POST['sector'];
$countries = explode(",", $_POST['countries']);
echo '$countries';
$conn = mysqli_connect("localhost", "root", "", "meedadb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$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 ) ) {
$totalProjects+= $row['SumofNoOfProjects'];
$totalBudget+= $row['SumofTotalBudgetValue'];
}elseif($selectedSectorByUser =="All available industries"){
$result = mysqli_query($conn,
"SELECT sum(SumofNoOfProjects) as 'SumofNoOfProjects, sum(SumofTotalBudgetValue) as SumofTotalBudgetValue
FROM `meed`
WHERE Countries = '$countries'");
while( $row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo json_encode([ $row['SumofNoOfProjects,'], $row['SumofTotalBudgetValue '] ] );
exit;
}
exit;
}
}
echo json_encode([ $totalProjects, $totalBudget ] );
exit();
?>
现在当用户点击All available industries btn 并选择一个国家时,我收到以下错误
b>致命错误:未捕获的错误:无法使用 mysqli_result 类型的对象作为 C:\custom-xammp\htdocs\editor\data.php:23 中的数组
我需要改变什么才能得到我想要的?任何帮助或建议将不胜感激,
【问题讨论】:
标签: javascript php jquery mysql ajax