2011-07-25 73 views
0

我不知道为什么会发生这种情况。下面是我的XML:为什么它只显示3个数组中的2个?

<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

什么是GB和GBA? – hakre

+0

GB是一个函数,它获取两个其他字符串之间的字符串。 GBA是一个函数,它可以获取两个其他字符串之间的字符串,但会搜索整个字符串并返回一个数组。 Getbetween和GetbetweenAll – Qasim

回答

0

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

我建议

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

好偷.... –

1

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

PHP没有块范围,所以这是相同的变量。随着它在内部循环中的变化,你正在搞乱你的外部循环。

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

+0

或只使用'foreach'。 – hakre

+0

是的,那也太.. –

相关问题