Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:34+00:00 2026-05-14T23:44:34+00:00

I have two for each loops and I am trying to output something different

  • 0

I have two for each loops and I am trying to output something different for each second result:

foreach ($wppost as $wp) {
            $wp_title = $wp->post_title;
            $wp_date = strtotime($wp->post_date);
            $wp_slug = $wp->post_name;
            $wp_id = $wp->ID;
            // Start Permalink Template
            $wp_showurl = $wp_url;
            $wp_showurl = str_replace("%year%", date('Y', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%monthnum%", date('m', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%day%", date('d', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%hour%", date('H', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%minute%", date('i', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%second%", date('s', $wp_date), $wp_showurl);
            $wp_showurl = str_replace("%postname%", $wp_slug, $wp_showurl);
            $wp_showurl = str_replace("%post_id%", $wp_id, $wp_showurl);
            // Stop Permalink Template
            $wp_posturl = $blog_address . $wp_showurl;
            echo '<li><a href="'.$wp_posturl.'" title="'.$wp_title.'">'.$wp_title.'</a></li>';          
        }

For this one I want it to echo <li class="even"> instead of just <li> for each second result. I think this will have to be changed to a for loop to accomplished this but I am not sure how that is done without breaking it.

Exact same for this one:

if(is_array($commenters)) {
       foreach ($commenters as $k) {
         ?><li><?php
          if($ns_options["make_links"] == 1) {
            $url = ns_get_user_url($k->poster_id);
            if(trim($url) != '')
            {
                echo "<a href='" . $url . "'>";
            }
          }
          if($ns_options["make_links"] == 2) {
            $url = ns_get_user_profile($k->poster_id);
            echo "<a href='" . $url . "'>";
          }


          $name = $bbdb->get_var("
       SELECT user_login 
       FROM $bbdb->users
       WHERE ID = $k->poster_id");
         echo ns_substr_ellipse($name, $ns_options["name_limit"]);

          if(trim($url) != '' && $ns_options["make_links"] == 1) {
             echo "</a>";
          }
          if($ns_options["make_links"] == 2) {
            echo "</a>";
          }
          if ($ns_options["show_posts"] == 1)
          {
          echo " (" . $k->num_posts . ")\n";
          }
          echo $ns_options["end_html"] . "\n";
          unset($url);
       }
    } else {
      ?></li><?php
    }

Thanks,

Wade

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:44:35+00:00Added an answer on May 14, 2026 at 11:44 pm

    You can create a “counting” variable, and then use the result of $count % 2 to determine if it is an odd or even row:

    $rowCount = 0;
    foreach ($wppost as $wp) {
      // stripping stuff:
      echo '<li';
      // increase $rowCount - if the remainder of a division by 2 is 1, echo extra class info:
      if ($rowCount++ % 2 == 1 ) echo ' class="even"';
      echo '><a href="'.$wp_posturl.'" title="'.$wp_title.'">'.$wp_title.'</a></li>';          
    }
    
    
    if(is_array($commenters)) {  
       $commentCount = 0;
       foreach ($commenters as $k) {
         ?><li<?php echo ($commentCount++%2==1)?' class="even"':''?>><?php
         //stripped the rest of the loop:
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have two entities, each from a different database and therefore different edmx files.
I have two lists, each on different tabs (sub-sites) of the site. I would
Hi I want to have two tables each have an INT id column which
I have two classes that each need an instance of each other to function.
The two key event argument classes KeyEventArgs and PreviewKeyDownEventArgs each have two properties, KeyCode
I have two applications written in Java that communicate with each other using XML
I have two tables in a DataSet where the ID field on each is
I have two strings and I would like to mix the characters from each
I have two lists of objects. Each list is already sorted by a property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.