2015-04-20 44 views
1

我有一定数量的帖子,每个帖子都包含一个模式(有点弹出),这个模式的HTML在$ html [] var中。下面的代码运行良好,除非您可以看到它不是动态的。用动态值重复var PHP

我已经试过:

$post_count = [1,2,3,4,5,6,7,8,9,10,11,12]; // i've to mannualy add a number according to the number of posts 
$counter = 1; 
foreach($number_of_modals as $modal){ 
    echo $html[$counter]; 
    $counter++; 
} 

什么,我需要说明完成

$post_count; // if this val is 3 for example 

echo $html[1]; 
echo $html[2]; 
echo $html[3]; 
+1

究竟有什么问题呢? – Rizier123

+0

我喜欢重复'$ html'多次post_count – Paul

+0

得到no。通过array.push在loop.it中将值推送到数组中。没有posts = 3那么foreach循环将用于推送$ post_count数组中的值。 – DevLakshman

回答

2

正如保罗所说,使用一个for循环或while循环:

$post_count = 3; 
$counter = 0; 
while ($counter <= $post_count) { 
    echo $html[$counter]; 
    $counter++; 
} 
+1

谢谢我接受它作为最佳答案,它的工作原理是我想要的。对不起,如果这个问题有点不清楚 – Paul

+0

“我需要完成什么的解释”部分几乎解释了应该是什么答案。 –

1

你可以试试这个吗?

foreach($number_of_modals as $key => $modal){ 
    echo $html[$key]; 
} 

因为你没有给$ number_of_modals阵列的结构,我认为它像$ HTML阵列

$modal = array('1', 'abc', 'etc'); 

它转换为$modal = array(0 => '1', 1 => 'abc', 2 => 'etc');

在这的foreach每个$按键对应到0,1,2,这给出你正在寻找的$计数器。

+0

有几句话的解释会让这个例子更好。 – Thom

+0

感谢您的回答! – Paul

+0

@TheThom我增加了解释。不客气,保罗。 – ag0702