如何将Codeigniter数据库数组传递给javascript函数 [英] how to pass codeigniter DB array to javascript function

查看:77
本文介绍了如何将Codeigniter数据库数组传递给javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中有此数据库数组,我想将其传递给javascript函数。

i have this database array in codeigniter that i want to pass it to the javascript function. is it possible?

模型:

public function show_employment_skills($P1)
{
    $this->db->select('*');
    $this->db->from('employee_skills');
    $this->db->where('emp_id', $P1);
    $this->db->order_by('ID', 'desc');

    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $data[] = array(
                    'id' => $row->ID,
                    'emp_id' => $row->emp_id,
                    'skills' => $row->skills


                );
        }
    }
    else
    {
        $data = "";
    }
    return $data;
}

并在获取$ data之后(这是一个数据库数组,对吧?)

and after getting the $data (that is a DB array, right?)

这是我的控制器

 public function swoosh_employment_skills()
{
    $P1 = $this->session->userdata('id');

    $set['record_skills'] = $this->emp->show_employment_skills($P1);
    $this->load->view('swoosh_template/employee/employmentskills',$set);

}

$ set ['record_skills']是数组来的

the $set['record_skills'] is the array came from the database..

现在这是我的观点:

<?php if ($record_skills != "") } ?>
<table class="table table-bordered">
<tr>
    <th>Skills</th>
<th style='width:50px;'>Alert it!</th>
</tr>
<?php foreach ($record_skills as $row): ?>
<tr>   
    <td class="skills"><?php echo $row['skills'];?></td>
    <td><img src=alert_it.png onclick="show(db array that i should put. but how?);"</td>
</tr>
</table>

这是JavaScript

here is the javascript

function show(array){
 //in here i should alert all the values of arrays. ..
}


推荐答案

您可以使用<为此,请使用href = http://php.net/manual/zh/function.json-encode.php rel = nofollow> json_encode(),例如:

<img src=alert_it.png onclick="show(<?php echo json_encode($your_array); ?>);" />

和js

function show(arr){
 console.log(arr); //check the array format and loop
}

这篇关于如何将Codeigniter数据库数组传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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