2011-09-25 73 views
1

我想知道是否有人可以幫助我請。如果在PHP腳本語句

我正在使用以下腳本將標記數據加載到我的地圖中。

<?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'聲明來做到這一點。我對此有點新,但也許有人可能會確認這是否是最好的方式。

非常感謝

回答

1

是的,你可以使用if,IE的

if($row['totalfinds'] == 0) $newnode->setAttribute("totalfinds", "No items found"); 
else $newnode->setAttribute("totalfinds","Items Found"); 

代替

$newnode->setAttribute("totalfinds",$row['totalfinds']); 
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

你可以做一個if,但短期問號標記將適合

$found_string = ($totalfinds != 0 ? 'Items Found' : 'No items found'); 
的更短

(當變量不爲零,等於true:在這種情況較好):

$found_string = ($totalfinds ? 'Items Found' : 'No items found'); 
+0

全部,非常感謝您的幫助。正是我在找什麼。親切的問候。 – IRHM

+0

歡迎:) ----- – MeLight

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";