dongqiao1151 2015-02-05 07:15
浏览 51

如何来回遍历数组

I have an array something like this

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

So the $steps holds value that I have to use to show a step/step procedure in PHP .

The array looks like this

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

I am trying to display forms according to the value saved in the array . I am able to go forward using some validation but I have trouble in moving to the previous name in the array , for the first step I am doing something like this

$steps = explode("[;;]",$_POST['str']);
$x = explode("=",$steps[0]);
$pais1 = $_POST['pais'];
$cnt=0;
    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;  
    }

//Here I am trying to match the next value from the array that I will save in this post value on every step

    if(isset($_POST['next1']) &&$_POST['method'] != "fase1")
{
$cnt =$_POST['cnt'];
$cnt++;
$array_var = unserialize(base64_decode($_POST["next1"]));
$steps = $array_var;
$pais1 = $_POST['pais'];

    $x = explode("=",$array_var[$cnt]);
include  'obj/'.trim($x[0]).'.php';
                //$step='categorias';   
}

So when I click on the next button of each file it goes to the next file in the array , But Now I need to add similar functionality to the previous button so that it goes back

The HTML content of every files has following structure

   <form method="post" action="./index.php" name="form_name">
    <table><tr><th>Some Name</td></tr>
    <tr><td><input type="submit" value="previous"></td><td><input type="submit" value="next"></td></tr></table>
<input type="hidden" name="next1" value="<?php print base64_encode(serialize($steps))  ?>">
<input type="hidden" name="pais" value="<?php echo $pais1?>"/>
<input type="hidden" id="cnt" name="cnt" value="<?php echo $cnt ?>" />
    </form>

Can any one give me some suggestion

Thanks in Advance

  • 写回答

2条回答 默认 最新

  • duanlu1950 2015-02-05 07:48
    关注

    If I am not reading your code wrong, you already have an array of all the objects stored in next1 and cnt as your currently counter. Essentially, what you need to do is to detect whether the user has pressed the Next or Previous button

    <input type="sumbit" name="next" value="Next">
    <input type="submit" name="previous" value="Previous">
    

    A form can have more than 1 submit button. Just be sure to name them differently. Then when you process your form:

    if (isset($_POST['next'])) {
      // go on to the next object
      $cnt = $_POST['cnt'] + 1;
    
    } else if (isset($_POST['previous']) {
      $cnt = $_POST['cnt'] - 1;
    }
    
    // retrieve object from array and include its file
    

    It is important that you don't increment your cnt like above -- and only increment or decrement it before you press the buttons.

    In short, change the value of cnt depending if the user presses Next or Previous.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测