开发者

If Statement In PHP Script

I wonder whether someone can help me please.

I'm using the following script to load marker data into my map.

<?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']); 
$newno开发者_开发技巧de->setAttribute("osgb36lat",$row['osgb36lat']); 
$newnode->setAttribute("osgb36lon",$row['osgb36lon']); 
$newnode->setAttribute("totalfinds",$row['totalfinds']);
} 

echo $dom->saveXML(); 

?>

I'd now like to extend the functionality of it further, by adding a new field that converts a numeric value to text. To be more precise if the 'totalfinds' figure is zero then I would like a field that says 'No items found' and if the value is greater than zero then this would return the text 'Items Found'.

From what I've read, I think that I'll need to do this via an 'If' Statement. I am a little new to this, but could someone perhaps confirm for me please whether this is the best way to go about it.

Many thanks


You can use the Ternary Operator in PHP. It is a concise way of doing the same thing an If-Else statement would:

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

is the same as

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


Yes, you can use if, ie

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

instead of

$newnode->setAttribute("totalfinds",$row['totalfinds']);


You could do an if, but short question mark notation would fit better in this scenario:

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

of even shorter (when variable is not zero it equals to true):

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


As others have pointed out, this could be done more concisely with the ternary operator. But considering that you asked if this could be done with an if statement, and you are relatively new to php, here is how this might be accomplished using an if statement.

Change this line:

$newnode->setAttribute("totalfinds",$row['totalfinds']);

to

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

In case you're still curious, the ternary operator solution looks like this:

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

This could also be accomplished by changing the MySQL query to use a 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"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜