php - Output three columns and row in loop

75

I am trying to output a loop with three columns surrounded by a row, like this:

<div >
  <div #">Title</a></div>
  <div #">Title</a></div>
  <div #">Title</a></div>
</div>
<div >
  <div #">Title</a></div>
  <div #">Title</a></div>
  <div #">Title</a></div>
</div>
<div >
  <div #">Title</a></div>
  <div #">Title</a></div>
  <div #">Title</a></div>
</div>

Here's what I've come up with. This works for the first and second row, but the third row ends up inside the second row. Any ideas what I'm doing wrong?

<?php 

$i = 0;

while ($myposts->have_posts()) : $myposts->the_post(); ?>

<?php if( $count%3 == 0 ) { echo '<div >'; }; $count++; ?>

<div ><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

<?php if( $count%3 == 3 ) { echo '</div>'; }; ?>

<?php endwhile; ?>
526

Answer

Solution:

What about this: You can play around a bit with first mod number and last one. And make sure you always get 3 titles from $myposts->post()

<?php 

$count = 0;
while ($myposts->have_posts()) : $myposts->the_post(); ?>
<?php if( $count%2 == 0 || $count == 0 ) { echo '<div >'; }; ?>
<div ><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<?php if( $count%2 == 3 ) { echo '</div>'; };
$count++; ?>
<?php endwhile; ?>
812

Answer

Solution:

If I got you, you wanna a div class row every three records or something like that. How I don't know the format of input data I put these example as reference:

$data = array(
  array('title' => "TITLE1", 'link' => "LINK1"),
  array('title' => "TITLE2", 'link' => "LINK2"),
  array('title' => "TITLE3", 'link' => "LINK3"),
  array('title' => "TITLE4", 'link' => "LINK4"),
  array('title' => "TITLE5", 'link' => "LINK5"),
  array('title' => "TITLE6", 'link' => "LINK6"),
  array('title' => "TITLE7", 'link' => "LINK7"),
  array('title' => "TITLE8", 'link' => "LINK8"),
  array('title' => "TITLE9", 'link' => "LINK9"),
  array('title' => "TITLE10", 'link' => "LINK10"),
); 

$rowCounter = 1;
$dataCounter = 0;
$newRow = true;

while($dataCounter < count($data))
{
  if($newRow == true)
  {
    echo '<div >'."\n";
    $newRow = false;
  }

  echo "\t".'<div ><a href="'.$data[$dataCounter]['link'].'">'.$data[$dataCounter]['title'].'</a></div>'."\n";

  if(($rowCounter / 3) == 1 OR ($rowCounter % 3) == 0 OR (count($data) - $dataCounter) < 3)
  {
    echo '</div>'."\n";
    $newRow = true;
  }
  $rowCounter++;
  $dataCounter++;
}

Whatever, your problem lay on conditions...

People are also looking for solutions to the problem: php - PHPUnit_Extensions_Selenium2TestCase return data when finish

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.