2011-12-02 117 views
1

所以我有這個片的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,我希望它是連續的。

我希望我已經說清楚了。

非常感謝先進。

馬特

回答

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

2

更換你的第二個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

0

如果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

0

不要做任何與你的循環指標複雜,只會讓你的代碼更難以閱讀。我將循環索引專門用於循環,並創建另一個變量來保存全部索引。

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