0

这将是我的最后一个问题。

结果我没有得到任何行,我不知道我做错了什么,另外我想返回多行,

就像在 mysql 中一样,我可以只使用这样的参数,这样我就可以返回多行,这将是一个有用的搜索查询,因为大多数搜索都没有准确地写入或很多。例如,我想搜索作者 john 的一本书,但 john 有 5 本书,如果我使用 first_row 它只会返回第一本书,并且我正在寻找的书可能会在第 2 或第 3 或第 4或第 5 行。

我是 ci 的新手,我阅读了一份指南,其中我可以使用 num_rows() 但我不确定如何使用它,因为我没有看到一个例子。我搜索过,但令我失望的是,我没有找到任何满足我需求的东西。

这是我的html:

<?php echo form_open('bookstore/booksearch'); ?>
                                <table>
                                    <tr>
                                        <td>
                                    <label for="searchid">Search:</label>
                                        </td>
                                        <td>
                                    <input type="text" size="15" name="searchval" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                    <label for="searchtype">Type:</label>
                                        </td>
                                        <td>
                                    <select name="searchtype">
                                        <option value="book_id">Id</option>
                                        <option value="book_author">Author</option>
                                        <option value="book_name">Title</option>
                                    </select>
                                        </td>
                                    </tr>
                                    <tr>
                                    <input type="submit" name="submit" value="Search" />
                                    </tr>
                                </table>
                                <?php echo form_close(); ?>

                            </ul>
                        </div>
                        <div>

<table cellpadding='5'>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Released Year</th>
<th>ISBN</th>

<?php
if(isset($books)) : foreach($books as $book) :  
?>      
        <tr>
        <td><?php echo $book['book_id'] ?></td>
        <td><?php echo $book['book_name'] ?></td>
        <td><?php echo $book['book_author'] ?></td>
        <td><?php echo $book['book_year'] ?></td>
        <td><?php echo $book['book_isbn'] ?></td>
        </tr>
<?php
    endforeach;
?>

<?php
else :
?>
<h5>No records</h5>
<?php
endif;
?>
</table>

控制器:

public function booksearch()
    {
    if($_POST['submit'])
        {
        $col= $this->input->post('searchtype');
        $val= $this->input->post('searchval');

        $data['book'] = $this->books_model->showbook($col,$val);

        }
        $this->load->view('showbooks',$data);
    }

和我的模型:

public function showbook($col, $searchid)
{
        $this->db->select()->from('books')->where(array($col=>$searchid));
        $query=$this->db->get();
        return $query->first_row('array');
}

我之前发布了一个问题,但使用类似的代码,我解决了它,因为我只错过了 name 属性,你不希望成为问题的小事情真的会浪费你的时间。我希望这次我没有错过任何东西,否则如果它发生两次,我会感到很遗憾。谢谢你的耐心!

4

1 回答 1

1

代替:

public function showbook($col, $searchid)
{
    $this->db->select()->from('books')->where(array($col=>$searchid));
    $query=$this->db->get();
    return $query->first_row('array');
}  

到:

public function showbook($col, $searchid)
{
    $this->db->select()->from('books')->where(array($col=>$searchid));
    $query=$this->db->get();
    return $query->result_array();
}
于 2013-08-06T14:29:36.023 回答