Page 1 of 1

Need help, adding values into arrays from forms

Posted: Fri Jan 02, 2009 10:17 am
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 :)

Re: Need help, adding values into arrays from forms

Posted: Fri Jan 02, 2009 10:43 am
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...

Re: Need help, adding values into arrays from forms

Posted: Fri Jan 02, 2009 11:02 am
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. :?

Re: Need help, adding values into arrays from forms

Posted: Sat Jan 03, 2009 11:43 pm
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...