我有4个选择字段,用户可以从中选择国家/地区。
这些选择字段中的每一个都是从数据库动态创建的。这意味着每个选项都是通过数据库获得的。
function addDropMenu($countryNo) {
$conn = new mysqli('localhost', 'xxx', 'zzz', 'aaa') or die ('Cannot connect to db');
$result = $conn->query("SELECT country_name,ISO_id from Country");
$dropMenu = "";
$dropMenu .= "<select style='width:100%' name='$countryNo' >"; // each box has unique id
$dropMenu .= "<option value=''></option>"; //adding an empty selection box
while ($row = $result->fetch_assoc()) {
$countryName = $row['country_name'];
$id = $row['ISO_id'];
$dropMenu .= '<option value="'.$id.'">'.$countryName.'</option>'; //giving the select options the iso id as value and select name
}
$dropMenu .= "</select>";
echo $dropMenu;
}
使用GET将这4个字段中的每个字段的值放入PHP文件中。
$country1 = $_GET['country1'];
$country2 = $_GET['country2'];
$country3 = $_GET['country3'];
$country4 = $_GET['country4'];
选择这些字段后,将执行搜索所选国家/地区的SQL语句
$sql="SELECT Country.*, COUNT(Cyclist.name) as noOfAthlete
FROM Country
LEFT JOIN Cyclist ON Cyclist.ISO_id = Country.ISO_id
WHERE Country.ISO_id = '$country1'
UNION
SELECT Country.*, COUNT(Cyclist.name) as noOfAthlete
FROM Country
LEFT JOIN Cyclist ON Cyclist.ISO_id = Country.ISO_id
WHERE Country.ISO_id = '$country2'
UNION
SELECT Country.*, COUNT(Cyclist.name) as noOfAthlete
FROM Country
LEFT JOIN Cyclist ON Cyclist.ISO_id = Country.ISO_id
WHERE Country.ISO_id = '$country3'
UNION
SELECT Country.*, COUNT(Cyclist.name) as noOfAthlete
FROM Country
LEFT JOIN Cyclist ON Cyclist.ISO_id = Country.ISO_id
WHERE Country.ISO_id = '$country4'
";
前两个字段是必填字段,后两个字段是可选字段。
如果用户为国家1和2选择空白选项,则会显示错误消息。如果他输入1和2的有效选项但为国家3或4输入空白选项,则只应将未留空的选择框添加到数组中(即,如果用户选择国家1,2和4,不应将国家3添加到数组中)。
问题是目前所有国家/地区都已添加,无论是否已选中或留空,都会创建包含空字段的记录。
这就是我的问题所在。只有当记录的字段不为空时,我才需要填充包含信息的数组。
我尝试过以下操作,但它仍然在数组中加载空记录。
while($row = mysql_fetch_assoc($result)){ //going through each row in the db
if(is_null($row) == 0){ //i have tried empty as well
array_push($rowsArr, $row)
}
else
echo "I wont fill the array with this record";
}
这是打印时典型阵列的样子。我需要第三条记录不要加载到我的数组中,因为所有字段都是null,这会在将数据加载到HTML表时创建一个额外的不需要的表行。
JSON ARRAY
[{"ISO_id":"BEL","gdp":"511533000000","population":"10896000","country_name":"Belgium","gold":"0","silver":"1","bronze":"2","total":"3","noOfAthlete":"14"},
{"ISO_id":"ARM","gdp":"10247788877","population":"3092000","country_name":"Armenia","gold":"0","silver":"1","bronze":"2","total":"3","noOfAthlete":"0"},
{"ISO_id":null,"gdp":null,"population":null,"country_name":null,"gold":null,"silver":null,"bronze":null,"total":null,"noOfAthlete":"0"}].
PHP ARRAY
Array ( [0] => Array ( [ISO_id] => BEL [gdp] => 511533000000 [population] => 10896000 [country_name] => Belgium [gold] => 0 [silver] => 1 [bronze] => 2 [total] => 3 [noOfAthlete] => 14 ) [1] => Array ( [ISO_id] => ARM [gdp] => 10247788877 [population] => 3092000 [country_name] => Armenia [gold] => 0 [silver] => 1 [bronze] => 2 [total] => 3 [noOfAthlete] => 0 ) [2] => Array ( [ISO_id] => [gdp] => [population] => [country_name] => [gold] => [silver] => [bronze] => [total] => [noOfAthlete] => 0 ) )
希望我清楚自己。任何帮助将不胜感激!
答案 0 :(得分:1)
您可以使用带有in子句和group by
的查询检查$ _GET(假设你有country1和country2不为null)你可以构建一个dinamic var string
$varStr = " ( 'country1' , 'country3') ";
"SELECT Country.ISO_id, Country.Name, COUNT(Cyclist.name) as noOfAthlete
FROM Country
LEFT JOIN Cyclist ON Cyclist.ISO_id = Country.ISO_id
WHERE Country.ISO_id in ". $varStr .
"group by Country.ISO_id, Country.Name";