0

我的 foreach 中有以下内容....这可行,但每个 $value 有 2 组值,它只显示第一个

<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?"); 
echo "<ul info>";

foreach ($info->sessions as $sessions): 
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';

            ?>


<?php
                    echo '<tr>',

              '<td class="blockcontentwhite sessionicon">',
              '<td class="blockcontentwhite center">' . ("$ip") . '</td>',
              '<td class="blockcontentwhite center">' . ("$nas") . '</td>',
              '<td class="blockcontentwhite center">' . ("$port") . '</td>',
              '<td class="blockcontentwhite center">' . ("$phone") . '</td>',

                    '</tr>';
                    endforeach;
                    ?>

所以基本上在第一行的表格中,它显示了 $ip、$nas、$port 和 $phone 但它没有在第二行显示第二个值,有什么想法吗?

多谢你们

4

2 回答 2

2

您正在尝试将对象作为数组访问。$info->sessionsSimpleXMLElement Object包含(作为属性)要在 foreach 中使用的数组的类型。

将您的 foreach ( foreach ($info->sessions as $sessions):) 更改为foreach ($info->sessions->session as $sessions):和:

$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';

到:

$count = $info->sessions->{'session-count'};
$ip = $sessions->{'ip-address'};
$nas = $sessions->{'nas-ip-address'};
$port = $sessions->{'nas-port'};
$phone = 'N/A';
于 2013-03-23T08:40:16.820 回答
0

我认为 Jueecy 关于您提出的问题是正确的。

但我注意到除此之外看起来可能是个问题。看起来代码会生成多个表,但我认为您只需要同一个表中的多行。

我建议将您的 foreach 移至生成行的位置:

<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?"); 
echo "<ul info>";
?>

<input type="hidden" name="ctl00$ctl00$contentDefault$contentControlPanel$hdnIsSecure"     id="ctl00_ctl00_contentDefault_contentControlPanel_hdnIsSecure" value="false" />
<table id="active_sessions_table" class="blocktable centered" cellpadding="0"   cellspacing="0">
<tr class="blockheader">
<td id="active_sessions_title" class="left" colspan="2">
      <label  class="floatleft">Current connections</label></td>
</tr>
<tr id="trconnections">
<td class="blockcellnopadding" colspan="2">
<table cellpadding="4" cellspacing="0" width="100%"  style="border-bottom:solid 1px  #bfbfbf;">
        <tr>
            <td class="columntitle center">&nbsp;</td>
            <td class="columntitle center">IP Address</td>
            <td class="columntitle center">NAS IP Address</td>
            <td class="columntitle center">Line Port</td>
            <td class="columntitle center">Telephone Number</td>
        </tr>
<?php
        foreach ($info->sessions->session as $session) {
            $ip = $session->{'ip-address'};
            $nas = $session->{'nas-ip-address'};
            $port = $session->{'nas-port'};
            $phone = 'N/A';

            echo '<tr>',
          '<td class="blockcontentwhite sessionicon">',
          '<td class="blockcontentwhite center">' . ("$ip") . '</td>',
          '<td class="blockcontentwhite center">' . ("$nas") . '</td>',
          '<td class="blockcontentwhite center">' . ("$port") . '</td>',
          '<td class="blockcontentwhite center">' . ("$phone") . '</td>',

          '</tr>';
       };
?>
于 2013-03-23T09:14:12.270 回答