索引计数使用PHP for循环

问题描述:

所以我有这个片的PHP代码,索引计数使用PHP for循环

$countRows = count($meta[text_group]); 
for ($ind = 0; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$ind.'][name]" value="'.$meta[text_group][$ind][text_name].'" />';  
    echo '<input type="text" name="my-item-option['.$ind.'][value]" />'; 
} 

$countRows = count($meta[textarea_group]); 
for ($ind = 0; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$ind.'][name]" value="'.$meta[textarea_group][$ind][textarea_name].'" />';  
    echo '<textarea rows="10" name="my-item-option['.$ind.'][value]" cols="30"></textarea>'; 
} 

产生此作为HTML,

<input type="hidden" name="my-item-option[0][name]" value="Text Name 1" /> 
    <input type="text" name="my-item-option[0][value]" /> 
    <input type="hidden" name="my-item-option[1][name]" value="Text Name 2" /> 
    <input type="text" name="my-item-option[1][value]" /> 

    <input type="hidden" name="my-item-option[0][name]" value="Text Area Name 1" /> 
    <textarea rows="10" name="my-item-option[0][value]" cols="30"></textarea> 
    <input type="hidden" name="my-item-option[1][name]" value="Text Area Name 2" /> 
    <textarea rows="10" name="my-item-option[1][value]" cols="30"></textarea> 
    <input type="hidden" name="my-item-option[2][name]" value="Text Area Name 3" /> 
    <textarea rows="10" name="my-item-option[2][value]" cols="30"></textarea>    

请注意,在每个输入/ textarea的名称属性与分配来自$ ind变量的索引号,例如my-item-option [0] [name],my-item-option [0] [value]。什么即时试图实现为HTML是以下...

<input type="hidden" name="my-item-option[0][name]" value="Text Name 1" /> 
    <input type="text" name="my-item-option[0][value]" /> 
    <input type="hidden" name="my-item-option[1][name]" value="Text Name 2" /> 
    <input type="text" name="my-item-option[1][value]" /> 

    <input type="hidden" name="my-item-option[2][name]" value="Text Area Name 1" /> 
    <textarea rows="10" name="my-item-option[2][value]" cols="30"></textarea> 
    <input type="hidden" name="my-item-option[3][name]" value="Text Area Name 2" /> 
    <textarea rows="10" name="my-item-option[3][value]" cols="30"></textarea> 
    <input type="hidden" name="my-item-option[4][name]" value="Text Area Name 3" /> 
    <textarea rows="10" name="my-item-option[4][value]" cols="30"></textarea>    

因此,而不是$ ind变量重置为0,我希望它是连续的。

我希望我已经说清楚了。

非常感谢先进。

马特

好了,所以我的第一次尝试是错误的:)去关什么@Andre建议我想你也想改变你的第二环,以$使用我作为指标,而不是$ IND因为$ IND从不增加。

$countRows = count($meta[text_group]); 
    for ($ind = 0; $ind < $countRows; $ind ++) { 
     echo '<input type="hidden" name="my-item-option['.$ind.'][name]" value="'.$meta[text_group][$ind][text_name].'" />';  
     echo '<input type="text" name="my-item-option['.$ind.'][value]" />'; 
    } 

    $countRows = count($meta[textarea_group]); 
    for ($i = $ind; $i < ($countRows + $ind) ; $i ++) { 
     echo '<input type="hidden" name="my-item-option['.$i.'][name]" value="'.$meta[textarea_group][$ind][textarea_name].'" />';  
     echo '<textarea rows="10" name="my-item-option['.$i.'][value]" cols="30"></textarea>'; 
    } 
+0

这不会起作用,因为$ IND将已经等于$ countRows,所以第二个循环将不被执行 – Andre

+0

嘿罗伯特,不幸的是设置$ IND变量作为你的例子不工作,我认为这是因为$ ind是由$ countRows设置的? – Shoebox

更换你的第二个for循环这样的:

for ($i = $ind; $i < ($countRows + $ind) ; $i ++) 

而且$ I代替$ IND使用内循环

+0

你的anwser很容易遵循:D @Andre大拇指 – hungneox

+0

嘿,这几乎是那里但是对于第二个循环的HTML $ IND或索引号被设置为2,任何想法? – Shoebox

+0

' \t \t \t \t \t \t \t \t \t \t ' – Shoebox

如果count($元[textarea_group])大于计数( $元[text_group]); ,你只需要省略第一初值为第二循环

$countRows = count($meta[text_group]); 
for ($ind = 0; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$ind.'][name]" value="'.$meta[text_group][$ind][text_name].'" />';  
    echo '<input type="text" name="my-item-option['.$ind.'][value]" />'; 
} 

$countRows = count($meta[textarea_group]); 
for (; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$ind.'][name]" value="'.$meta[textarea_group][$ind][textarea_name].'" />';  
    echo '<textarea rows="10" name="my-item-option['.$ind.'][value]" cols="30"></textarea>'; 
} 
+0

嘿,不知道什么是错的,但输出产生以下\t \t' \t \t \t \t ' – Shoebox

+0

什么$ meta [text_group]和$ meta [textarea_group]的值? – hungneox

+0

$ meta [text_group]和$ meta [textarea_group]都是数组,因此当我回显$ countRows时,它们都返回一个数字。嗯,也许总结$ meta [text_group]和$ meta [textarea_group]并使用它作为$ ind ..不知道 – Shoebox

不要做任何与你的循环指标复杂,只会让你的代码更难以阅读。我将循环索引专门用于循环,并创建另一个变量来保存全部索引。

$overallIndex = 0; 

$countRows = count($meta[text_group]); 
for ($ind = 0; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$overallIndex.'][name]" value="'.$meta[text_group][$ind][text_name].'" />'; 
    echo '<input type="text" name="my-item-option['.$overallIndex.'][value]" />'; 
    $overallIndex++; 
} 

$countRows = count($meta[textarea_group]); 
for ($ind = 0; $ind < $countRows; $ind ++) { 
    echo '<input type="hidden" name="my-item-option['.$overallIndex.'][name]" value="'.$meta[textarea_group][$ind][textarea_name].'" />';  
    echo '<textarea rows="10" name="my-item-option['.$overallIndex.'][value]" cols="30"></textarea>'; 
    $overallIndex++; 
}