我想知道是否有人可以帮助我。
我正在使用以下脚本将标记数据加载到我的地图中。
<?php
require("phpfile.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ("hostname", $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$query = "select l.locationid, f.locationid, l.locationname, l.address, l.osgb36lat, l.osgb36lon, count(f.locationid) as totalfinds from detectinglocations as l left join finds as f on l.locationid=f.locationid group by l.locationid";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("locationid",$row['locationid']);
$newnode->setAttribute("locationname",$row['locationname']);
$newnode->setAttribute("address",$row['address']);
$newnode->setAttribute("osgb36lat",$row['osgb36lat']);
$newnode->setAttribute("osgb36lon",$row['osgb36lon']);
$newnode->setAttribute("totalfinds",$row['totalfinds']);
}
echo $dom->saveXML();
?>
我现在想通过添加一个将数值转换为文本的新字段来进一步扩展它的功能。更确切地说,如果'totalfinds'数字为零,那么我想要一个显示“找不到项目”的字段,如果该值大于零,那么这将返回“找到的项目”文本。
根据我的阅读,我认为我需要通过'If'声明来做到这一点。我对此有点新意,但也许有人可能会向我确认这是否是最佳方式。
非常感谢
答案 0 :(得分:2)
您可以在PHP中使用Ternary Operator。这是一个简单的方法来做同样的事情,If-Else语句:
$newnode->setAttribute("totalfinds", ($row['totalfinds'] > 0 ? 'Items Found' : 'No items found'));
与
相同if ($row['totalfinds'] > 0) {
$newnode->setAttribute("totalfinds", 'Items Found');
} else {
$newnode->setAttribute("totalfinds", 'No items found');
}
答案 1 :(得分:1)
您可以执行if
,但在这种情况下,简短的问号表示法会更合适:
$found_string = ($totalfinds != 0 ? 'Items Found' : 'No items found');
甚至更短(当变量不为零时,它等于true
):
$found_string = ($totalfinds ? 'Items Found' : 'No items found');
答案 2 :(得分:1)
是的,您可以使用if
,即
if($row['totalfinds'] == 0) $newnode->setAttribute("totalfinds", "No items found");
else $newnode->setAttribute("totalfinds","Items Found");
而不是
$newnode->setAttribute("totalfinds",$row['totalfinds']);
答案 3 :(得分:1)
正如其他人所指出的,这可以用ternary operator更简洁地完成。但考虑到你问这是否可以使用if
语句完成,并且你对php相对较新,这里可以使用if
语句来实现这一点。
更改此行:
$newnode->setAttribute("totalfinds",$row['totalfinds']);
到
if ($row['totalfinds'] > 0) {
$foundText = 'Items Found';
} else {
$foundText = 'No Items Found';
}
$newnode->setAttribute("totalfinds", foundText);
如果您仍然好奇,ternary operator解决方案如下所示:
$newnode->setAttribute("totalfinds", $row['totalfinds'] > 0 ? 'Items Found' : 'No Items Found');
这也可以通过将MySQL查询更改为使用case statement来实现。
$query = "select l.locationid, f.locationid, l.locationname, l.address, l.osgb36lat, l.osgb36lon, case when count(f.locationid) > 0 then 'Items Found' else 'No Items Found' end as totalfinds from detectinglocations as l left join finds as f on l.locationid=f.locationid group by l.locationid";