内循环回路引起不希望输出

内循环回路引起不希望输出

问题描述:

我有以下的环...内循环回路引起不希望输出

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

内...

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;

然而,当我执行我的代码,说我的while()循环返回4个值,我对()循环然后输出同样的事情10,在浏览器中的显示为...

enter image description here

所有我想做的事是每个<span class="srch-val'>是每个“S-RCH-VAL”下课后添加一个数字,所以S-RCH-VAL-1,S-RCH-VAL-2等..

+1

“每个” 是什么? – bfavaretto 2012-03-26 15:40:33

+0

你能举一个你想要的输出的例子吗?很明显,for循环会打印10次相同的东西,因为$ value不会改变。 – flo 2012-03-26 15:42:42

+0

您的意思是免费设置? – 2012-03-26 15:43:04

你有你的foreach循环中的额外的循环。删除这个循环,直接在foreach循环中执行echo,每次使用时都增加$i

像这样:

$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>"; 
    } 
} 

您在创建一个变量你的foreach循环的开始(比如$counter == 1;

比你的for循环应该是:

for($i = $counter; $i <= $counter+10; $i++){ 
    //do your span-class thing here 
} 

,并在foreach循环结束做到:$counter += 10;