【问题标题】:CodeIgniter search formCodeIgniter 搜索表单
【发布时间】:2010-12-16 13:50:48
【问题描述】:

我目前正在学习 CodeIgniter,我希望开发一个由 2 个表单组成的简单示例,我们将它们称为表单 a 和表单 b。表单 a 有一个名为“LastName”的编辑字段,表单 b 将显示与“LastName”中的值匹配的表中所有名称的列表,例如

select first_name, last_name from my_table where last_name = :LastName

我需要这个例子来理解将变量从一个表单和控制器传递到另一个的机制。我猜这是使用 $_POST 之类的方法,但网络上没有任何示例看起来很清楚。

【问题讨论】:

  • 表格会在同一页上吗?如果是这样 - 我只能考虑 AJAX 这样做的方式。如果您想在 1 个页面上显示 1 个表单,然后用户单击按钮,结果将显示在另一个页面上 - 您可以只使用 PHP。请。指定你希望它如何工作。
  • 表格和结果将在不同的页面上;它必须使用 CodeIgniter
  • 那你问的是如何使用 CI 做表单,这在他们自己的文档中很好地涵盖了。
  • 我很抱歉 DampeS8N 但这不是一个非常有用的答案

标签: php forms codeigniter search


【解决方案1】:

所以你会有一个表格......

<form action="/controller/name/" method="post">
    <p>Last Name: <input type="text" name="LastName" /></p>
    <p><input type="submit" value="Submit"/></p>
</form>

然后在你的控制器中(假设你已经连接到数据库):

function index() {

    // This is the last name from the form
    $LastName = $this->input->post('LastName');

    // Create the query
    $sql = "SELECT * FROM users WHERE last_name = ?";

    // Execute it, replacing the ? with the last name from the form
    $query = $this->db->query($sql, array($LastName));

    // Show results
    foreach ($query->result() as $row) {
       echo $row->first_name . "<br />";
       echo $row->last_name;
    }

}

【讨论】:

  • 谢谢火;至少,我需要一个还是两个控制器?
  • 这取决于你想要做什么,控制器实际上是一个页面,重要的是你理解术语从头开始阅读 CI 用户指南很容易理解
  • 我想我需要 2 个控制器(用于 2 个页面),一个用于输入表单(页面),另一个用于显示页面。在您的示例中,两者合并在一起;将它们分开的最佳方法是什么?
  • 是的,那会很好用!请点击勾选将此标记为预期答案:)
【解决方案2】:

您的视图文件夹:application/view/form_a.php、application/view/forma_b.php

您的控制器文件夹:application/controller/controller_name.php

你的模型文件夹:application/model/related_model_name.php

你的 controller_name.php 文件:

 class Controller_name extends Controller
{

function index()
{
  $this->load->view('form_a'); //this loads the form  
}

function results()
{
  $name= $this->post->input('last_name');
  $this->load->model('related_model_name'); //this is the model to fetch the data
  $data['names']= $this->related_model_name->searchByLastName($name);
  if(!empty($data))
  $this->load->view('form_b', $data);
}


}//eoc

您的related_model_name.php 文件

class Related_model_name extends Model

{
function __construct()
{
 parent::Model(); 
}

function searchByLastName($name)

{
 $query = $this->db->get_where('table_name', array('last_name'=>$name)); 
 if($query->nu_rows() > 0)
 return $query->results(); 
}//eof

}//eoc

您的 form_b.php 查看文件

执行 print_r($data) ,这应该让您了解如何显示数据。 可能是这样的

foreach ($names as $name)
{
 echo $name->name; 
}

【讨论】:

    【解决方案3】:

    我意识到这个线程很旧,但我是 CodeIgniter 的新手,并且一直在处理类似的挑战。我的挑战是创建一个搜索表单,在特定邮政编码中查找种植者。这是我的解决方案。它比我预期的要简单,并且可能对其他人有所帮助。

    此代码假定您已连接到数据库并拥有标准的 MVC CI 应用程序等。

    我在模型和视图中处理大部分任务,但我的控制器中确实有这个方法:

    public function result()
        {
            $zipcode = $this->input->post('zip_code');
            $query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
            return $query->result_array();  
        } 
    

    在我的模型中,我使用了以下方法:

    public function result()
        {
            $zipcode = $this->input->post('zip_code');
            $query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
            return $query->result_array();  
        }
    

    我有三个视图——一个页面(位于views/pages/search.php)和两个小部件——一个用于搜索表单,一个用于搜索结果(位于views/widgets/result)。

    我在结果显示的同一页面上有搜索结果表单。但是,每个部分都包含在其自己的视图文件中,我已将其放置在视图/小部件中。该部分在页面视图中的代码是:

        <div class="search" style="margin-top:0px;">
                <?php 
                    $this->load->view('widgets/search');
                ?>
            </div>
        </div>
        <div id="results">
            <div id="grower-info">
                <?php
                    $this->load->view('widgets/result');            
                ?>
            </div>
        </div>
    

    搜索表单小部件是:

    <form action="search-results" method="post">        
        <input type="text" maxlength="10" name="zip_code" value="zip code" size="10">
        <input type="submit" name="submit" value="SEARCH">
    </form>
    

    搜索结果小部件是:

    <?php
    
    $results = $this->pages_model->result();
     foreach ($results as $result)
     {
        echo '<h4>'.$result['company'].'</h4>';
        echo $result['address_1'] . ' ' . $result['address_2'].'<br>';
        echo $result['city'].', ' . $result['state'] . '  ' . $result['zip'].'<br>';
        echo 'Phone:  ' . $result['office_phone'].'<br>';
        echo 'Fax:  ' . $result['office_fax'].'<br>';
        echo 'Website:  <a href="'.$result['website'].'" target="_blank">' . $result['website'].'</a><br>';
        echo '<br>';
        echo '<hr>';    
    }
    
    if (count($results) < 1) {
        echo 'No results found. Please try your search again, or try <a href="another-search">another search</a>.';
    }
    ?>
    

    希望对大家有所帮助!

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 2017-04-18
      • 2010-09-18
      • 2011-08-14
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2012-05-15
      相关资源
      最近更新 更多