Codeigniter:登录增量对象后刷新主页

时间:2016-01-11 23:49:03

标签: php codeigniter login

我的网络应用程序中有一件奇怪的事情。我有一个登录表单。当我登录时,我得到一个带有字段的用户页面。该字段包含我帐户中正在运行的项目数量。它只是一个带有结果等的SQL查询。

当我登录时,我得到了我需要的数据..因为它只是一个新用户。我那个时刻的网址是: http://www.xxxxx.be/yyyyyy/index.php/User/login

刷新页面时。我的项目数量达到+1。所以现在1 .. 我认为它是关于参数等的东西。

这是我登录的控制器功能:

public function login() {
    $loggedin = $this->session->userdata('user_id');
    if (!$loggedin) {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('user_mail', 'user mail', 'required');
        $this->form_validation->set_rules('user_password', 'user password', 'required');

        if ($this->form_validation->run() == false) {
            $data = new stdClass();
            $data->error = 'Check your user and password';
            $this->load->view('dashboard_login', $data);
        } else {
            $usermail = $this->input->post('user_mail');
            $password = $this->input->post('user_password');

            if ($this->user_model->resolve_user_login($usermail, $password)) {

                $user_id = $this->user_model->get_user_id_from_mail($usermail);
                $user = $this->user_model->get_user($user_id);

                $this->session->set_userdata('user_id', $user_id);
                $this->session->set_userdata('user_name', (string) $user->user_name);
                $this->session->set_userdata('user_gsm', (string) $user->user_gsm);
                $this->session->set_userdata('user_address', (string) $user->user_address);
                $this->session->set_userdata('user_city', (string) $user->user_city);
                $this->session->set_userdata('userlevel', $this->user_model->get_user_level((int) $user->user_id));
                $this->session->set_userdata('user_mail', $usermail);

                $data['userdata'] = $this->session->userdata;

                if ($this->session->userdata('userlevel') == "3") {
                    $data['employeetotal'] = $this->user_model->get_amount_employees();
                    $data['customertotal'] = $this->user_model->get_amount_customers();
                    $data['projectstotal'] = $this->user_model->get_amount_projects();
                } else {
                    $data['projectstotal'] = $this->user_model->get_amount_projects_from_user($this->session->userdata('user_id'));
                }

                $data['userdata'] = $this->session->userdata;
                $this->load->view('header', $data);
                $this->load->view('dashboard_index', $data);
                $this->load->view('wrapper', $data);
            } else {
                $data = new stdClass();
                // login failed
                $data->error = 'Wrong username or password.';
                // send error to the view
                $this->load->view('dashboard_login', $data);
            }
        }
    } else {
        $data['userdata'] = $this->session->userdata;
        $data['employeetotal'] = $this->user_model->get_amount_employees();
        $data['customertotal'] = $this->user_model->get_amount_customers();
        $data['projectstotal'] = $this->user_model->get_amount_projects();
        $this->load->view('header', $data);
        $this->load->view('dashboard_index', $data);
        $this->load->view('wrapper', $data);
    }
}

特定用户具有userlevel 1.因此执行模型功能:

$data['projectstotal'] = $this->user_model->get_amount_projects_from_user($this->session->userdata('user_id'));

这是该函数的代码:

function get_amount_projects_from_user($userid) {
    $sql = "select count(project_id) as aantal from tbl_user_project where user_id = $userid";
    $query = $this->db->query($sql);
    $result = $query->result();
    return $result;
}

索引页面如下:

<?php if ($userdata['userlevel'] == "3") { ?> 
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                <div class="row">
                    <div class="col-lg-3 col-xs-6">
                        <!-- small box -->
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?php echo $customertotal[0]->aantal; ?></h3>
                                <p>Customers</p>
                            </div>
                            <div class="icon">
                                <a href="<?= site_url('User/redirect_register') ?>"<i class="ion ion-person-add" ></i></a>
                            </div>
                            <a href="<?= site_url('User/redirect_register') ?>" class="small-box-footer">Add<i class="fa fa-arrow-circle-right"></i></a>
                        </div>
                    </div>
                    <div class="col-lg-3 col-xs-6">
                        <!-- small box -->
                        <div class="small-box bg-aqua">
                            <div class="inner">
                                <h3><?php echo $projectstotal[0]->aantal; ?></h3>
                                <p>Projects</p>
                            </div>
                            <div class="icon">
                                <i class="ion ion-bag"></i>
                            </div>
                            <a href="<?= site_url('Project/redirect_projects/' . $userdata['user_id']) ?>" class="small-box-footer">More info<i class="fa fa-arrow-circle-right"></i></a>
                        </div>
                    </div>
                    <div class="col-lg-3 col-xs-6">
                        <!-- small box -->
                        <div class="small-box bg-green">
                            <div class="inner">
                                <h3><?php echo $employeetotal[0]->aantal; ?></h3>
                                <p>Employees</p>
                            </div>
                            <div class="icon">
                                <i class="ion ion-stats-bars"></i>
                            </div>
                            <a href="<?= site_url('User/redirect_register_employee') ?>" class="small-box-footer">Add<i class="fa fa-arrow-circle-right"></i></a>
                        </div>
                    </div><!-- ./col -->
                    <div class="col-lg-3 col-xs-6">
                        <!-- small box -->
                        <div class="small-box bg-red">
                            <div class="inner">
                                <h3>65</h3>
                                <p>Unique Visitors</p>
                            </div>
                            <div class="icon">
                                <i class="ion ion-pie-graph"></i>
                            </div>
                            <a href="#" class="small-box-footer">
                                More info <i class="fa fa-arrow-circle-right"></i>
                            </a>
                        </div>
                    </div><!-- ./col -->
                </div>
                <div class="row">
                    <!-- /.col -->
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        <div class="info-box bg-yellow">
                            <span class="info-box-icon"><i class="fa fa-calendar"></i></span>
                            <div class="info-box-content">
                                <span class="info-box-text">Events</span>
                                <span class="info-box-number">41,410</span>
                                <div class="progress">
                                    <div class="progress-bar" style="width: 70%"></div>
                                </div>
                                <span class="progress-description">
                                    70% Increase in 30 Days
                                </span>
                            </div><!-- /.info-box-content -->
                        </div><!-- /.info-box -->
                    </div><!-- /.col -->
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        <div class="info-box bg-aqua">
                            <span class="info-box-icon"><i class="fa fa-bookmark-o"></i></span>
                            <div class="info-box-content">
                                <span class="info-box-text">Running projects</span>
                                <span class="info-box-number">3</span>
                                <div class="progress">
                                    <div class="progress-bar" style="width: 70%"></div>
                                </div>
                                <span class="progress-description">
                                    70% Increase last 10 Days
                                </span>
                            </div><!-- /.info-box-content -->
                        </div><!-- /.info-box -->
                    </div>
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        <div class="info-box bg-green">
                            <span class="info-box-icon"><i class="fa fa-thumbs-o-up"></i></span>
                            <div class="info-box-content">
                                <span class="info-box-text">Likes</span>
                                <span class="info-box-number">510</span>
                                <div class="progress">
                                    <div class="progress-bar" style="width: 20%"></div>
                                </div>
                                <span class="progress-description">
                                    70% Increase in 30 Days
                                </span>
                            </div><!-- /.info-box-content -->
                        </div><!-- /.info-box -->
                    </div><!-- /.col -->
                    <div class="col-md-3 col-sm-6 col-xs-12">
                        <div class="info-box">
                            <span class="info-box-icon bg-red"><i class="fa fa-google-plus"></i></span>
                            <div class="info-box-content">
                                <span class="info-box-text">Likes</span>
                                <span class="info-box-number">80</span>
                            </div><!-- /.info-box-content -->
                        </div><!-- /.info-box -->
                    </div>
                </div>
            </h1>
    </div><!-- /.content-wrapper -->
<?php } else { ?>
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-aqua">
                        <div class="inner">
                            <h3><?php echo $projectstotal[0]->aantal; ?></h3>
                            <p>Projects</p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-bag"></i>
                        </div>
                       <a href="<?= site_url('Project/redirect_projects/' . $userdata['user_id']) ?>" class="small-box-footer">More info<i class="fa fa-arrow-circle-right"></i></a>
                    </div>
                </div>
            </h1>
    </div><!-- /.content-wrapper -->
<?php } ?>

1 个答案:

答案 0 :(得分:0)

尝试检查get_amount_projects_from_user($userid)方法的结果。我认为$query->result()会返回一个结果数组,因此您可以通过循环结果来获取项目的计数

检查此示例:

$sql = "select count(project_id) as aantal from tbl_user_project where user_id = $userid";
$query = $this->db->query($sql);

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->aantal;
   }
}

在您的情况下,我认为您应该使用$query->row(),因为我们确信我们只需要一个结果。

$sql = "select count(project_id) as aantal from tbl_user_project where user_id = $userid";
$query = $this->db->query($sql);

if ($query->num_rows() > 0)
{
   $row = $query->row(); 

   return $row->aantal;
}

我希望这能给你一些想法。

我认为

Here is a link很有用: