doushi1510 2015-08-31 11:34
浏览 12

当第二个存储在php中时,将替换数组值

I am storing session values into array getting from URL and try to display it (getting size of product in a shopping cart). But first value is replaced by second one.

My code:

if(isset($_POST['radio'])){
    $_SESSION['sz']=$_POST['radio'];
    $si=$_SESSION['sz'];
}

<a href="product_detail.php?pdt_id='.$pdid.'&add=' .$pdid .'&size='.$si.'" 
 class="cartBtn" onclick="return fun1()">Add to cart</a>';

?>

Display page:

$rt=$_GET['size'];
$_SESSION['wer']=$rt;
$array = $_SESSION['wer'];  //Assigns session var to $array
//print_r($array);  
echo $array[$x];
}
  • 写回答

1条回答 默认 最新

  • dousong2967 2015-08-31 13:20
    关注

    About First part mistakes :

    <?php
        if(isset($_POST['radio'])){
            $_SESSION['sz']=$_POST['radio'];
            $si=$_SESSION['sz'];
        }
        <a href="product_detail.php?pdt_id='.$pdid.'&add=' .$pdid .'&size='.$si.'"      class="cartBtn" onclick="return fun1()">Add to cart</a>';
    ?>
    
    1. Your a tag is completely wrong as @Hearner said. It should be out of the php tag or inside with an "echo" like this :

      echo "<a href='product_detail.php?pdt_id=".$pdid."&add=".$pdid."&size=".$si."' class='cartBtn' onclick='return fun1()'>Add to cart</a>";
      
    2. You cannot access your $si variable out of your if statement. As written here over, if your $si isn't declared before (since you said that it was not your complete code...) then $si (in the link href) does not exist. You should therefore declare it before your if statement OR place your link (a tag) inside your if statement too!

    3. What if your "$_POST['radio']" is NOT set ?? what happens? code missing... !! is $si declared anyway?

    About Second part :

    <?php
        for( $x = 0, $max = count($array); $x < $max; ++$x ) {
            $rt=$_GET['size'];
            $_SESSION['wer']=$rt;
            $array = $_SESSION['wer'];
            echo $array[$x];
        }
    ?>
    

    I don't understand what you're trying to do here... need more code/information... cannot help more without your whole code...

    EDIT :

    Here is a very simple example to show you how to keep your get vars into a session array.

    page one (pageOne.php):

    <?php
        session_start();
        if(!(isset($_SESSION['myTest']))){
            $_SESSION['myTest'] = "AWESOME";
            $_SESSION['varToKeep'] = [];
        }else{
            echo "A session is already started. This is : ".$_SESSION['myTest']."<br/>";
            if(count($_SESSION['varToKeep']>0)){
                echo "There are ".count($_SESSION['varToKeep'])." vars in the array!<br/>";
                for($i=0;$i<count($_SESSION['varToKeep']);$i++){
                    echo "Item ".$i." : ".$_SESSION['varToKeep'][$i]."<br/>";
                }
            }
        }
        echo "<br/>Click below to add a value in array<br/>";
        $random = rand(1,100);
        echo "<a href='pageTwo.php?mygetvar=STACKTEST".$random."'>Click here</a>";
    
    ?>
    

    page two (pageTwo.php):

    <?php
        session_start();
        echo "myTest value is : ".$_SESSION['myTest']."<br/><br/>";
    
        $value = $_GET['mygetvar'];
        $_SESSION['varToKeep'][] = $value;
        echo "<a href='pageOne.php'>CLICK HERE TO RETURN ON PAGE ONE!</a>";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)