0

私は学校のプロジェクトで、新聞から RSS フィードを取得し、それらを組積造レイアウトでスタイル化することに取り組んでいます。Simplepieクラスを使用して html に変換する RSS フィードを取得できますが、各記事にカウンター付きの 1 から 5 までの列サイズを指定したいと考えています。

これは私がhtmlで得るものです:

<div class="post col1 col2 col3 col4 col5 col1 col2 col3 col4 col5"> <!-- begin post -->
<h3 class="title"><a href="http://feedproxy.google.com/~r/dso-nieuws-sport/~3/zfFYeKYGagk/detail.aspx">Bergen naar halve finales play-offs basket</a></h3>

代わりに、最初の記事にクラス「post col1」、2 番目の記事に「post col2」、5 つの記事の後、6 番目の記事に再び「col1」などを取得する必要があります。

これは私のPHPコードです:

<?php if ($sportfeed->data): ?>
 <?php $sportitems = $sportfeed->get_items(); ?>

      <?php foreach($sportitems as $sportitem): ?>

            <?php $enclosure = $sportitem->get_enclosure(0); ?>

                  <?php if ($enclosure):?> 

                    <div class="post

                    <?php  $teller = 1;
                           for ($i = 1; $i <= 10 /* aantal artikels in feed */; $i++) {
                           if ($teller == 1) {

                             echo " col1";
                             ++$teller;

                              } else if ($teller ==2) 
                              {
                                echo " col2"; 
                                ++$teller;
                               } else if ($teller ==3) 
                               {
                                 echo " col3"; 
                                 ++$teller; 
                                 } else if ($teller ==4) 
                                 {
                                     echo " col4"; 
                                     ++$teller;
                                  } else 
                                  { echo " col5"; 
                                  $teller =1; 
                                  }

                            }?>"> <!-- begin post -->
                    <h3 class="title"><a href="<?php echo $sportitem->get_permalink(); ?>"><?php echo $sportitem->get_title(); ?></a></h3>
                    <img src ="<?php echo $enclosure->get_link(); ?> "class="img_artikel"/>
                    </div> <!-- einde post -->  

                  <?php endif; ?> 

       <?php endforeach; ?>

よろしくお願いします!私のプロジェクトを進行させるには、大きな意味があります。

4

5 に答える 5

0
$teller = 1;
for ($i = 1; $i <= 10 ; $i++) {
   if($i%6==0)
    {
       $teller=1;
       echo " col1";
    }else{
      echo " col".$teller;
    }
    $teller++;
}
于 2012-05-17T15:21:45.377 に答える
0

私はあなたがほぼそこにいると思いますが、問題は、出納係= 1の場合、それをインクリメントして2番目のブロックを実行することであると思います.2番目のブロックを実行するなど、ループ内にすべて入れて最後に1回インクリメントしてみてください...

私はこのようなものがうまくいくと思います:

<?php  $teller = 1;                            
 for ($i = 1; $i <= 10; $i++)
{                           
   if ($teller == 1) 
   {                               
      echo " col1";
   } 
   else if ($teller ==2)
   { 
      echo " col2";                                
    } 
    else if ($teller ==3)                                
    {                                 
    echo " col3";        
    } 
     else if ($teller ==4)                                   
    {                                      
      echo " col4"; 
               } 
   else                                    
   {
     echo " col5"; 
      $teller =1;
   } 

echo " '>";              
echo"<h3 class='title'> <a href='#'></a></h3>";                   
echo"<img src ='#' class='img_artikel'/>";                 
echo"</div>"; 
$teller++;
}

?>
于 2012-05-17T15:44:30.383 に答える
0

これらは 1 組しか必要としないのに、たくさんの<?phpとタグを使用します。また、私にはより論理的に思えますが、を 使用します。私はあなたのコードを再フォーマットしました:?>
endifendforeach}

if ($sportfeed->data)
{
    $sportitems = $sportfeed->get_items(); 
    $teller = 1;
    foreach($sportitems as $sportitem)
    {
        $enclosure = $sportitem->get_enclosure(0); 
        if ($enclosure)
        {
            echo '<div class="post col' . $teller;
            $teller = $teller == 5 ? 1 : $teller + 1;

            echo '"> <!-- begin post --><h3 class="title"><a href="' . $sportitem-    >get_permalink(); . '">' . $sportitem->get_title(); . '</a></h3>';
            echo '<img src="' . $enclosure->get_link(); . '"class="img_artikel"/></div>    <!-- einde post -->';
        }
    }
}

forあなたが達成したいことを理解していれば、ループ全体は必要ありませんでした。上記のコードは、次のような出力を出力するはずです。

<div class="post col1"> .... </div>
<div class="post col2"> .... </div>
<div class="post col3"> .... </div>
<div class="post col4"> .... </div>
<div class="post col5"> .... </div>
<div class="post col1"> .... </div>
<div class="post col2"> .... </div>

それが役に立てば幸いです: )(ところで、あなたもオランダ人だと思います、笑)

于 2012-05-17T15:24:02.130 に答える
0

カウンターを作成する代わりに係数を使用してみてください。$teller = $i % 5剰余を返すので、0 から 4 までの数値が得られます。

for ($i = 1; $i <= 10 /* aantal artikels in feed */; $i++) {
    $teller = $i % 5;

    if ($teller == 0) {
        echo " col5";
    } else {
        echo " col" . $teller;
    }
}

動作するはずです。

于 2012-05-17T15:17:43.107 に答える
0
<?php
if ($sportfeed->data):
    $sportitems = $sportfeed->get_items();
    $i = 1;
    foreach($sportitems as $sportitem):
        $enclosure = $sportitem->get_enclosure(0);
        if ($enclosure):
            echo '<div class="post';
            if ($i % 5 == 0) {
                echo " col5";
            } else {
                echo " col" . ($i % 5);
            }
            echo '"> <!-- begin post -->';
 ?>
     <h3 class="title"><a href="<?php echo $sportitem->get_permalink(); ?>"><?php echo $sportitem->get_title(); ?></a></h3>
     <img src ="<?php echo $enclosure->get_link(); ?> "class="img_artikel"/>
 </div> <!-- einde post -->  
        <?php endif; ?>
        <?php $i++; ?>
    <?php endforeach; ?>
<?php endif; ?>
于 2012-05-17T15:25:16.007 に答える