0

I insert data to the array from an input box. But I don't know why I can't print the associative array. I can only echo the latest data from the input box. I want to add data to the array and echo it every time I write to the input box.

    <?php
    $part_insert_message = "";

    $inserted_parts = array();

    session_start();

    $part_inserted_id;
if(isset($_POST['submit'])) {
    $part_inserted_id = $_POST['arrdata'];
    $inserted_parts[$part_inserted_id] = $part_inserted_id; 
    echo sizeof($inserted_parts);
    // store session data
    $_SESSION['views']= $inserted_parts;
    $part_insert_message = "ID: " . $part_inserted_id;
}

?>

<html>
<body>
<div>
<h2>Part</h2>
<form action="array_session_example.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
Array Data: <input type="text" name="arrdata"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo $part_insert_message;
    foreach($inserted_parts as $key => $value){
        echo $key;
    }
?>
</div>
</body>
</html> 

1 Answer 1

2

You're creating a new array every time, and then adding that to the session. You'll need to check if an array has already been stored, and if it has add to that.

    $inserted_parts = array();

    session_start();
    if(isset($_SESSION['views']) && is_array($_SESSION['views'])) 
        $inserted_parts = $_SESSION['views'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.