paging concept in symfony 2.3

Controller code

 

namespace Admin\ManageuserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Admin\ManageuserBundle\Entity\User;
use Admin\ManageuserBundle\Form\UserType;
use Symfony\Bundle\AsseticBundle\Factory\Worker\UseControllerWorker;
use Symfony\Component\Security\Tests\Core\User\UserTest;

class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$session =$this->getRequest()->getSession();
$sessionid = $session->get(‘adminlogin’);

$page=$request->get(‘page’);
$count_per_page=4;
$gettotalcount =$this->getDataUser();
$totalpage=ceil($gettotalcount/$count_per_page);

if(!is_numeric($page))
{
$page=1;
}
else
{
$page=floor($page);
}
if($gettotalcount<=$count_per_page)
{
$page=1;
}
if(($page * $count_per_page)>$gettotalcount){
$page=$totalpage;
}
$offset=0;
if($page>1)
{
$offset=$count_per_page * ($page-1);
}
$em=$this->getDoctrine()->getEntityManager();
$ctryQuery=$em->createQueryBuilder()
->select(‘c’)
->from(‘ProfileLoginBundle:User’,’c’)

->setFirstResult($offset)
->setMaxResults($count_per_page);
$ctrFinalQuery=$ctryQuery->getQuery();
$catquery=$ctrFinalQuery->getArrayResult();

return $this->render(‘AdminManageuserBundle:Default:index.html.twig’,array(‘adminlogin’=>$sessionid,’userlist’=>$catquery,’totalpage’=>$totalpage,’currentpage’=>$page));

}

public function getDataUser()
{
$em=$this->getDoctrine()->getEntityManager();
$ctryQueryVal=$em->createQueryBuilder()
->select(‘count(User.id)’)
->from(‘ProfileLoginBundle:User’,’User’);
$finalquery=$ctryQueryVal->getQuery();
$total=$finalquery->getSingleScalarResult();
return $total;

}

}

——————————————

View code

 

{% extends ‘::base.html.twig’ %}
{% block body -%}
<div>
<table width=”100%” valign=”top” cellspacing=”0″>
<tr>
<td>
{{msg | default(“”) }}
</td>
</tr>
<tr bgcolor=”blue;”>
<td>
ID
</td>
<td>
Name
</td>

<td>
Email address
</td>

<td>
Status
</td>
<td>Action</td>
</tr>
{% for users in userlist%}
<tr>

<td>
{{users.id}}
</td>
<td>
{{users.firstname}}
</td>
<td>
{{users.lastname}}
</td>
<td>
{{users.email}}
</td>

<td>
<a href=”{{ path(‘admin_manageuser_edit’, { ‘editid’: users.id }) }}”>Edit</a> |
<a href=”{{ path(‘admin_manageuser_delete’, { ‘deleteid’: users.id }) }}”>Delete</a>

</td>
</tr>

{%endfor%}

<tr>
{%if totalpage >0 %}
<td colspan=”5″>
<table align=”center”>
<tr>

{% for i in 1.. totalpage%}
{%if loop.first %}
<td><a href=”{{path(‘admin_manageuser_homepage’,{ ‘page’: currentpage-1 })}}”>Previous</a></td>
{%endif%}
<td {%if currentpage==loop.index%}>
<a href=”{{path(‘admin_manageuser_homepage’,{ ‘page’: loop.index })}}”>{{loop.index}}</a></td>

{%if loop.last%}
<td><a href=”{{path(‘admin_manageuser_homepage’,{ ‘page’: currentpage+1 })}}”>Next</a></td>
{%endif%}
{%endfor%}

</tr>
</table>
</td>
{%endif%}
</tr>

</table>

{% endblock %}

———————————————

 

routing

admin_manageuser_homepage:
pattern:  /admin/manageuser/
defaults: { _controller: AdminManageuserBundle:Default:index }

 

Thanks

Vinod K Maurya (OSSCube)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s