Nikita
Nikita

Reputation: 31

Dynamic table creation in cakephp

In my module, I need a functionality which is similar to auto complete. What I require is to create a table whenever I type a name. Say for example if I type John then a table should appear having all 'John's in the database with their other attributes like email ID, specialization, address, etc. Then when I choose a particular 'john'(by clicking on the name/check box/any other way), his info should get filled in my form automatically. I am able to extract the details of all 'John's using json (I have printed in the alert box) but two problems that I am facing are: 1.) I am not able to create a table corresponding to 'John' 2.) I don't know how to pass the data to my form when the required row is being clicked by the user.

Also when I extract data, only the data for the last entry in the table (i.e. last ''john'') is displayed; i.e only last ''john'' is being saved. Although the for loop shows all ''john''s.

I am pasting my controller and view code for your reference.

Please suggest something.

//Controller:
public function getmasterdata($id=NULL)
{
    $this->layout = 'ajax';
    $this->autoRender = false;

    $q=$this->request->data['nns'];
    $details=array();
    $this->loadModel('Approved_panel');
    $conditions = 'Approved_panel.name LIKE "%'.$q.'%"'; 

    $countt=$this->Approved_panel->find('count',array('conditions'=>$conditions));
    $masterdata=$this->Approved_panel->find('all',array('conditions'=>$conditions));
    //print_r ($masterdata);

    foreach($masterdata as $m)
    {
        $details=$m['Approved_panel'];
        print_r($details);

    }
    //print_r($details);
    $data='';
    foreach($details as $key=>$value)
    {
        $data.=$key.'//'.$value.'&&&';
    }
    print_r($data);
    //echo json_encode($details);
    exit;
}

`

//View:
 $("#name"+lastChar).on('keyup',function ()
 {
        $('#master_details').show();
        var namess=$('#name'+lastChar).val();
        //alert(namess);
        $.ajax({  
        cache: false,
        dataType: "html",
        type: "POST",  
        evalScripts: true,
        url: '<?php echo         Router::url(array('controller'=>'Panels','action'=>'getmasterdata'));?>',
        data: ({nns:namess}),  
        success: function(result){ 
                alert(result);
                $('#detailss').val(result);
           }
        }); 
    });

`

//This is the div in which data should be displayed. These are the headers. I don’t know how to get data into this.

 <div id="master_details">
 <h2 align="center">Previous Panel Member's Details</h2>
 <table>
 <thead>
<tr>
    <th> Name</th>
    <th> Designation</th>
    <th> Specialization</th>
    <th> University</th>
    <th> College</th>
    <th> Address</th>
    <th> Phone</th>
    <th> Email</th>
    <th> Papercode</th>
    <th> Action</th>
   </tr>
 </thead>
 </table>
</div>

Upvotes: 0

Views: 439

Answers (2)

earnest
earnest

Reputation: 261

Perhaps is the way your condition statement is written. What does the echo $sql says. You could also run the statement in phpmyadmin. Try this:

$masterdata=$this->Approved_panel->find('all', ['conditions'=>[Approved_panel.name LIKE' =>  "%'.$q.'%"]];

Upvotes: 0

earnest
earnest

Reputation: 261

Please check if $q is returning anything echo $q;

or do pr($this->request->data) // to get a print of the array data

Upvotes: 0

Related Questions