Array value is replaced when second one stores in php

About First part mistakes :

<?php
    if(isset($_POST['radio'])){
        $_SESSION['sz']=$_POST['radio'];
        $si=$_SESSION['sz'];
    }
    <a href="https://stackoverflow.com/questions/32310282/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="https://stackoverflow.com/questions/32310282/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="https://stackoverflow.com/questions/32310282/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="https://stackoverflow.com/questions/32310282/pageOne.php">CLICK HERE TO RETURN ON PAGE ONE!</a>";
?>

Leave a Comment