使用Pagination的Codeigniter中的复选框删除

时间:2013-06-26 12:20:17

标签: php mysql codeigniter

我正在尝试在我的应用程序中的选定记录上应用删除功能。问题是记录显示在分页中,即3,4行,我能够从第一页删除记录,但当我移动到第二页并选择几个记录时,删除记录按钮不起作用。任何adivise都会有所帮助。 我正在寻找回复 这是我的列表代码

public function index($offset=0)
{
    if ( $this->session->userdata('u_name') == FALSE ) 
    {
         $data['page_title']="Admin Login";
         redirect('admin/login',"refresh",$data);
    }   
    $this->load->model('candidate_model');
    $limit=4;       
    $results=$this->candidate_model->get_all_Candidate($limit,$offset);
    $data['candidates']=$results['rows'];
    $offset = $this->uri->segment(3, 0);

    $data['num_results']=$results['num_rows'];
    $this->load->library('pagination');

    $config=array();
    $config['first_url'] = 'candidateList/index';
    $config['base_url']=site_url('candidateList/index');
    $config['total_rows']=$data['num_results'];

    $config['per_page']=$limit;
    $config['uri_segment']=3;
    $config['first_link'] = '>';
    $config['first_tag_open'] = '<div>';
    $config['first_tag_close'] = '</div>';
    $this->pagination->initialize($config);

    $data['page_title']="Candidate List";



    $this->load->view('manageCandidate',$data);
}

我的删除功能代码是

if($this->input->post('Delete')=='Delete')
        {
            for($i=0;$i<count($_POST['checkbox']);$i++)
            { 

                $candidate_id = $_POST['checkbox'][$i]; 
                $this->candidate_model->deleteCandidate($candidate_id);
                $this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');

            }

            $data['page_title']="Candidate List";   
            redirect('candidateList',$data);


        }

删除功能在这里:

public function candidate()
    {

        if ( $this->session->userdata('u_name') == FALSE ) 
        {
             $data['page_title']="Admin Login";
             redirect('admin/login',"refresh",$data);
        }

        $checkList=$this->input->post('checkbox');
        $this->load->model('candidate_model');  

        if($checkList!=NULL)
        {

            if($this->input->post('Delete')=='Delete')
            {
                for($i=0;$i<count($_POST['checkbox']);$i++)
                { 

                    $candidate_id = $_POST['checkbox'][$i]; 
                    $this->candidate_model->deleteCandidate($candidate_id);
                    $this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');

                }

                $data['page_title']="Candidate List";   
                redirect('candidateList',$data);


            }
            else if($this->input->post('Email')=='Email')
            {
                for($i=0;$i<count($_POST['checkbox']);$i++)
                { 

                    $admin_id = $_POST['checkbox'][$i];
                    $email_to[$i] = $_POST['checkbox'][$i];
                    $data['email_to'] = $email_to;

                }

                $data['page_title']='Send Mail';
                $this->load->view('admin/sendmail4',$data);
            }
        }
        else
        {
            $this->session->set_flashdata('deleteSelect','Please Select at-least one Candidate.');
            $data['page_title']="Candidate List";
            redirect('candidateList',$data);
        }



    }

ManageCandidate代码:

    <?php
$this->load->view('includes/template3');
?>

<?php
$this->load->view('includes/superAdminMenu');
?>


<div class="dashboard">
    <a href="<?php echo base_url(); ?>index.php/admin/addCandidate" class="addBtn">Add Candidate</a>



    <?php if($this->session->flashdata('deleteCandidate')) : ?>
    <p class="successMsg"><?php echo $this->session->flashdata('deleteCandidate')?></p>
<?php endif; ?>

    <?php if($this->session->flashdata('editCandidate')) : ?>
        <p class="successMsg"><?php echo $this->session->flashdata('editCandidate')?></p>
    <?php endif; ?>

<?php if($this->session->flashdata('deleteSelected')) : ?>
    <p class="successMsg"><?php echo $this->session->flashdata('deleteSelected')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('deleteSelect')) : ?>
    <p class="noRows"><?php echo $this->session->flashdata('deleteSelect')?></p>
<?php endif; ?> 
<?php if($this->session->flashdata('msgSent')) : ?>
    <p class="successMsg"><?php echo $this->session->flashdata('msgSent')?></p>
<?php endif; ?> 

    <?php 

    if($num_results==0)
    { ?>
    <p class="noRows"><?php echo "You have not added any Candidate.";?></p>
    <?php } 
    else
        if($num_results>0)
        { ?>

    <form action="candidateList/candidate" method="post" name="sendMail" class="addformClass" id="candidateList1">


    <!--        Script by hscripts.com          -->
<!--        copyright of HIOX INDIA         -->
<!-- Free javascripts @ http://www.hscripts.com -->
<script type="text/javascript">
checked=false;
function checkedAll (candidateList1) {
    var aa= document.getElementById('candidateList1');
     if (checked == false)
          {
           checked = true
          }
        else
          {
          checked = false
          }
    for (var i =0; i < aa.elements.length; i++) 
    {
     aa.elements[i].checked = checked;
    }
      }
</script>
<!-- Script by hscripts.com -->

    <?php if(isset($candidates)) { ?>
    <div class="candidateTable">
       <div class="candidateRowHeading">
            <div class="candidateHeadingChkBox">       
                <h4><input type='checkbox' name='checkall' onclick='checkedAll(candidateList1);'>

                </h4>
            </div><!--END candidateHeadingChkBox-->         
            <div class="candidateColHeading1">     
                <h4>Candidate Name</h4>
            </div><!--END candidateColHeading1-->

            <div class="candidateColHeading3">
                    <h4>Email</h4>
            </div><!--END candidateColHeading3-->
            <div class="candidateColHeading4" style="display:none">
                <h4>Status</h4>
            </div><!--END candidateColHeading4-->
            <div class="candidateColHeading6">
                <h4>Recruiter Assigned</h4>
            </div><!--END candidateColHeading6-->
            <div class="candidateColHeading5">
                <h4>Action</h4>
            </div><!--END candidateColHeading5-->
        </div><!--END candidateRowHeading-->


            <?php $count=0; ?>
        <?php foreach ($candidates as $candidate) { ?>
        <div class="candidateRowData">
            <div class="candidateColChkBox">
                <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $candidate->candidate_id; ?>">

            </div><!--END candidateColChkBox-->         
            <div class="candidateColData1">

            <?php echo anchor('admin/viewCandidate/'.$candidate->candidate_id, $candidate->first_name." ".$candidate->last_name); ?>

            </div><!--END candidateColData1-->

            <div class="candidateColData3">
                <h4><?php echo $candidate->email; ?></h4>
            </div><!--END candidateColData3-->

            <div class="candidateColData4"  style="display:none">
                <h4><?php echo $candidate->lead_status; ?></h4>
            </div><!--END candidateColData4-->
            <div class="candidateColData6">
                <h4>
                    <?php if($candidate->recruiter_id_fk!=0) { ?>
                        <?php echo get_recruiterFirstName($candidate->recruiter_id_fk); ?>
                        <?php echo get_recruiterLastName($candidate->recruiter_id_fk); ?>
                        <?php echo " (<strong>"; ?>
                        <?php echo get_recruiterLogin($candidate->recruiter_id_fk); ?>
                        <?php echo "</strong>) "; ?>
                    <?php }
                    else if($candidate->recruiter_id_fk==0) { ?>
                        <?php echo "Not Assigned"; ?>
                    <?php } ?>

                </h4>
            </div><!--END candidateColData6-->
            <div class="candidateColData5">
                <?php echo anchor('admin/editCandidate/'.$candidate->candidate_id,'Edit'); ?>
                <?php echo anchor('admin/deleteCandidate/'.$candidate->candidate_id,'Delete'); ?>
            </div><!--END candidateColData5-->
        </div><!--END candidateRowData-->

        <?php $count++; } ?>
         </div><!--END candidateTableCom-->
    <?php } ?>

    <?php } ?>

    <?php if(isset($candidates)) { ?>

    <div id="pageNum">
        <?php echo $this->pagination->create_links(); ?>
    </div>
    <?php } ?>


    <?php 
    if($num_results>0) {

    echo '<input type="submit" name="Delete" class="emailAllBtn" value="Delete" />'; 
    echo '<input type="submit" name="Email" class="emailAllBtn" value="Email" />'; 
    echo "</form>";
    } ?>


</div>

<?php
$this->load->view('includes/footer2');
?>
<?php
function get_recruiterFirstName($id)
{
    $CI =& get_instance();
    $mod = $CI->load->model('recruiter_model');
    $count = $CI->recruiter_model->get_recruiterFirstName($id);
    return $count;
}
?>

<?php
function get_recruiterLastName($id)
{
    $CI =& get_instance();
    $mod = $CI->load->model('recruiter_model');
    $count = $CI->recruiter_model->get_recruiterLastName($id);
    return $count;
}
?>

<?php
function get_recruiterLogin($id)
{
    $CI =& get_instance();
    $mod = $CI->load->model('recruiter_model');
    $count = $CI->recruiter_model->get_recruiterLogin($id);
    return $count;
}
?>

1 个答案:

答案 0 :(得分:0)

您的分页代码偏移值存在小错误

public function index($offset=0)
{
    if ( $this->session->userdata('u_name') == FALSE ) 
    {
         $data['page_title']="Admin Login";
         redirect('admin/login',"refresh",$data);
    }

    $this->load->model('candidate_model');
    $limit=4;     

    //whenever you are calling this pagination  offset value you need to fetch it from URL
    $offset_url = $offset = $this->uri->segment(3, 0);

    //if it's not exist take the default value
    $offset = is_numeric($offset_url)?$offset_url:$offset;

    $results=$this->candidate_model->get_all_Candidate($limit,$offset);
    $data['candidates']=$results['rows'];


    ..............

}