Implementing User Profiles and Project Management : Day 7

Implementing User Profiles

Welcome to Day 7 of our journey to build a Implementing User Profiles, portfolio website with CodeIgniter! Today, we’ll focus on implementing user-specific features such as user profiles and project management. These functionalities will allow users to manage their profiles, add new projects, and update existing ones.

Implementing User Profiles

Step 1: Create Profile Model and Controller Methods

  • Profile Controller: Navigate to application/controllers/ and create a new file named Profile.php. Add the following code:
<?php
class Profile extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('project_model');
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    public function index() {
        $user_id = $this->session->userdata('user_id');
        $data['user'] = $this->user_model->get_user_by_id($user_id); // Implement this method in user_model
        $data['projects'] = $this->project_model->get_projects_by_user($user_id); // Implement this method in project_model
        $this->load->view('header');
        $this->load->view('profile', $data);
        $this->load->view('footer');
    }

    public function update_profile() {
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[50]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
        if ($this->form_validation->run() === FALSE) {
            $this->load->view('header');
            $this->load->view('profile');
            $this->load->view('footer');
        } else {
            $this->user_model->update_profile();
            $this->session->set_flashdata('success_msg', 'Profile updated successfully.');
            redirect('profile');
        }
    }
}
?>
  • Profile View: Create profile.php in application/views/ directory:
<!-- Display User Profile Details -->
<section>
    <h2>Profile Details</h2>
    <form action="<?php echo base_url('profile/update_profile'); ?>" method="post">
        <input type="text" name="username" value="<?php echo $user['username']; ?>" /><br />
        <input type="email" name="email" value="<?php echo $user['email']; ?>" /><br />
        <button type="submit">Update Profile</button>
    </form>
</section>

<!-- List of User Projects -->
<section>
    <h2>My Projects</h2>
    <?php foreach ($projects as $project): ?>
        <div class="project">
            <img src="<?php echo base_url('path/to/project_image.jpg'); ?>" alt="<?php echo $project['title']; ?>">
            <h3><?php echo $project['title']; ?></h3>
            <p><?php echo $project['description']; ?></p>
            <a href="#">Edit Project</a>
        </div>
    <?php endforeach; ?>
</section>

Step 2: Create Project Model and Controller Methods

  • Project Controller: Navigate to application/controllers/ and modify the Home.php file to include project management methods:
<?php
class Home extends CI_Controller {
    // Existing methods...

    public function add_project() {
        // Form validation rules for adding a project
        // Load necessary models and views for adding a project
    }

    public function edit_project($project_id) {
        // Form validation rules for editing a project
        // Load necessary models and views for editing a project
    }

    public function delete_project($project_id) {
        // Logic to delete a project
    }
}
?>
  • Project Model: Create project_model.php in application/models/ directory. Implement methods to handle project CRUD operations:
<?php
class Project_model extends CI_Model {
    public function get_projects_by_user($user_id) {
        // Retrieve projects for a specific user
    }

    public function add_project() {
        // Insert a new project into the database
    }

    public function update_project($project_id) {
        // Update an existing project in the database
    }

    public function delete_project($project_id) {
        // Delete a project from the database
    }
}
?>

Testing User Profiles and Project Management

  1. Access Profile Page: Open your web browser and go to http://localhost/your_project_name/index.php/profile. Ensure you can view and update user profile details.
  2. Manage Projects: Implement links or buttons in the profile view (profile.php) to add, edit, and delete projects.

Conclusion

Congratulations! You’ve successfully implemented user profiles and project management functionalities in your portfolio website using CodeIgniter. Users can now manage their profiles, add new projects, and edit existing ones. In Day 8, we’ll focus on integrating a blog section where users can create and manage blog posts.

Compalgo Labs offering School Management Software:Implementing User Profiles

Feel free to leave any questions or comments below. Keep exploring CodeIgniter and enhancing your website! Happy coding!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *