2011-07-25 8 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

を示唆? – hakre

+0

GBは、2つの他の文字列の間の文字列を取得する関数です。 GBAは他の2つの文字列の間の文字列を取得するファンクションですが、文字列全体を検索して配列を返します。 GetbetweenAll – Qasim

答えて

0

内側ループの内側に$ sizeを再割り当てします。 2番目のSMSのチャットグループには2つのエントリしかありません。これにより、$ size = 2と$ i = 2のために外部ループが終了します。Jon Martinの提案は、特定のデータに対しては機能するようですが、

私はGBとGBAは何

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

よく盗まれました.... –

1

外側ループに$size、内側ループに$sizeを使用しました。

PHPにはブロックスコープがありませんので、と同じ変数です。内側のループで変化するので、あなたは外側のループを台無しにしています。

内部ループの変数に別の名前を指定します。

+0

または単に 'foreach'を使用してください。 – hakre

+0

そうです、それも.. –

関連する問題