2011-09-25 5 views
1

나는 누군가가 나를 도울 수 있는지 궁금해.If Statement in PHP Script

마커 데이터를 내지도에로드하는 데 다음 스크립트를 사용하고 있습니다.

<?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'수치가 0이면 'No items found'라고 쓰여진 필드를 원하고 값이 0보다 크면 'Items Found'텍스트를 반환합니다.

내가 읽은 것으로부터, 나는 'If'진술을 통해 이것을 할 필요가 있다고 생각한다. 나는 이것에 조금 새로운지만, 누군가가 아마도 나를 위해 그것을 확인하는 가장 좋은 방법인지 확인해 줄 수 있습니다.

많은 감사

답변

1

예, 즉

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를 사용할 수 있습니다.

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

당신은 if을 할 수

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

과 동일하지만, 짧은 물음표 표시는 맞는 것입니다 : 그것은 것 인 IF-Else 문을 같은 일을하고 간결한 방법입니다 심지어 짧은의

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

(변수가 true에 동일 0이이 시나리오에서 더 나은) : 다른 사람들이 지적이 더 간결하게 ternary operator과 함께 할 수 있기

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

모두 감사합니다. 정확히 내가 무엇을 찾고 있었는지. 친절합니다. – IRHM

+0

환영합니다 :) ----- – MeLight

1

의, if 사용할 수 있습니다. 그러나 이것이 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'); 

이도 수행 할 수있다 case statement을 사용하도록 MySQL 쿼리를 변경합니다.

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