2017-09-02 145 views
1

我想要在用戶儀表板上自動顯示這些結果,當用戶登錄到他們的儀表板時,我希望此查詢運行並向他們顯示其用戶ID的統計信息。請建議如何做到這一點。如何獲得登錄用戶ID,統計的工作代碼在下面發佈。獲取登錄從codeigniter會話中的用戶ID

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

我越來越:

<?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 />"; 
     } 
?> 

檢查這個代碼後

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 
) 
+0

任何人,請幫幫忙。 –

+0

如何獲得用戶登錄? ?您必須將它存儲在codeigniter會話中? ? –

+0

@Root Ingenious:謝謝你在這裏,好友,是的,我已經存儲了,但正如你可以看到上面的代碼,一個是由你給出的完整的sqlquery,我將如何在選擇語句中應用用戶ID,現在它是手動userid = 1 ..或者我必須從數據庫中獲取所有的用戶ID和結果,將不得不篩選使用會話。 –

回答

2

所有我建議你先用,以檢查在控制器中的所有會話數據

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

嘗試打印並檢查什麼是會話中用於存儲用戶標識的密鑰。

假設你有一個像userId數組中的鍵可用,那麼 您可以使用下面的線

$userId = $this->session->userdata('client_user_id'); 

獲取會話數據,但要確保你有一個名爲「client_user_id」會話密鑰可用。

一旦你得到$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."' "); 
+0

嗨親愛的Punit,我檢查了會話數據,正如你上面提到的,以及我發佈的問題(更新)的結果。 –

+0

@GauravSingh,我已經更新了答案..嘗試'$ userId = $ this-> session-> userdata('client_user_id');'你會得到登錄的客戶端。 –

+0

像Charm Buddy一樣工作!非常感謝。 –