2015-01-05 19 views
0

遍歷數組我有一個數組像這樣如何在PHP

$steps = explode("[;;]",$str); 

所以$步驟認爲我必須使用顯示在PHP中的步/步程序價值。

數組看起來像這樣

$steps = array('corredores' , 'something' , 'something'...and so one); 

我試圖根據存儲陣列中的第一步,我做這樣的事情

switch($steps[0]){ 
    case 'categorias' : 
    include 'obj/categorias.php'; 
       //$step='categorias'; 
       break; 
    case 'corredores': 
    include 'obj/corredores.php'; 
       //$step='corredores'; 
       break; 
    case 'monedas': 
    include 'obj/monedas.php'; 
       //$step='monedas'; 
       break; 
    case 'location': 
    include 'obj/location.php'; 
       //$step='location'; 
       break; 
    default: 
    break; 
} 

//這裏的值顯示形式我試圖匹配下一個數組中的值,我將在每個步驟中保存此後期值

if(isset($_POST['next'])) 
{ 
include 'obj/'.$_POST["next"].'php'; 
       //$step='categorias'; 
       break; 

} 
else { 


} 

所以,當我點擊每個文件的下一個按鈕,它應與該數組中的下一個值,並顯示相關文件

每一個文件的HTML內容有以下結構

<form method="post" action="./index.php" name="form_name"> 
<table><tr><th>Some Name</td></tr> 
<tr><td><input type="submit" value="next"></td></tr></table> 

    <input type="hidden" name="next" value="<?php //here i will add the value for next ?>"> 
</form> 

任何一個可以給我一些建議

+0

你的$ steps變量實際上是怎樣的? –

+0

@NitsanBaleli請檢​​查我已更新的問題 – Vikram

+0

每次打印表格時,隱藏輸入的值應該是$ steps [current + 1]。使用Key()函數 – briosheje

回答

1

根據我的理解,您需要使用POST var搜索數組,然後在input標籤的value屬性中回顯下一個值。

<?php 
$key=0; 
if(isset($_POST['next'])){ 
    include 'obj/'.$_POST["next"].'php'; 
    $key = array_search($_POST["next"], $steps); 
    if($key !== false) $key++; 
    if($key==count($steps)) $key=0; 
     //if post contains last element then set $key to first OR do whatever you want 
    break; 
} 
else { } 
?> 

<input type="hidden" name="next" value="<?php echo $steps[$key]; ?>">