Displaying data on accordion?

Hi guys,

I have an accordion menu.
It has titles and sub tittles.

I have four tables,

  1. Classes table
    > Columns are:
    - classid
    - nameclass

  2. Parts table
    > Columns are:
    - partid
    - sequence
    - namepart
    - classid

  3. Lessons table
    > Columns are:
    - lessonid
    - sequence
    - namelesson
    - partid
    - classid

  4. Completions table
    > Columns are:
    - completionid
    - logoutid
    - userid
    - lessonid
    - classid

The data displayed on accordion menu is base from these tables above.
You can see this accordion menu live here,

Currently for displaying the lessons names on the accordion are these CodeIgniter codes below,

	function get_lesssonMenu()	
		$logoutid = $this->session->userdata('logoutid'); //Get logoutid from session.
		$classid = $this->get_ClassIdFromLessonId();	
		$table = $this->get_table();
		$this->db->select('c.lessonid as clessonid, l.*, c.completionid');
		$this->db->from($table.' as l');
		$this->db->join('completions as c', 'c.lessonid = l.lessonid','left');
		$this->db->where('`partid` IN(SELECT `partid` FROM `parts` WHERE classid = "'.$classid.'")', NULL, FALSE);
		$this->db->order_by("sequence", "asc");
		$query = $this->db->get();
		return $query->result_array();

How to make/change the codes above so only lessonid in the lesson table matched the lessonid in the completion table
and also the column ‘logoutid’ in the completion is equal to session variable $logoutid?

The session variable $logoutid is a cookie ID.
So when a visitor visited the page only lessons record (lesson table) related to his cookie (completion table) will be displayed on the accordion menu.

The codes above are not mine, that is why I’m currently scratching my head.
I hope someone will help me.

Thanks in advance.