    SET usertotalquantity = @usertotalquantity;

--  ==========================================================================================================================

    if myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   2    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    3    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    4    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto <= 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   5    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     6   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid <= 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   7    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid <= 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   8    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) ASC LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.id desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userid' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by a.userid desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid <= 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;


    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname = '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail = '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'userfullname' && reportto > 0 && roleid > 0 && departmentid > 0 && userid > 0 && searchuserfullname != '' && searchemail != '' then 
       set  @querypart7 = "  order by concat(a.firstname, ' ', a.lastname) desc LIMIT ? OFFSET ? ";
       SET @sqlv = concat(@querypart_result1, @querypart2, @querypart3, @querypart4, @querypart5 ,@querypart6, @querypart7);
       PREPARE stmt2 FROM @sqlv;
       EXECUTE stmt2 USING @userstatus, @userstatus2, @canreadinadminpage, @canreadinadminpage2, @cancreateinadminpage, @cancreateinadminpage2, @caneditinadminpage, @caneditinadminpage2, @canactivateinadminpage, @canactivateinadminpage2, @candeactivateinadminpage, @candeactivateinadminpage2, @userid, @searchuserfullname, @searchemail, @reportto, @roleid, @departmentid, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getCurrencyList`$$
CREATE PROCEDURE `wp_expenseszyx987_getCurrencyList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN currencystatus int,
   OUT currencytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @currencystatus = 0;
   set @currencystatus2 = 1;
   if currencystatus = -1 then set @currencystatus = 0; set @currencystatus2 = 1; end if;
   if currencystatus = 0 then set @currencystatus = 0; set @currencystatus2 = 0; end if;
   if currencystatus = 1 then set @currencystatus = 1; set @currencystatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @currencytotalquantity from ', ' wp_expenseszyx987_currency WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @currencystatus, @currencystatus2;
       DEALLOCATE PREPARE stmt;    
            
    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @currencytotalquantity from ', ' wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET currencytotalquantity = @currencytotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
   
    
    --   ---------------------------------------------
    
    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    
    
     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    
    
    --   ---------------------------------------------
    
    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    

     
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_currency WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_currency WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
DROP PROCEDURE IF EXISTS `wp_expenseszyx987_getTaxList`$$
CREATE PROCEDURE `wp_expenseszyx987_getTaxList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN taxstatus int,
   OUT taxtotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @taxstatus = 0;
   set @taxstatus2 = 1;
   if taxstatus = -1 then set @taxstatus = 0; set @taxstatus2 = 1; end if;
   if taxstatus = 0 then set @taxstatus = 0; set @taxstatus2 = 0; end if;
   if taxstatus = 1 then set @taxstatus = 1; set @taxstatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @taxtotalquantity from ', ' wp_expenseszyx987_tax WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @taxstatus, @taxstatus2;
       DEALLOCATE PREPARE stmt;    
           
    elseif searchwords != '' then SET @statement = concat( 'Select count(*) into @taxtotalquantity from ', ' wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET taxtotalquantity = @taxtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    
    
    --   ---------------------------------------------
    
    elseif myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
   
    
     --   ---------------------------------------------

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    elseif myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
   
    
    --   ---------------------------------------------
    
    elseif myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    

     
    --   ---------------------------------------------

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_expenseszyx987_tax WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
      PREPARE stmt2 FROM @sqlv;
      EXECUTE stmt2 USING @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;

    elseif myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_expenseszyx987_tax WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @taxstatus, @taxstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
DELIMITER ;
--  -------------------------------------------
