First, we must remember that the result of the SELECT query is a newly created table. This is not a multidimensional array. If it were a multidimensional array, then you could leave with printing the category of tasks at the beginning of each new array, which could group all the tasks in one category, however, since this is not the type of result that SQL SELECT QUERY received, you print the category tasks after each line:
echo "<h4>$job_category</h4>"; echo "<p>$job_title</p>";
Decision:
The solution to your problem was to first use ORBER BY ASC in your sql query:
$get_job = mysqli_query($conn, "SELECT jobs.job_id, jobs.job_title, job_category.job_cat_name FROM jobs LEFT JOIN job_category ON job_category.job_cat_id = jobs.job_cat_id WHERE jobs.is_active = '1' ORDER BY job_cat_id ASC");
From there, you know that tasks in each category should be at least grouped next to each other (from the lowest to the highest, like 1,1,1,1,2,2,3,3,3). Now you can conditionally print $job_category if AND ONLY IF it hasn't been printed before.
Change this line:
echo "<h4>$job_category</h4>";
to this line:
if ($previous_print != $job_category) { echo "<h4>$job_category</h4>"; $previous_print = $job_category; }
Let me know if it works now.
source share