munaz munaz - 7 months ago 126
SQL Question

Delete multiple selected rows in phalcon

I want to delete selected multiple rows or all rows on a single click.
But i cant figure it. how could i do this with my code? please edit my code for expected result.

Here is my jquery for select all rows

[Jquery]
function selectAll(status){
$('input[name=slId]').each(function(){
$(this).prop('checked', status);
});
}


How to get id's in to controller to execute delete process? My jquery does not sends any id, i tested with var_dump its shows NULL.

[Controller]

public function deleteAction()
{
if($this->request->isPost())
{
if($this->session->has('uname'))
{
$id = array();
$id = $this->request->getPost('id');
$data = Blogs::findByid($id);
if(!$data->delete())
{
echo('Unable to Delete');
}
}
}
}

[volt]

{{ form('blog/delete', 'enctype': 'multipart/form-data') }}
<table class="bloglist">
<thead>
<tr class="fbold">
<td>
{{check_field('checkbox','id':'sall','onclick':'selectAll(this.checked)')}} </td>
<td>Title</td>
<td>Author</td>
<td>Views</td>
<td>PublishedOn</td>
</tr>
</thead>
<tbody>
{%for all in ball %}
<tr class="zebra">
<td>{{check_field('slId', 'class':'slId','id':all.id)}}</td>
<td class="tal">{{all.btitle}}</td>
<td>{{all.bauthor}}</td>
<td>{{all.views}}</td>
<td>{{all.datetime}}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="6">{{submit_button('DELETE')}}</td>
</tr>
</tfoot>
</table>
{{end_form()}}

Answer

I just figure it like this and its working as expected!

[controller]
public function deleteBlogAction()
{
    if($this->request->isPost() == true)
    {
        if($this->session->has('uname'))
        {
            $ids = $this->request->getPost('item');                
            foreach($ids as $item)
            {
                $blogs = Blogs::findFirst($item);
#Erase Post Related Image                    
                $uploadPath = 'uploads/blogs/';
                $defaultImg = $uploadPath.'empty.png';
                $getImg = $uploadPath.$blogs->bimage;
                if($getImg == true AND $getImg != $defaultImg)
                {
                    if(@unlink($getImg) == false)
                    {
                        echo('Uploaded Image Cannot Delete');
                    }
                }
 #Erase Post Related Comments      
                $deleteC = Comments::findByentry_id($item)->delete();
#Erase Blog Posts                    
                $deleteB = Blogs::findFirst($item)->delete();
            }
            if($deleteC != false AND $deleteB != false)
            {
                $this->flashSession->success("The post &amp; related comments has been deleted.");
                return $this->response->redirect('blog/getBlog');
            }
            else
            {
                $this->flashSession->error("Sorry! We are unable to delete.");
                return $this->response->redirect('blog/getBlog');
            }
        }
        else
        {
            $this->flashSession->error("Unauthorised Access!");
            return $this->response->redirect('blog/getBlog');                
        }
    }
    else
    {
            $this->flashSession->error("Request May Not Posted.");
            return $this->response->redirect('blog/getBlog');            
    }
}


[volt]

    {{ form('blog/deleteBlog', 'enctype': 'multipart/form-data') }}
<table class="bloglist">
    <thead>
        <tr class="fbold">
            <td>{{check_field('item','class':'toggle-button')}}</td>
            <td>Title</td>
            <td>Author</td>
            <td>Views</td>
            <td>PublishedOn</td>
        </tr>
    </thead>
    <tbody>
{%for all in ball %}    
        <tr class="zebra">
            <td>{{check_field('item[]','value':all.id)}}</td>
            <td class="tal">{{all.btitle}}</td>
            <td>{{all.bauthor}}</td>
            <td>{{all.views}}</td>
            <td>{{all.datetime}}</td>
        </tr>       
{% endfor %}        
    </tbody>
    <tfoot>
        <tr>
            <td colspan="6">{{submit_button('DELETE')}}</td>
        </tr>   
    </tfoot>
</table>
{{end_form()}}