Jump to content

Show 3 blog entries then 3 images then 3 blog entries...


onjegolders
 Share

Recommended Posts

Was just wondering how feasible/complicated it would be to display 3 blog entries, followed by 3 images which will be stored in the blog-index page, followed by the next 3 blog entries, then the next 3 images and so on and so-forth?

I'm currently converting a static site (where this was obviously simple) and as a PHP beginner, I would ideally like to know whether the code will become so complicated that it's better to say to the client "Hey let's do this a different way".

I had a quick play around and I made a blog entries loop then an images loop, problem is I don't know if it's possible to loop them within another loop...

<?php  // Show 3 blog entries
$entries = $page->children("sort=-sort, limit=3");
$count = 0;

foreach ($entries as $entry) { 

$count++; 
$class = "blog_box"; 
if ($entry == $entries->last()) {$class .= " blog_box_last"; }
if ($entry == $entries->first()) {$class .= " blog_box_first"; }
if (0 == $count % 2) { $class .= " blog_box_even"; }
?>

<div class="<?php echo $class; ?>">

<div class="blog_text">

<h3><?php echo $entry->title; ?></h3>
<h6><?php echo $entry->entry_date; ?></h6>
<p><?php echo $entry->body; ?></p>

</div><!-- /.blog_text -->

<?php if ($entry->image) { 

$image = $entry->image->width(350);

?>

<img src="<?php echo $image->url; ?>" width="<?php echo $image->width; ?>" alt="<?php echo $entry->title; ?>" />

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /.blog_box -->

<?php }

?>

// Show 3 blog images

<?php $blog_images = $page->get("image_uploads")->find("limit=3");
foreach ($blog_images as $img) { 
$b_img = $img->size(200,140); ?>
<img src="<?php echo $b_img->url; ?>" width="<?php echo $b_img->width; ?>" height="<?php echo $b_img->height; ?>" alt="<?php echo $b_img->description; ?>" class="small_frame" />
<?php } ?>
Link to comment
Share on other sites

This should be reasonably simple to do in PW. I think your best bet is to just grab all the entries you want to display at the top, and then use a counter to keep track of where you are. Written in the browser here, so may need adjustment:

$entries = $page->children("sort=-sort");
$images = $page->get("/image_uploads/"); 
$cnt = 0;

do {
   $cnt++; 
   $entry = $entries->shift();
   if($entry) {
       // output 1 entry
   }

   if($cnt == 3) {
       while(++$cnt <= 6) {
           $image = $images->shift();
           if($image) {
               // output 1 image
           }
       }
       $cnt = 0;
   }

} while(count($entries) && count($images)); 
Link to comment
Share on other sites

Hi onjegolders. Just wondering: Is your Processwire learning experience also one of your first times working with PHP? A 'do while loop' is pretty common stuff. Along with for, foreach, if, elseif, else etc. Don't take this the wrong way, i'm just curious.

As for shift; i think this is just an PW api shortcut to a standard php array_shift function, for use on Page- WireArrays. It's on the PW cheatsheet ( http://processwire.com/api/cheatsheet/ )

Link to comment
Share on other sites

Hi onjegolders. Just wondering: Is your Processwire learning experience also one of your first times working with PHP? A 'do while loop' is pretty common stuff. Along with for, foreach, if, elseif, else etc. Don't take this the wrong way, i'm just curious.

As for shift; i think this is just an PW api shortcut to a standard php array_shift function, for use on Page- WireArrays. It's on the PW cheatsheet ( http://processwire.com/api/cheatsheet/ )

Hi Sinnut, yep I'm very new to PHP, I'm more familiar with ExpressionEngine syntax so this is all relatively new to me!

Thanks for the pointers.

Link to comment
Share on other sites

Hi Sinnut, yep I'm very new to PHP, I'm more familiar with ExpressionEngine syntax so this is all relatively new to me!

You are doing a great job of learning the PHP basics and asking all the right questions. Unlike what you learn with EE, everything you learn in ProcessWire (PHP) will be things you can take with you to almost any other development situation, whether in ProcessWire or not.

There are two types of while() loops. A regular while loop only executes while the condition is met. It will execute 0 or more times, since the condition has to be met before it will begin looping:

while(condition) {
   // some code
}

The other type of while() loop starts with a "do". Note that the condition check is at the bottom rather than the top. That means that this loop will always execute 1 or more times, since it doesn't decide whether to continue for another round until it reaches the end.

do {
   // some code
} while(condition); 

Like Sinnut said, the shift() function is just a way to shift the first item off the beginning of the array. I don't really know the source of the word 'shift' but I used it in PW's API just for consistency with PHP's array_shift function. If you wanted to instead pop off the last item from the array, you'd use the pop() function (which is consistent with the naming of PHP's array_pop function, and a little easier to remember). I'm not good at remembering all the PHP or PW function names off the top of my head... I pretty much always have to refer to the cheatsheet or docs. :)

Link to comment
Share on other sites

You are doing a great job of learning the PHP basics and asking all the right questions. Unlike what you learn with EE, everything you learn in ProcessWire (PHP) will be things you can take with you to almost any other development situation, whether in ProcessWire or not.

There are two types of while() loops. A regular while loop only executes while the condition is met. It will execute 0 or more times, since the condition has to be met before it will begin looping:

while(condition) {
// some code
}

The other type of while() loop starts with a "do". Note that the condition check is at the bottom rather than the top. That means that this loop will always execute 1 or more times, since it doesn't decide whether to continue for another round until it reaches the end.

do {
// some code
} while(condition);

Like Sinnut said, the shift() function is just a way to shift the first item off the beginning of the array. I don't really know the source of the word 'shift' but I used it in PW's API just for consistency with PHP's array_shift function. If you wanted to instead pop off the last item from the array, you'd use the pop() function (which is consistent with the naming of PHP's array_pop function, and a little easier to remember). I'm not good at remembering all the PHP or PW function names off the top of my head... I pretty much always have to refer to the cheatsheet or docs. :)

Thanks Ryan, thanks for your kind words, I do feel good just learning the basics of this system, it's so intuitive and the support is pretty damn good too ;)

Link to comment
Share on other sites

Try changing the last line to this:

} while(count($entries) || count($images));

Basically replacing the "&&" (and) with "||" (or). If that doesn't do it, paste in your code so we've got the full context to look at.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...