0

I'm working on a school project to get rss feeds from newspapers and get them styled in a Masonry lay-out. I can get the rss feed to convert in html with the Simplepie class but I want to give each article a column size from 1 to 5 with a counter.

This is what I get in 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>

Instead I want the first article to have class "post col1", the second article "post col2" and after five articles the sixth should get "col1" again and so on..

This is my PHP code:

<?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; ?>

Thanks a lot in advance! It would mean a lot, to get my project going.

Robin
  • 47
  • 3
  • 5
  • What's the exact problem ? By the way, i've never seen something like `++$teller` before, are you sure it's correct ? PHP standard is `$teller++` – Sliq May 17 '12 at 15:06
  • 1
    `++$teller` is valid as well. The difference is in the value it returns. If `$teller` starts at one, I believe `echo $teller++;` prints 1 while `echo ++$teller;` prints 2. – soimon May 17 '12 at 15:08
  • There is no difference when `++$teller` or `$teller++` is used on a line on it's own. Only matters when it's part of a larger statement such as echo, or if statements, etc. It's all a matter of when it increments the number – Recognizer May 17 '12 at 15:22

5 Answers5

0

Try using modulus instead of creating a counter; $teller = $i % 5 will return the remainder, so it'll give you a number from 0 to 4.

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

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

Should work.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
0
$teller = 1;
for ($i = 1; $i <= 10 ; $i++) {
   if($i%6==0)
    {
       $teller=1;
       echo " col1";
    }else{
      echo " col".$teller;
    }
    $teller++;
}
M Rostami
  • 4,035
  • 1
  • 35
  • 39
0

You use a lot of <?php and ?> tags while you only need one pair of these.
You also use endif, endforeach while } seems more logical to me. I reformatted your code:

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 -->';
        }
    }
}

The entire for loop wasn't necessary, if I do understand what you want to achieve. The above code should print an output like:

<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>

Hope it helped :) (I suppose you're Dutch as well btw, haha)

soimon
  • 2,400
  • 1
  • 15
  • 16
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; ?>
shadyyx
  • 15,825
  • 6
  • 60
  • 95
0

I think you are nearly there but I think the problem is if teller = 1 then you incrment it so it equals 2 executing the second block and so on, just try putting it all inside the loop and incrementing once at the end...

I think something like this will work:

<?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++;
}

?>