[英]foreach td loop values
I have the following within my foreach....this works but there are 2 sets of values per $value and it only shows the first 我在我的foreach中有以下内容....这有效但每$ value有两组值,它只显示第一组
<?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;
?>
so basically within a table on 1st row its showing the $ip , $nas, $port and $phone BUT it is not showing the second values on the 2nd row , any ideas? 所以基本上在第一行的一个表中它显示了$ ip,$ nas,$ port和$ phone但它没有显示第二行的第二个值,任何想法?
Thanks Guys 多谢你们
You are trying to access an object as an array. 您正尝试将对象作为数组访问。
$info->sessions
is of type SimpleXMLElement Object
which contains (as a property) the array you want to use in the foreach. $info->sessions
的类型为SimpleXMLElement Object
,它包含 (作为属性)要在foreach中使用的数组。
Change your foreach ( foreach ($info->sessions as $sessions):
) to foreach ($info->sessions->session as $sessions):
and: 将foreach(
foreach ($info->sessions as $sessions):
)改为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';
to: 至:
$count = $info->sessions->{'session-count'};
$ip = $sessions->{'ip-address'};
$nas = $sessions->{'nas-ip-address'};
$port = $sessions->{'nas-port'};
$phone = 'N/A';
I think Jueecy is correct regarding the question you asked about. 我认为对于你提出的问题,Jueecy是正确的。
But I noticed something that looks like it might be a problem besides that. 但我注意到除此之外看起来可能是一个问题。 It looks like the code will generate multiple tables, but I think you just want multiple rows in the same table.
看起来代码会生成多个表,但我想你只想在同一个表中有多行。
I would suggest moving your foreach down to where you are generating the rows: 我建议将你的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"> </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>';
};
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.