Jump to content

Parent checkbox to match with children checkboxes


genzedu777

Recommended Posts

Hi all,

 

I need some help here.

 

Currently I am using 2 functions.

1) To call out the 'academic levels'

2) To call out the 'subjects' which matches to the specific 'academic level'

 

However, I am stucked in the codes.

 

Please view my picture attachment, my question is, how do I assigned 'red box (Pre-School)' to 'red box (subjects)' and so on...What are the codes or tweaks that I should make so that it will look like 'what do I want to achieve.jpg'

 

 

My process and execution

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_error($dbc));
    
    //Run query
    $tutor_id = mysqli_real_escape_string($dbc, $_GET['tutor_id']);
    $query = "SELECT tl.level_id, tl.level_name,
                     ts.subject_id, ts.subject_name, tsl.subject_level_id, 
                     IF(tosl.tutor_id='{$tutor_id}', 1, 0) as checked
              FROM tutor_level AS tl
              INNER JOIN tutor_subject_level AS tsl
                  USING (level_id)
              INNER JOIN tutor_subject AS ts
                  USING (subject_id)
              LEFT JOIN tutor_overall_level_subject AS tosl
                  ON tosl.subject_level_id = tsl.subject_level_id
                  AND tosl.tutor_id = '{$tutor_id}'
              ORDER BY tl.level_id, ts.subject_name";
    $sql = mysqli_query($dbc, $query) or die(mysqli_error($dbc));

$query1 = "SELECT tl.level_id, tl.level_name,
                     IF(tslvl.tutor_id='{$tutor_id}', 1, 0) as checked
              FROM tutor_level AS tl
              LEFT JOIN tutor_selected_level AS tslvl
                  ON tslvl.level_id = tl.level_id
                  AND tslvl.tutor_id='{$tutor_id}'
              ORDER BY tl.level_id, tl.level_name";
    $sql1 = mysqli_query($dbc, $query1) or die(mysqli_error($dbc));


//Process the results
    $checkboxes = ''; //Create variable for output
    $current_level_id = false; //Flag to check when records change level
    while($data = mysqli_fetch_array($sql)) //Iterate throug the DB results
    {
        if($current_level_id != $data['level_id']) //Determine if this level is different from the last
        {
		print_r ($data);
            $subjectCheckboxes .= createSubjectCheckboxes($subject_data, 5);
            $current_level_id = $data['level_id'];
            $subject_data = array();
        }
        //Add the current record to the $level_data array
        $subject_data[] = $data;
    }
//$checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5);

while($data1 = mysqli_fetch_array($sql1))
{
		print_r ($data1);
		$levelCheckboxes .= createLevelCheckboxes($level_data, 5);
            $level_data = array();
		$level_data[] = $data1;
}

    //Call the createLevelCheckboxes() function to generate the HTML for the LAST level records
    $levelCheckboxes .= createLevelCheckboxes($level_data, 5);
$subjectCheckboxes .= createSubjectCheckboxes($subject_data, 5);

 

 

My functions

function createLevelCheckboxes($levelArray, $columns)
    {	
        if(count($levelArray)==0) { return false; }
        $htmlOutput = '';
	foreach($levelArray as $data1)
        {
        //Display level header row
        $levelID   = $levelArray[0]['level_id'];
        $levelName = $levelArray[0]['level_name'];
	$checked = ($data1['checked'] == 1) ? '  checked="checked"' : '';
        $htmlOutput .= "<tr>\n";
        $htmlOutput .= "<th colspan=\"{$columns}\" style=\"text-align:left;padding-top:15px;\">";
        $htmlOutput .= "<input name=\"level[]\" type=\"checkbox\" id=\"level_{$levelID}\" type=\"checkbox\" {$checked} value=\"{$levelID}\">";
        $htmlOutput .= "<span class=\"zone_text_enlarge\"><label for=\"level_{$levelID}\">{$levelName}</label></span>";
        $htmlOutput .= "</th>\n";
        $htmlOutput .= "</tr>\n";
	}
	return $htmlOutput;
}

function createSubjectCheckboxes($subjectArray, $columns)
    {	
        //Display each subject
        $recordCount = 0;
        foreach($subjectArray as $data)
        {
            //Increment counter
            $recordCount++;

            //Start new row if needed, 1/5 = R1 --> So create a new row
            if ($recordCount % $columns == 1)
            {
                $htmlOutput .= "<tr>\n";
            }
            
            //Display the record
            $subjectID   = $data['subject_level_id'];
            $subjectName = $data['subject_name'];
            $checked = ($data['checked'] == 1) ? '  checked="checked"' : '';
            $htmlOutput .= "  <td>\n";
            $htmlOutput .= "    <input name=\"subject_level[]\" class=\"subject_a\" type=\"checkbox\" {$checked}";
            $htmlOutput .= " id=\"subject_level_{$subjectID}\" value=\"{$subjectID}\"/>\n";
            $htmlOutput .= "    <label for=\"subject_level_{$subjectID}\" class=\"subject_1\">{$subjectName}</label>\n";
            $htmlOutput .= "</td>\n";
            
            //Close row if needed, 5/5 = 0 --> So close the row
            if ($recordCount % $columns == 0)
            {
                $htmlOutput .= "</tr>\n";
            }
        }
        //Close last row if needed
        if ($recordCount % $columns != 0)
        {
            $htmlOutput .= "</tr>\n";
        }
        return $htmlOutput;
    }

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.