【问题标题】:how do i get the data from view in codeigniter我如何从codeigniter中的视图中获取数据
【发布时间】:2016-03-31 11:26:17
【问题描述】:

我在 codeigniter 方面做得不好,我还在学习这个。所以我需要你们的帮助。

我想从数据库中获取 id 等于下拉列表按钮的值的数据。所以这是我的代码。

这是我的控制器:controller.php

function getdataload(){
     $this->load->view('data',$data);
}

我真的不知道在控制器里放什么。

这是我的观点:view.php

<html>
<body>
<label for="member">Member</label>
            <select class="form-control" id="member" name="member" required onchange="showCustomer(this.value)">
              <option selected="" value="">--select--</option>
                 <?php foreach ($members as $row): ?>
              <option value="<?php echo $row->mem_id; ?>"><?php echo ucwords($row->mem_fname.' '.$row->mem_lname) ?></option>
               <?php endforeach ?>
            </select>
</body>
</html>
<script>
$('#member').on('change',function(){
    $.post('<?php echo base_url("transactions/getdataload")?>',
        {
            mem_id:$(this).val()
        }).done(function(res)
        {
        $('#select_member').text(res);
    });
});
</script>

这是从控制器调用的另一个视图 data.php

<?php

$q = intval($_GET['member']);

$con = mysqli_connect('localhost','root','','global89_point');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"global89_point");

$sql="SELECT * FROM loading_service WHERE member='".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>";
echo "<tr>";
echo " <th>";
echo "Member ID";
echo "</th>";

echo "</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['member'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);

?>

请帮帮我。

【问题讨论】:

  • 先使用合适的模态和控制器
  • 永远不要尝试在视图中执行数据库查询 look@codeigniter.com/user_guide/general/models.html 阅读文档并尝试了解如何将数据从模型传递到控制器以及从控制器传递到视图跨度>
  • 你必须做codeigniter手册中的教程。了解框架的基础知识,然后开始构建。

标签: php html database codeigniter


【解决方案1】:

如果您不使用模型和数据库逻辑写入控制器,则应从控制器调用数据库库,请参见:

public function result()
{
    $this->load->library('database')
    $data = array();

    $service_list = $this->db->query("SELECT * FROM loading_service WHERE member='".$q."'")->result_array();
    $data['service'] = $service_list;
    $this->load->view('result', $data);
}

在这里result 查看你得到$service 数组,你可以轻松地迭代它。

【讨论】:

    【解决方案2】:
         Our model in CodeIgniter will be placed in application/models/form_model.php 
    
           <?php
    
            class Form_model extends Model {
            function Formmodel() {
            // load the parent constructor
            parent::Model();
            }
            function submit_posted_data() {
            // db is initialized in the controller, to interact with the database. 
            $this->db->insert('loading_service ',$_POST);     
            }
            function get_all_data() {
            // again, we use the db to get the data from table ‘form’
            $data['result']=$this->db->get('loading_service');
            return $data['result'];
            }
            }
            ?>
    
    
        controller
    
        ss Form extends Controller {
        function Form(){
        // load Controller constructor
        parent::Controller();
        // load the model we will be using
        $this->load->model('form_model');
        // load the database and connect to MySQL
        $this->load->database();
        // load the needed helpers
        $this->load->helper(array('loading_service','url'));
        }
        //Display the posted entries
        function index() {
        $data['member']='Form Data';
        //use the model to get all entries
        $data['result'] = $this->form_model->get_all_data();
        // load 'forms_view' view
        $this->load->view('forms_view',$data);
        }          
        //Process the posted loading_service
        function submit() {
        //use the model to submit the posted data
        $this->form_model->submit_posted_data();
        redirect('loading_service');
        }
        }
        ?>
    
    you can refer this code..It will helpful for u...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2017-08-24
      • 1970-01-01
      相关资源
      最近更新 更多