我有一个简单的部分,其中显示数据库中的数据,我的数据库如下所示。
现在我有四个看起来像这样的按钮
用户单击上述按钮之一时,将显示此
因此,现在当用户(例如)选择construction
并下一步选择(例如)Egypt' in the console and clicks button
确认displays [855,599075], user can select multiple countries, this works as expected for
构造,
功率,
油时,
现在我想要的是,如果用户例如在这四个按钮中单击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;
}
});
}
});
这里是php,用于获取所有dataall.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]
的国家/地区时。
我需要更改以获得我想要的东西吗?任何帮助或建议,将不胜感激,
答案 0 :(得分:2)
您可以使用SQL JOIN运算符,否则在这种情况下,隐式联接将是最干净的:
$result = mysqli_query($conn, "SELECT * FROM construction, power, oil_and_gas, industrial WHERE construction.Countries = power.Countries AND power.Countries = oil_and_gas.Countries AND oil_and_gas.Countries = industrial.Countries");
您需要WHERE条件,以便它知道每个不同表的行如何相互关联。您可以使用表的别名将其缩短一点:
$result = mysqli_query($conn, "SELECT * FROM construction as C, power as P, oil_and_gas as G, industrial as I WHERE C.Countries = P.Countries AND P.Countries = G.Countries AND G.Countries = I.Countries");
但是,在这种情况下,我认为您可能需要考虑更改数据库的结构。好像您在它们之间重复了很多列。也许这些都可以放在一个表中,并带有一个“类型”列来指定它的功能,构造等。然后,您只查询一个表并按国家/地区名称分组即可获得所有结果,而不会造成凌乱的跨4个联接表。
答案 1 :(得分:1)
您的dataAll.php
如果您选择了All available industries
您不检查扇区(最终您应该检查国家)
因此您应该避免检查这种情况
<?php
$conn = mysqli_connect("localhost", "root", "", "love");
$result = mysqli_query($conn, "SELECT * FROM meed");
$data = [];
$wynik = [];
$totalProjects = 0;
$totalBudget = 0;
while ($row = mysqli_fetch_array($result)) {
$totalProjects += $row['SumofNoOfProjects'];
$totalBudget += $row['SumofTotalBudgetValue'];
}
echo json_encode([$totalProjects, $totalBudget]);
答案 2 :(得分:0)
单个表看起来不错。
(此答案的其余部分尚未完成,但可能很有用。)
首先,让我们设计将请求数据的URL。
.../foo.php?industry=...&country=...
但是,与其在客户端中特殊包装“全部”,不如在服务器中完成。也就是说,将生成用于工业的最后一个按钮
?industry=all
,PHP代码不会在WHERE
子句中包含此代码:
AND industry IN (...)
与&country=all
和&country=egypt,iran,iraq
相似
现在,让我简要介绍一下PHP:
$wheres = array();
$industry = @$_GET['industry'];
if (! isset($industry)) { ...issue error message or use some default... }
elseif ($industry != 'all') {
$inds = array();
foreach (explode(',', $industry) as $ind) {
// .. should test validity here; left to user ...
$inds[] = "'$ind'";
}
$wheres[] = "industry IN (" . implode(',', $inds) . )";
}
// ... repeat for country ...
$where_clause = '';
if (! empty($wheres)) {
$where_clause = "WHERE " . implode(' AND ', $wheres);
}
// (Note that this is a generic way to build arbitrary WHEREs from the data)
// Build the SQL:
$sql = "SELECT ... FROM ...
$where_clause
ORDER BY ...";
// then execute it via mysqli or pdo (NOT mysql_query)
现在,让我们谈谈使用AJAX。或不。有2个选择:
GET
来调用PHP,并让PHP显示一个新页面。这意味着PHP将构建结果表。哪种选择可能取决于您更喜欢哪种语言。