为什么它只显示3个阵列中的2个?

时间:2011-07-25 23:58:21

标签: php xml arrays for-loop

我不确定为什么会这样。这是我的XML:

<ResultSet>
<chats>
    <chat>
        <messages>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612055</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612068</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612074</sent>
            </sms>
            <sms>
                <from>Bisma Iqbal</from>
                <msg>Hi</msg>
                <sent>1311612186</sent>
            </sms>
        </messages>
        <contact>Bisma Iqbal</contact>
    </chat>
    <chat>
        <messages>
            <sms>
                <from>Javaid Iqbal</from>
                <msg>this is the first message</msg>
                <sent>1311612055</sent>
            </sms>
            <sms>
                <from>Javaid Iqbal</from>
                <msg>this is the second message</msg>
                <sent>1311612055</sent>
            </sms>
        </messages>
        <contact>Javaid Iqbal</contact>
    </chat>
    <chat>
        <messages>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
            <sms>
                <from>Ankur Shahi</from>
                <msg>Wanna play dawg at 7:15</msg>
                <sent>1311632708</sent>
            </sms>
        </messages>
        <contact>Ankur Shahi</contact>
    </chat>
</chats>
</ResultSet>

现在,这是我的代码:

require_once('global.php');
$statement = $db->prepare("SELECT * FROM user WHERE id=1");
$statement->execute();
$result = $statement->fetchObject();
$string = $result->chats;
$chats = GBA($string, "<chat>", "</chat>"); //the XML I showed you

for($i = 0, $size = count($chats); $i < $size; ++$i) {
//for every chat:
    $all_sms = GBA($chats[$i], "<sms>", "</sms>");
    $name = GB($chats[$i], "<contact>", "</contact>");
    echo "<center><table border='1' cellpadding='5'><tr><td colspan='3' align='center'>SMS Chat with <i>{$name}</i></td></tr><tr><th>From</th><th>Message</th><th>Sent</th></tr>";

    for($j = 0, $size = count($all_sms); $j < $size; ++$j) {
    //for every sms in each chat
            $from = GB($all_sms[$j], "<from>", "</from>");
            $msg = GB($all_sms[$j], "<msg>", "</msg>");
            $sent = time_since(GB($all_sms[$j], "<sent>", "</sent>"));
            echo "<tr><td>{$from}</td><td>{$msg}</td><td>Sent: {$sent}   </td></tr>";
        }
    echo "</table><br /><br /></center>";
}

正如你所看到的,一切看起来都很好,但它只显示前两个表,而不是第三个!我已经调试了很多但没有结论。

2 个答案:

答案 0 :(得分:1)

您在外部循环中使用了$size,在内部循环中使用了$size

PHP没有块范围,因此这是相同的变量。当它在内循环中发生变化时,你会搞乱外循环。

为内循环中的变量选择一个不同的名称。

答案 1 :(得分:0)

在内部循环中重新分配$ size。第二个短信聊天组只有2个条目导致外循环终止,因为$ size = 2和$ i = 2. Jon Martin的建议似乎适用于您的特定数据但在一般情况下会失败。

我建议

for($j = 0, $jsize = count($all_sms); $j < $jsize; ++$j) {
// ...