Jump to content
onjegolders

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

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

Share this post


Link to post
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)); 

Share this post


Link to post
Share on other sites

Wow thanks Ryan though it's a bit over-my-head in PHP terms so will try and get learning before I put the code in! I hadn't even heard of "do" or "shift" before!

Share this post


Link to post
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/ )

Share this post


Link to post
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.

Share this post


Link to post
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. :)

Share this post


Link to post
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 ;)

Share this post


Link to post
Share on other sites

Sorry Ryan, I seem to be getting just one blog entry showing up when I add my code to yours, do you have any idea why that may be?

If not, don't worry, I'll just drop that part of the page as it's getting the better of me!

Share this post


Link to post
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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By clickpass
      I want to split my output of an image array, see my code here:
      <div class="row"> <?php foreach($page->umf_imgs as $image) {echo "<div class='one-fourth column refs'><img src='$image->url'></div>";} ?> </div> <div class="row"> <?php foreach($page->umf_imgs as $image) {echo "<div class='one-fourth column refs'><img src='$image->url'></div>";} ?> </div> <div class="row"> <?php foreach($page->umf_imgs as $image) {echo "<div class='one-fourth column refs'><img src='$image->url'></div>";} ?> </div> The number of images is 11, but I only want to add four in a row. Is it possible to grab them, e.g. image 1-4, image 5-8, image 9-12?
    • By rareyush
      hi everyone
       
      I am trying get a full width sub-menu on 3rd item in menu or navbar which is coming from a loop.
      i was hoping this can be done by using jquery by targeting 3rd element in navbar but jquery is blocked in some browser by default so is there any other method I can do this
    • By alxndre
      I just happened upon something that I think is curious, and I'm wondering if this is default behavior. So help me because I know nothing.
      While writing a module, i logged something in wire('log') inside the module's init() function. I was surprised to see that the text was being logged multiple times continuously. So I tried to open a small random module and logged something in its init() function as well, and the same thing happened - the text being logged many times.
      My question is, is this supposed to happen? I just want to understand what's going on. My concern is that if I do something huge in the init function, it will get called repeatedly, as with the log, and cause performance issue. Please note that these are both autoload modules. I was expecting them to log at least once when I refresh, but not continuously like what happened.
      Please let me know. Thanks.
    • By MilenKo
      Hello guys. I am almost on the final line of building my second complete website with ProcessWire and got stuck a bit on the recipe nutrition presentation. I am not sure if I would be able to explain it promptly, but will do my best. So I have the code in my HTML template:
      So to fill up the values in the frontend, I am using a simple TextArea field and dropping the following content:
      As far as I needed to split the text field into separate lines and every line into 3 parts (nutrition name, quantity and measure) I used regex code to sort everything through and the results are OK. Now the issue I am facing is that as per the theme, I need to assign class to left or right block and on top of that to close the nutrition-detail div after every right-box div.
      For the class I added $counter and using odd:even managed to assign the class of left-box & right-box. But I am having difficulty to insert the opening div of nutrition-detail on the beginning of every row and the closing div after the second block. Here is the code that sort of works, but is not inserting the divs properly:
      Any suggestions how to go around or some simple function to introduce to make it work? I tried to add another check for odd:even and if the result is odd, to insert the <div class="nutrition-detail"> and close the div if even, but I am still missing something...
    • By creativejay
      My brain is probably just tiring out on me right this moment, I'm hoping that by the time I write out my problem I'll see the way through it. If you're reading this, it didn't work.
      Structure in question is:
      Series Page Product pages Page Fields for each product page Some of these fields are repeaters Fields within the repeater I have made an array of page fields so I don't have to keep track of them all as I develop:
      $listings = $page->children; // grab all the published children of the Series page foreach($listings as $l) { // loop through the children foreach($l->fields as $f) { } // loop through each child's page fields where they have a value set } Then I break down how to handle each type of field:
      if($f->type == 'FieldtypeFile') { } elseif($f->type == 'FieldtypeDatetime'||$f->name == 'prod_status_pages'||$f->type == 'FieldtypeImage'){ } elseif($f->type == 'FieldtypePage'){ } These are all largely working as expected (though I do have a couple of offset/isset exceptions to clean up...)
      It's when I get to the repeaters that I run into trouble getting the API calls to work.
      Just cycling through the fields as above, the output for a FieldtypeRepeater is the ID of the repeater in that field's array. Everything I read suggests I should treat a repeater the same as I would treat a page, which leads me to the following code.
      elseif($f->type == 'FieldtypeRepeater'){ // Repeaters need special treatment otherwise output is just ID $th .= "<th><b>{$f->label}</b></th>\n"; $trows = ""; // creating an empty variable to build my foreach into foreach($f->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID $trows .= "{$rf->label}: {$f->get($rf)->title} ({$rf->type})<br />\n"; // add an entry to the variable } $rows .= "<td style='padding: 8px 16px; vertical-align: middle;'>".$trows."<br />\n ({$f->type})</td>\n"; // back out to rendering the Repeater field }  
      What I would hope would output in the HTML I've been building is something like:
       
      <td style='...'>Lo temp: -40 (Integer)<br /> Hi temp: 75 (Integer)<br /> Storage Lo: -40 (Integer)<br /> Storage Hi: 85 (Integer)<br /> Functional to: 85 (Integer)<br /> (FieldtypeRepeater)</td> So what I'm trying to do here is loop through the populated fields in the unknown Repeater field, and output them as a simple (so far) text list of the repeater.field and its value (and then its type for my reference).
      I'm afraid typing this out has fixed some syntax but not enough to get this working as I'd hoped.
      Please note not all Repeater fields are integers. Some also have floats, files, or options, and probably a couple others I'm forgetting.
       
      I appreciate your time in taking a look at this!
×
×
  • Create New...