从codeigniter

时间:2017-09-02 14:47:29

标签: php mysql codeigniter-3

我想在用户仪表板上自动显示这些结果,当用户登录其仪表板时,我希望运行此查询并向其显示其用户ID的统计信息。请建议如何做到这一点。如何登录userid,统计信息的工作代码发布在下面。

<?php 
$query = $this->db->query("SELECT 
          tblclients.userid as client_id, 
          tblclients.company as company_name, 
            IFNULL(tblinvoices.total, 0) AS total_amount, 
              IFNULL(tblinvoicepaymentrecords.amount, 0) AS paid_amount, 
                ( IFNULL(tblinvoices.total, 0) - IFNULL(tblinvoicepaymentrecords.amount, 0)) AS amount_due 
            FROM 
            tblclients 
            LEFT JOIN tblinvoices ON tblclients.userid = tblinvoices.clientid 
            LEFT JOIN tblinvoicepaymentrecords ON tblinvoices.id = tblinvoicepaymentrecords.invoiceid
            WHERE tblclients.userid = 1");

foreach ($query->result_array() as $row)

{
      echo $row['total_amount'] . " " . $row['paid_amount'] . " " .$row['amount_due']; 
      echo "<br />";
      }
?>
检查此代码后

echo '<pre>'; print_r($this->session->all_userdata());exit;

我得到了:

Array
(
    [__ci_last_regenerate] => 1504507372
    [client_user_id] => 1
    [contact_user_id] => 1
    [client_logged_in] => 1
    [red_url] => https://www.trisuka.com/crm/admin
)

1 个答案:

答案 0 :(得分:2)

首先,我建议您使用

检查控制器中的所有会话数据
echo '<pre>'; print_r($this->session->all_userdata());exit;

尝试打印并检查会话中存储的密钥是​​什么,以存储用户ID。

假设您在数组中有密钥,例如userId 您可以使用以下行

获取会话数据
$userId = $this->session->userdata('client_user_id');

但请确保您在名为&#34; client_user_id&#34;的会话中有密钥可用。

一旦你从会话中获得了$userId,你可以将它连接到这样的查询。

$query = $this->db->query("SELECT 
          tblclients.userid as client_id, 
          tblclients.company as company_name, 
            IFNULL(tblinvoices.total, 0) AS total_amount, 
              IFNULL(tblinvoicepaymentrecords.amount, 0) AS paid_amount, 
                ( IFNULL(tblinvoices.total, 0) - IFNULL(tblinvoicepaymentrecords.amount, 0)) AS amount_due 
            FROM 
            tblclients 
            LEFT JOIN tblinvoices ON tblclients.userid = tblinvoices.clientid 
            LEFT JOIN tblinvoicepaymentrecords ON tblinvoices.id = tblinvoicepaymentrecords.invoiceid
            WHERE tblclients.userid = '".$userId."' ");