Setting every nth range in php

I’m trying achieve a specific style for every 3rd 4th and 5th element in loop. I know it is possible in css but I want to learn it in php too. The following code inserts class to every 3rd element.

global $post_num;
if ( ++$post_num % 3 == 0 )
    $class = 'whatever';
echo $class;

I’ve tried if ( ++$post_num % 3 == 0 ) && ( ++$post_num % 4 == 0 ) && ( ++$post_num % 5 == 0 ), but it doesn’t work

Related posts

Leave a Reply

3 comments

  1. The problem seems to be you are attempting to increment the $post_num multiple times within the if clause. Try this instead.

    global $post_num;
    $class = '';
    if (($post_num % 3 == 0) || ($post_num % 4 == 0) || ($post_num % 5 == 0)) {
      $class = 'whatever';
      ++$post_num;
    }
    echo $class;
    

    But that said, I am still unclear on what role the ++$post_num plays since wouldn’t this logic be placed in a larger loop? So wouldn’t this work?

    global $post_num;
    $class = '';
    if (($post_num % 3 == 0) || ($post_num % 4 == 0) || ($post_num % 5 == 0)) {
      $class = 'whatever';
    }
    echo $class;
    

    Meaning $post_num is a value based on the content rolling through your script logic? And this clause will simply take the value of $post_num and act on it? If you have more code to illustrate the structure you have in place that would help.

    EDIT Just adding my own loop for this example so it’s clearer what is happening.

    for ($post_num = 1; $post_num <= 20; $post_num++) {
      $class = '';
      if (($post_num % 3 == 0) || ($post_num % 4 == 0) || ($post_num % 5 == 0)) {
        $class = 'whatever';
      }
      echo $post_num . ' | ' . $class . '<br />';
    }
    

    But looking at the output it might not be what you expect, since 3rd, 4th or 5th with a modulus (%) operator will encompass a whole range of divisible items:

    1 | 
    2 | 
    3 | whatever
    4 | whatever
    5 | whatever
    6 | whatever
    7 | 
    8 | whatever
    9 | whatever
    10 | whatever
    11 | 
    12 | whatever
    13 | 
    14 | 
    15 | whatever
    16 | whatever
    17 | 
    18 | whatever
    19 | 
    20 | whatever
    

    ANOTHER EDIT Okay, I think I got it now. I am assigning the value of the modulus to the largest number to $post_count, then I set an array for the items you want to assign a class to in $class_value_array and then the if clause now uses in_array() to validate.

    for ($post_num = 1; $post_num <= 20; $post_num++) {
      $class = '';
      $post_count = $post_num % 5;
      $class_value_array = array(0, 3, 4);
      if (in_array($post_count, $class_value_array)) {
        $class = 'whatever';
      }
      echo $post_count . ' | ' . $post_num . ' | ' . $class . '<br />';
    }
    

    The output is now:

    1 | 1 | 
    2 | 2 | 
    3 | 3 | whatever
    4 | 4 | whatever
    0 | 5 | whatever
    1 | 6 | 
    2 | 7 | 
    3 | 8 | whatever
    4 | 9 | whatever
    0 | 10 | whatever
    1 | 11 | 
    2 | 12 | 
    3 | 13 | whatever
    4 | 14 | whatever
    0 | 15 | whatever
    1 | 16 | 
    2 | 17 | 
    3 | 18 | whatever
    4 | 19 | whatever
    0 | 20 | whatever
    

    Which I am pretty sure is what you are looking for.

  2. if ( ++$post_num % 3 == 0 ) && ( ++$post_num % 4 == 0 ) && ( ++$post_num % 5 == 0 )
    

    will give you the class on every gcd(3,4,5) = 60th row. Just do:

    if  ( ++$post_num % 5 == 0 )
    

    for five

    if( ++$post_num % 4 == 0 )
    

    for 4, etc.

  3. It looks like you want to apply a separate style to the third, fourth, and fifth item. Why complicate things? Just add a class indicating that the element is the third, fourth, or fifth element in the series. You can use CSS to determine if an element is a combination. You don’t want to use CSS which I assume means you don’t want to use the nth-child selector.

    This code is not CSS/HTML only, you can easily change the code from applying a class to executing a function, for example.

    for ( $i = 0; $i < 300; $i++ ) {
      $classes = array();
      if ( $i % 3 == 0 ) $classes[] = 'tri';
      if ( $i % 4 == 0 ) $classes[] = 'quad';
      if ( $i % 5 == 0 ) $classes[] = 'quint'; 
    
      echo "<div class='" . implode(' ', $classes), "'>Number " . $i . "</div>";
    }
    

    Just style the classes at this point. Here’s the output:

    <div class="tri quad quint">Number 0</div>
    <div class="">Number 1</div>
    <div class="">Number 2</div>
    <div class="tri">Number 3</div>
    <div class="quad">Number 4</div>
    <div class="quint">Number 5</div>
    <div class="tri">Number 6</div>
    <div class="">Number 7</div>
    <div class="quad">Number 8</div>
    <div class="tri">Number 9</div>
    <div class="quint">Number 10</div>