add item to arrayobject with a form

61 Views Asked by At

for better understanding i describe my task. We have to create a tool in php to add mobile phones with a form into a list one by one. Later the list should be exportet to a CSV/XML file. this is done with an Arrayobject. My problem is that i can enter mobiles with the form, but they will get overridden and not added. So i only have one phone at a time in my list. my code so far: HTML:

      <div>
    <form name="Eingabe" action="handler.php" method="POST">
        <label for="Hersteller">Hersteller</label>
        <input id="Hersteller" name="Hersteller" type="text"><br>
        <label for="Modell">Modell</label>
        <input id="Modell" name="Modell" type="text"><br>
        <label for="Preis">Preis</label>
        <input id="Preis" name="Preis" type="number" min="1" max="1000"><br>
        <label for="Seriennummer">Seriennummer</label>
        <input id="Seriennummer" name="Seriennummer" type="text"><br>
        <br>
        <input name="Senden" type="submit" value="Speichern">
    </form>
  </div>

List:

    <?php


class handy /* extends guid */ {

    private $id;
    private $hersteller;
    private $modell;
    private $seriennr;
    private $preis;

    public function setid($id)
    {
        $this->id = $id;
    }

    public function sethersteller($hersteller)
    {
        $this->hersteller = $hersteller;
    }

    public function setmodell($modell)
    {
        $this->modell = $modell;
    }

    public function setseriennr($seriennr)
    {
        $this->seriennr = $seriennr;
    }

    public function setpreis($preis)
    {
        $this->preis = $preis;
    }

    public function getid()
    {
        return $this->id;
    }

    public function gethersteller()
    {
        return $this->hersteller;
    }

    public function getmodell()
    {
        return $this->modell;
    }

    public function getseriennr()
    {
        return $this->seriennr;
    }

    public function getpreis()
    {
        return $this->preis;
    }

}

?>
<?php
class handyliste extends ArrayObject {

public function addhandy($i, $hdy)
{
    $this->offsetSet($i, $hdy);
}
?>

Handler:

        $h = new handy();
    $h->sethersteller($_POST['Hersteller']);
    $h->setid(uniqid());
    $h->setmodell($_POST['Modell']);
    $h->setpreis($_POST['Preis']);
    $h->setseriennr($_POST['Seriennummer']);
    $hl = new handyliste();
    $hl->addhandy(null, $h);
 echo "<table>";
    echo "<thead><tr><th>ID</th><th>Hersteller</th><th>Modell</th><th>Seriennummer</th><th>Preis</th></tr></thead>";
    foreach ($hl as $h)
    {
        echo "<tr><td>";
        echo $h->getid() . "</td><td>";
        echo $h->gethersteller() . "</td><td>";
        echo $h->getmodell() . "</td><td>";
        echo $h->getseriennr() . "</td><td>";
        echo $h->getpreis() . "€</td></tr>";
    }
    echo "</table>";

i hope someone can help. with a normal array it would be easy i think, but i never worked with arrayobjects before.

1

There are 1 best solutions below

6
On

You do have something like:

$hl = new handyliste();

Now, at the end of your code, after you populate the data you can do something like:

$_SESSION['hl'] = $hl;

To use the sessions you need to have on top of your code the line:

session_start();

The will start or continue a session. Now, before doing

$hl = new handyliste();

You need to check with something like:

$hl = $_SESSION['hl'];

And only add elements, you can if not working, cast the list (example), this will save on sessions, and you can see this vars on each page that has got (session_start();) for more info on sessions please refer to php.net