2009-07-28 3 views
0

私はこのPHPコードを持っていますが、その下のhtmlレイアウトと統合しようとしていますが、私はそれを理解できません。 コードはすべてのステータスポストを取得し、それらを順番に表示します。ステータス投稿には コメントがあるか、複数のコメントがある可能性があります。このPHPループ内でこのhtmlをどのように統合できますか?

あなたは、私はPHPコードで左のノート、私はHTMLで左のノートを読めば、あなたは私が何とか状況ポストのテーブルセル

の内側にコメントを配置する必要があり を直面しています、問題が表示されます

下の画像には1つのステータスポストとそのポストに対する3つのコメントがありますが、私がしようとしているのは、ステータスポストの右側にネストされる自分のテーブルにコメントを表示することです。だから、あなたがその記事の下部にあるテーブルの枠線を参照してくださいトップ@ステータスポストに、その境界線はそのポストのための最後のコメント以下でなければならない、もう少し理にかなっての希望今

alt text http://i32.tinypic.com/2afnc3l.jpg

<?PHP 
$last_id = 0; 
echo '<table width="400">'; 
while ($row = mysql_fetch_array($res)) { 
    //start output of new status post and comments 
    if ($row['0'] != $last_id) { 
     echo 'status post stuff' 
    } 
    //start output of new status post and comments 

    //output comment here 
    $last_id = $row['0']; 
    if($row['commentid'] != ''){ 
     echo 'status COMMENT for above status post' 
    } 
    //END output comment here 
} 
echo '</table>'; 
?> 

<table width="400"> 

    <!-- begin status post --> 
    <tr> 
     <td width="99" valign="top" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;"> <div class="imageSub" style="width: 90px;"> <img class="female" src="http://cache2.mycrib.net/images/image_group66/0/43/t_6871399b0962b5fb4e29ce477541e165950078.jpg" alt="Something" width="90"/> </div></td> 
     <td width="489" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">so and so said blah blah blah @ wee hours of the moring! <BR> 

      <!-- begin comment --> 
      <table width="90%" style="border: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;"> 
       <tr> 
        <td width="14%" rowspan="2" valign="top"><img class="male" src="http://cache2.mycrib.net/images/image_group34/0/39/T_653807517aff2b1f5662d865b40d87d527c8eb.jpg" alt="Something" width="45"/></td> 
        <td width="86%">Date Posted</td> 
       </tr> 
       <tr> 
        <td>Comment text</td> 
       </tr> 
      </table> 
      <!-- end comment --> 

     </td> 
    </tr> 
    <!-- end status post --> 

</table> 
+2

私はあなたに正直になるだろう、私はこの問題は、あなたが直面していることが何であるかを理解していません。詳しく教える? :) – falstro

+0

画像を追加しましたもう少し説明しました – JasonDavis

答えて

2

phpタグを閉じて、再度htmlにしてループ内で開くことができます。

例えば:

<table width="400"> 

<?PHP 
$last_id = 0; 
echo '<table width="400">'; 
while ($row = mysql_fetch_array($res)) { 
    //start output of new status post and comments 

    if ($row['0'] != $last_id) { 
     //echo 'status post stuff' 
    ?> 
<tr> 
      <td width="99" valign="top" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;"> <div class="imageSub" style="width: 90px;"> <img class="female" src="http://cache2.mycrib.net/images/image_group66/0/43/t_6871399b0962b5fb4e29ce477541e165950078.jpg" alt="Something" width="90"/> </div></td> 
      <td width="489" style="border-bottom: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">so and so said blah blah blah @ wee hours of the moring! <BR> 
    <?php 
    } 
    //start output of new status post and comments 

    //output comment here 
    $last_id = $row['0']; 
    if($row['commentid'] != ''){ 
     echo 'status COMMENT for above status post' 
    } 
    //END output comment here 
    ?> 

       <!-- begin comment --> 
      <table width="90%" style="border: 1px solid rgb(204, 204, 204); margin: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;"> 
       <tr> 
        <td width="14%" rowspan="2" valign="top"><img class="male" src="http://cache2.mycrib.net/images/image_group34/0/39/T_653807517aff2b1f5662d865b40d87d527c8eb.jpg" alt="Something" width="45"/></td> 
        <td width="86%">Date Posted</td> 
       </tr> 
       <tr> 
        <td>Comment text</td> 
       </tr> 
      </table> 
      <!-- end comment --> 

     <?php 
} 
echo '</table>'; 
?> 
+0

私は非常に驚いていますが、君は – JasonDavis

-1

SMARTYのようなテンプレートシステムの学習を始めることをお勧めします。多くの問題を解決しやすく、コードを読みやすく理解しやすくなります。

+3

smartyが問題を複雑にする、PHP自体が正しく使用されるとテンプレートシステムです – dusoft

+0

はdusoftに同意する必要があります – slipbull

+0

私は以前にsmartyを使用しましたが、私はtemplets IMOのファンではありませんより多くのコードを解析する – JasonDavis

4

私はこのようなものを試してみると、データベースから要素をフェッチしているロジックからデザインを分離することができます。

1.Build以下のようなものです、あなたのデータベースから配列 - これは簡単でなければなりません:配列と出力HTML(私が簡略化されていますが、あなたは適応することができるはずです)を介し

<?php 
$posts = array(
    array( 
     'title' => 'Hello', 
     'post' => 'This is the post', 
     'comments' => array( 
      array(
       'date_posted' => '28/07/2009', 
       'text' => 'this is the first comment' 
      ), 
      array(
       'date_posted' => '28/07/2009', 
       'text' => 'this is the second comment' 
      ) 
     ) 
    ), 
    array(
     'title' => 'Another post', 
     'post' => 'Hello', 
     'comments' => array() 
    ) 
); 
?> 

2.Loop 。

<?php foreach ($posts as $post): ?> 
    <!-- begin status post --> 
    <h1><?php echo $post['title']; ?></h1> 
    <p><?php echo $post['post']; ?></p> 
    <?php if ($post['comments']): ?> 
     <h2>Comments:</h2> 
     <ul> 
     <?php foreach ($post['comments'] as $comment): ?> 
      <!-- begin comment --> 
      <li> 
       <?php echo $comment['text']; ?> 
       etc. 
      </li> 
     <!-- end comment --> 
     <?php endforeach; ?> 
     </ul> 
    <?php endif; ?> 
<!-- end status post --> 
<?php endforeach; ?> 
+0

私はこれが好きです。今は配列を学んでいるので、このようなことをいつもやりたいと思っています。うまくいけば、このようなことができます。 – JasonDavis

関連する問題