2013-03-25 64 views
2

我的脚本通过我的WordPress网站上的帖子循环。如何处理嵌套循环中的变量?

我还计算未发布的帖子数量并将其存储在我的$i变量中。

但是,由于这是遍历多个结果,我需要在每次迭代后将最终的$i图存储在唯一变量中。

我该如何处理?我可以在任何阶段使用我的$currentID变量吗?

这是我的PHP参考片段:

while ($query->have_posts()) : $query->the_post(); 

    $currentID = get_the_ID(); 

     while ($events_list->have_posts()) : 

      $events_list->the_post(); 

      $postdate = get_the_date('Y-m-d'); 
      $currentdate = date('Y-m-d'); 

      if ($postdate > $currentdate) { 
       $i++; 
      } 

     endwhile; 

// preferrably store the total of $i here in a unique variable then reset $i 

    the_content(); 

endwhile; 

为:-)

+1

你的意思是在每次运行循环后存储$ i的值的数组? – Patashu 2013-03-25 23:32:06

+0

是的。所以在每个循环结束时,我会在一个唯一的变量名称中有一个“$ i”总数。 – michaelmcgurk 2013-03-25 23:35:35

+1

如果你发现自己在想像'我想存储任意数量的命名变量',你实际上需要一个具有数值数组的变量。阅读关于阵列,你会看到我的意思 – Patashu 2013-03-25 23:36:33

回答

1

任何指针非常感谢为什么不能有一个数组由$currentID一键保存所有的价值观呢?

$postCount = array(); 
while(condition){ 
    $currentID = get_the_ID(); 
    $i = 0; // don't forget to reset your counter 
    // processing goes here 
    while(condition){ 
    // more processing goes here 
    $i++; 
    } 
    $postCount[$currentID] = $i; 
} 

,将让你用含有$i用于外循环的每次迭代值的阵列。 $i的值将存储在等于$currentID的密钥中。不要忘记在每次迭代后重置计数器!

+0

酷 - 让我试试这个,我会马上回来:) – michaelmcgurk 2013-03-25 23:36:52

+0

这工作完美!非常感谢 !! :) – michaelmcgurk 2013-03-25 23:39:57

+1

@mic - 高兴地帮忙! :) – Lix 2013-03-25 23:42:29