dslk6326846 2014-12-15 08:08
浏览 62

使用表单将项添加到数组对象

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条回答 默认 最新

  • doudiza9154 2014-12-15 08:34
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了