我有这种 xml 格式:
<searches>
<search>
<name> stephen </name>
<address> box 23 </address>
<sector> IT </sector>
<contacts>+233247764939 </contacts>
<search>
<name> stephen </name>
<address> box 23 </address>
<sector> IT </sector>
<contacts>+233247764939 </contacts>
<searches>
当未找到名称节点的值时,我想向用户显示错误。我尝试使用下面的代码,但似乎没有执行 echo 语句,因此当名称节点值为空或不包含值时,空屏幕显示没有回显消息。这段代码有什么帮助吗?注意:如果名称没有价值,则表示该公司不存在。此代码将 $objj 的值返回到下一个 php 脚本。
下面是完整的代码:
<?php
function processing($term){
$params = array ('username' => 'username', 'password' => 'password', 'term'=> $term);
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
print_r($context);
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
$obj = @simplexml_load_string($result);
return $obj
}
?>
//the outputting is done in this script
<?php $objj = processing ($term); ?>
<div data-role="listview">
<ul data-role="listview" data-theme="g" data-inset="true" class="ui-listview ui- listview-inset ui-corner-all ui-shadow">
<a href="" data-role="button" data-theme="g" data-inset="true"><strong>Search Results</strong></a>
<?php if ($objj){ ?>
<?php foreach(@$objj as $searches):?>
<li> <a href="Result_Selected.php?company=<?php echo $searches -> name.'<br/>'.$searches->sector.'<br/>'.$searches ->address.'<br/>'.$searches -> contacts.'<br/>'; ?>" data-role="button"><?php echo( $searches -> name).'<br/>'.$searches - > sector ?></a></li>
<?php endforeach ?>
<a data-ajax="false" href="#top" data-role="button" name= "smt"><strong>End Of List... </strong></a>
<?php } else if(empty($objj)||($objj==null)||($objj->name=="")){ ?>
<?php echo "hello company not found.try again"; } ?>
</ul>