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

  • SEARCH
  • Home
  • 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 7710015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:00:28+00:00 2026-06-01T01:00:28+00:00

I have the following loop… for ($i = 1; $i <= 10; $i++) {

  • 0

I have the following loop…

for ($i = 1; $i <= 10; $i++) {
    echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
}

within…

while ( $query->have_posts() ) : $query->the_post();
if ( $keys = get_post_custom_keys() ) {
  echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>";
  echo the_title();
  echo "</span>";

  foreach ( (array) $keys as $key ) {
    $keyt = trim($key);
    if ( '_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt )
      continue;
    $values = array_map('trim', get_post_custom_values($key));
    $value = implode($values,', ');
    for ($i = 1; $i <= 10; $i++) {
                echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
            }

  }
   echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />';      echo the_excerpt()."</div>";}
  $i++;

endwhile;

When I execute my code however, say my while() loop returns 4 values, my for() loop then outputs 10 of the same thing, in my browser its shown as…

enter image description here

All I want to do is for each <span class="srch-val'> is to add a number after each ‘srch-val’ class, so srch-val-1, srch-val-2 etc…

  • 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-06-01T01:00:30+00:00Added an answer on June 1, 2026 at 1:00 am

    You have an extra for-loop inside your foreach-loop. Remove this loop and just do the echo directly inside the foreach-loop and increment $i each time you actually use it.

    Like this:

    $i = 1;
    while ( $query->have_posts() )
    {
        $query->the_post();
        if ( $keys = get_post_custom_keys() )
        {
            echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>";
            echo the_title();
            echo "</span>";
    
            foreach ( (array) $keys as $key )
            {
                $keyt = trim($key);
                if ( '_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt )
                    continue;
                $values = array_map('trim', get_post_custom_values($key));
                $value = implode($values,', ');
    
                echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
                $i++;    // move the incrementer here so that you only increment when you actually use it.
            }
    
            echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />';      echo the_excerpt()."</div>";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following loop in my view foreach ($value as $row): echo
I have the following loop: while($row = mysql_fetch_assoc($result)) { <some code> if ($user) echo
I have written the following while loop in a php file. The file does
I have following code: $boxId = 1; $explainationBox='<input type=button id=<?php echo $boxId; ?> value=send
I have following while loop. $readNews_SQLselect = SELECT ; $readNews_SQLselect .= id, content ;
I have the following loop: for row in rows: print row.value('customer', 'name') # print
Hey guys I have the following while loop which seems not to stop. It's
I have following PHP loop + SQL Update query. for ($i=0;$i<count($_POST['id']);$i++) { if(!isset($_POST['live'][$i])){ $_POST['live'][$i]
I have following .each jQuery loop. $('.category').each(function(index) { if( $('.category > span').parent().next('h2') ) {
I have the following PowerShell loop: Set-Variable -Name YELLOW -Option ReadOnly -Value 0 Set-Variable

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.