0

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

MODEL:

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

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

here's my controller

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

}

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

now here's my view:

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

here is the javascript

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

2 Answers 2

2

you could use json_encode() for that, like:

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

and in js

function show(arr){
 console.log(arr); //check the array format and loop
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

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

Then you pass the string to the function in JS, and then in the JS you can decode that string as JSON:

function show(arr){
    var _json = $.parseJSON( arr );
    console.log( _json ); //check the array format and loop
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.