搜索,$ _POST变量 [英] Searching, $_POST variable

查看:108
本文介绍了搜索,$ _POST变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有这个代码块用于输入搜索。

I have this code block in html for inputting the search.

<?php echo form_open('bookstore/_searchbook'); ?>
<table>
    <tr>
        <td>
    <label for="searchid">Search:</label>
        </td>
        <td>
    <input type="text" size="15" name="searchid" />
        </td>
    </tr>
    <tr>
        <td>
    <label for="searchtype">Type:</label>
        </td>
        <td>
    <select>
        <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" value="Search" />
    </tr>
</table>
<?php echo form_close(); ?>

我在控制器上有这个,

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

    $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');
}

进一步了解我的观点,

我有这个打印我的搜索结果。

I have this to print the results of my search.

<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>

它不返回任何东西,所以我看到的都是没有记录。

It returns nothing so all i see is no records. Someone direct me to the thing i did wrong.

推荐答案


  • 您的表单的动作(视图) code> _searchbook ,而您的控制器函数 booksearch

  • c $ c> $ data ['book'] 在控制器函数中使用 name 属性

  • <而在你的视图中(在foreach循环中),你引用该变量为 $ books
  • 模型: ;例如 $ this-> db-> select('*') - > from('books') - >其中(array($ col => $ searchid)); c $ c>而不是 $ this-> db-> select()

  • c $ c> return $ query-> result_array(); 而不是 $ query-> first_row('array')

    • Your form's action (view) is _searchbook while your controller function is booksearch
    • Your submit input needs a name attribute
    • in the controller function you have $data['book'] while in your view (in the foreach loop) you reference that variable as $books
    • Model: you need to select at least something; e.g. $this->db->select('*')->from('books')->where(array($col=>$searchid)); instead of $this->db->select()
    • Model: I think you need return $query->result_array(); instead of $query->first_row('array')
    • 这篇关于搜索,$ _POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆