从zend框架中的2个表中获取数据

时间:2015-06-18 07:21:12

标签: php mysql zend-framework

我已经开始学习Zend Framework 3天并且卡住了 我有一个网页,允许用户在选择框中选择手机品牌:Apple和Samsung。选择后,mySQL的数据将显示在下面(对于每个品牌,数据将不同)。现在我想实现一个搜索功能,它将列出所有匹配结果(按每个表计算)。在PHP基础上,这将很容易使用mysql查询LIKE,但现在在Zend Framework中我没有理想应该做什么。到目前为止,这是我的代码,但它不起作用:

index.phtml

<script>
    function change(){
        document.getElementById("brand").submit();

    }

</script>
<h3>Choose your brand</h3>
<form id="brand" method="post">
<select name="a" onchange="change();">
    <option selected="selected"></option>
    <option value="apple" >Apple</option>
    <option value="samsung">Samsung</option>

</select>
</form>
<h3>Choose your product or search for your devices  </h3>
<form id="search" method="post">
    <input type="text" name="search" placeholder="Enter device name">
    <input type="submit" name="subsearch">
</form>
<p><table border="2">
    <tr>
        <th>ID</th>
        <th>Product Name</th>
        <th>Manage</th>
    </tr>

     <?php 
        session_start();
        $_SESSION['a'] = $_POST['a'];
        if($_POST['a'] == 'apple'){
        foreach ($this->apple as $apple):
            ?>
            <tr>
                <td><?php echo $apple['id'] . ""; ?></td>
                <td><?php echo $apple['name'] . ""; ?></td>
                <td colspan="2"><a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'edit', 'id' => $apple['id'])) ?>"> Edit </a>
                    <a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'deleteapp', 'id' => $apple['id'])) ?>"> Delete </a></td>
            </tr>
    <?php endforeach; ?> 
    </p></table>
        <a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'showform'), 'show') ?>">Add new devices</a><?php } ?>
     <?php 
        if($_POST['a'] == 'samsung'){
        foreach ($this->samsung as $samsung):
            ?>
            <tr>
                <td><?php echo $samsung['id'] . ""; ?></td>
                <td><?php echo $samsung['name'] . ""; ?></td>
                <td colspan="2"><a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'edit', 'id' => $samsung['id'])) ?>"> Edit </a>
                    <a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'deletesam', 'id' => $samsung['id'])) ?>"> Delete </a></td>
            </tr>
    <?php endforeach; ?> 
    </p></table>
        <a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'showf'), 'show') ?>">Add new devices</a><?php } ?>

的IndexController:

 public function searchAction() {
        $request = $this->getRequest();
        if ($request->isPost()) {
        $table = $this->getDbTable();
        $select = $table->select();
        $select->where('name LIKE ?', $userName . '%');
        $rows = $table->fetchAll($select);

1 个答案:

答案 0 :(得分:0)

尝试更改此部分

$select->where('name LIKE ?', $userName . '%');

喜欢这个(但请不要忘记将表{tablename,anothertable}和列{name,surname,email}替换为您在项目中使用的那些)

    $select->from('tablename', 'name')
    ->where('tablename.name LIKE ?', $userName . '%')
    ->join('anothertable', 'anothertable.name = tablename.name', array('name', 'surname', 'email'))
    ->order("anothertable.name ASC");

- &gt;加入(&#39; anothertable &#39;,&#39; anothertable.name = tablename.name &#39; ,数组(&#39;名称&#39;,&#39;姓氏&#39;,&#39;电子邮件&#39;) - &gt;订单(&#34; anothertable.name ASC&#34;);

anothertable - 您要加入的表格的名称 anothertable.name = tablename.name - 将从适当的行中添加列的标准 array('name', 'surname', 'email') - 查询将添加的列名称 ->order("anothertable.name ASC") - 这将说明要排序的列

这里有更多信息http://framework.zend.com/manual/1.12/en/zend.db.select.html 希望这可以帮助你。