Need help, adding values into arrays from forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Need help, adding values into arrays from forms

Post by Icethrill »

So I have been looking around for a solution to my problem but can't think come up with anything. At the moment I have an multi-dimensional associative array(I think thats what you call it) with some start values in it. And there is a form adding an one new array in the lastspot in my multi-dimensional array. The problem is I want the script to be able to add many values. But at the moment it just replaces the old value.

Also I am trying to save the values in the arrays using sessions. The written person in the form isnt saved when the page is reloaded. So two problems basically, adding more persons using the form and the session problem.

This is just a training for me, since I am pretty new to php. So this might be a really easy solution I havent thinked of. The code has english mixed with swedish variable names. So it might be confusing for some. Anyway here is the code:

Code: Select all

<?php
    session_start();
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Klasslista</title>
    <style type="text/css" media="screen"> 
    td{
        border: #000000 1px solid;
    }
    </style>
</head>
<body>
    <?php
        
        $_SESSION['people'] = array(
            array(
                "first_name" => "David",
                "last_name" => "Cakeman",
                "alder" => 21,
                "telefon" => "048224455"
            ), 
            array(
                "first_name" => "Daniel",
                "last_name" => "Mancake",
                "alder" => 22,
                "telefon" => "04823355"
            ), 
            array(
                "first_name" => "Donald",
                "last_name" => "Duck",
                "alder" => 31,
                "telefon" => "048555455"
            )
        );
        
        if($_POST["forname"] && $_POST["eftername"] && $_POST["age"] && $_POST["telefon"]){
            $_SESSION['new_person'] = array(
                "first_name" => $_POST["forname"],
                "last_name" => $_POST["eftername"],
                "alder" => $_POST["age"],
                "telefon" => $_POST["telefon"]
            );
            $_SESSION['people'][] = $_SESSION['new_person'];
        }
        ?>
        
        <form action="" method="post">
            Firstname: <input type="text" name="forname" />
            Lastnamn: <input type="text" name="eftername" />
            Age: <input type="text" name="age" />
            Telephone: <input type="text" name="telefon" />
            <input type="submit" value="Skicka" />
        </form>
           
        <?php
        print_r($_SESSION['people']);
        echo "<br />";
        
        echo "<table>";
        foreach($_SESSION['people'] as $person){
            echo "<tr>";
            echo "<td> {$person[first_name]} {$person[last_name]}</td>";
            echo "<td> {$person[alder]} år</td>";
            echo "<td> {$person[telefon]} </td>";
            echo "</tr>";
        }
        echo "</table>";
        ?>
</body>
</html>
Thanks in advance :)
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Need help, adding values into arrays from forms

Post by msurabbott »

The syntax for adding to the end of an array is simply:

Code: Select all

$array[] = something...
If you wanted to add it to a particular spot in the array you could use:

Code: Select all

$array['some_key'][] = something...
Hope this is what you're looking for...
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Re: Need help, adding values into arrays from forms

Post by Icethrill »

msurabbott wrote:The syntax for adding to the end of an array is simply:

Code: Select all

$array[] = something...
If you wanted to add it to a particular spot in the array you could use:

Code: Select all

$array['some_key'][] = something...
Hope this is what you're looking for...

Thanks for the answer, I am using that at the moment. It does work but I am probably using it in a bad way.

Here is the array I have added the written content too.

Code: Select all

 
# $_SESSION['people'] = array(
#             array(
#                 "first_name" => "David",
#                 "last_name" => "Cakeman",
#                 "alder" => 21,
#                 "telefon" => "048224455"
#             ),
#             array(
#                 "first_name" => "Daniel",
#                 "last_name" => "Mancake",
#                 "alder" => 22,
#                 "telefon" => "04823355"
#             ),
#             array(
#                 "first_name" => "Donald",
#                 "last_name" => "Duck",
#                 "alder" => 31,
#                 "telefon" => "048555455"
#             )
#         );
Here is the process to add a new spot in the array.

Code: Select all

#  if($_POST["forname"] && $_POST["eftername"] && $_POST["age"] && $_POST["telefon"]){
#             $_SESSION['new_person'] = array(
#                 "first_name" => $_POST["forname"],
#                 "last_name" => $_POST["eftername"],
#                 "alder" => $_POST["age"],
#                 "telefon" => $_POST["telefon"]
#             );
#             $_SESSION['people'][] = $_SESSION['new_person'];
#         }
#         ?>
I just want to be able to write one person then another one through the form. If I want to write in 2 people the first one I wrote in is overwritten by the second. Sorry if I am bad at explaining. :?
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Need help, adding values into arrays from forms

Post by msurabbott »

I see, well this this I suppose could be caused by the index of array() is the same if the contents of the array are the same, so it overwrites it. Regardless of whether or not this is true, I would suggest using objects.

Create a class called user, that has the 4 attributes you are storing. Then when you add the objects to the session use:

Code: Select all

$_SESSION['people'][] = serialze(new User('John', 'Smith', '31', '5555555555'));
When you need to pull this person from the session you can use this:

Code: Select all

$user = unserialize($_SESSION['people'][2]);
This ofcourse, is if you are using PHP5+, if this is not the case let me know and we can work something else out...
Post Reply