如何从codeigniter中的视图中获取数据

时间:2016-03-31 11:26:17

标签: php html database codeigniter

我在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);

?>

请帮助我这些家伙。

2 个答案:

答案 0 :(得分:0)

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

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数组,您可以轻松地对其进行迭代。

答案 1 :(得分:0)

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