CodeIgniter中用户会话数据长数组简化及过滤脚本问题求助
Hey Andrea, let’s work through these three CodeIgniter + MySQL issues one by one—they’re all super common pain points, so I’ve got practical, no-fuss solutions for each:
1. Simplifying Long Arrays When Fetching User Session Data
You don’t need to pull every single database field for session use. Instead, limit your query to only the fields you actually need and leverage CodeIgniter’s built-in session system to avoid repeated database calls.
Fix:
- In your
Datalogin.phpmodel, create a dedicated method to fetch trimmed session data:public function get_user_session_data($user_id) { // Select ONLY the fields you need (e.g., id, username, role, email) return $this->db->select('id, username, role, email') ->where('id', $user_id) ->get('users') ->row_array(); // Returns a compact single-row array } - When the user logs in, store this trimmed data directly in the session:
// In Login.php controller, after validating credentials $user_data = $this->Datalogin->get_user_session_data($user_id); $this->session->set_userdata($user_data); - Now you can access individual session values directly without dealing with a bloated array:
// Get username anytime: $username = $this->session->userdata('username'); // Get role: $role = $this->session->userdata('role');
2. Avoiding Long Arrays for Admin Detail Pages
Manually writing out every field in your view is a nightmare when you have lots of data. Instead, use dynamic rendering or object-oriented results to clean this up.
Option 1: Use Object Syntax (Cleaner Than Arrays)
Modify your model to return an object instead of an array:
// In Datalogin.php public function get_admin_details($user_id) { return $this->db->where('id', $user_id) ->get('users') ->row(); // Returns a single object instead of array }
Then in your view, access fields directly as object properties (no messy brackets):
<p>Username: <?= $admin->username ?></p> <p>Email: <?= $admin->email ?></p> <p>Role: <?= $admin->role ?></p>
Option 2: Dynamic Rendering for All Fields
If you need to display every field without writing each one out, loop through the array/object in your view:
// View file (works for arrays or objects) <?php foreach ($admin_data as $field => $value): ?> <div class="detail-row"> <strong><?= ucfirst(str_replace('_', ' ', $field)) ?>:</strong> <span><?= $value ?></span> </div> <?php endforeach; ?>
This automatically renders every field—even if you add new ones later—no more updating your view every time the database schema changes.
3. Fixing Non-Working User-Filtered Data in Login Controller
Let’s troubleshoot this step by step, since there are a few common culprits:
Step 1: Confirm the Logged-In User’s ID Exists in Session
First, make sure the user’s ID is stored correctly. Add a quick check in your controller:
// In Login.php -> admin_details() method if (!$this->session->userdata('logged_in')) { redirect('login'); // Redirect if not logged in } $current_user_id = $this->session->userdata('user_id'); // Debug: Verify this isn't empty (remove after testing) var_dump($current_user_id);
Step 2: Fix the Model’s Filter Method
Ensure your model is correctly filtering by the user ID. Here’s a reliable implementation:
// In Datalogin.php public function get_filtered_data($user_id) { // Replace 'admin_logs' with your actual table name return $this->db->where('created_by', $user_id) // Use your actual foreign key column here ->order_by('created_at', 'DESC') ->get('admin_logs') ->result_array(); }
- Double-check your table’s foreign key name (is it
user_id,created_by, or something else?). - Add
echo $this->db->last_query();at the end of the model method to see the generated SQL—this will tell you if the WHERE clause is correctly applied.
Step 3: Correct Controller Implementation
Put it all together in your controller:
// In Login.php public function admin_details() { // Check login status if (!$this->session->userdata('logged_in')) { redirect('login'); } $current_user_id = $this->session->userdata('user_id'); $this->load->model('Datalogin'); // Fetch filtered data $data['user_data'] = $this->Datalogin->get_filtered_data($current_user_id); // Load the view $this->load->view('admin_details', $data); }
- Make sure your model filename is
Datalogin.php(CodeIgniter requires model filenames to start with a capital letter) and the class inside isclass Datalogin extends CI_Model { ... }. - Verify that the database table has rows associated with the current user’s ID—if there’s no matching data, the query will return an empty array.
内容的提问来源于stack exchange,提问作者Andrea Sha

