The code selects department, which then lists courses in that department, i then want the total amount of resources for linked courses (parent and child courses) to also be echoed.
I am trying to get a Total sum from foreach count in php array.
Can anbody help me with this please?
Here is my code:
$categoryid = $_POST['dept'];
//get course codes from department
$get_dept_codes = mysql_query("SELECT id, name FROM course_categories WHERE parent = 0 order by name asc");
echo "<form method='POST' action='gsb_by_department.php'><p>";
echo "<select size='1' name='dept'>";
//loop through and list department names in drop down box
while($row = mysql_fetch_array($get_dept_codes))
{
$catid = $row['id'];
$catname = $row['name'];
echo "<option name='category' value=$catid>$catname</option>";
//echo $catid;
//echo $catname;
echo "<br />";
}
echo "</select><input type='submit' value='Submit' name='submit'></p></form>";
//get course codes from department
$get_dept_codes = mysql_query("SELECT course.id, course.shortname, course.fullname, gsb_content.gsb
FROM _course INNER JOIN gsb_content ON course.id = gsb_content.courseid
WHERE (((course.category)=$categoryid)) AND course.metacourse = 0
ORDER BY course.id;");
//loop through and process for courses
while($row = mysql_fetch_array($get_dept_codes))
{
$childcourse = $row['id'];
//Get the $parentcourses
$parentcourses= mysql_query("SELECT parent_course FROM course_meta where child_course = $childcourse");
//for each of the $parentcourses count FROM resource where course=$theparentcourse
foreach($parentcourses as $parentcourse)) {
$thenewid = $parentcourse['parent_course'];
$thecount = mysql_query("SELECT count(*) FROM resource where course=$thenewid");
}
//I want to be able to add up all the counts from the above and store in variable
$allcountstandardslinknum = array_sum($countstandardslinknum);
}
Does anybody have any ideas/guidance.
Would be greatly appreciated.
I’m not sure if the above was meant as pseudo code but the
mysql_querymethod returns a result object so you need to do something likemysql_fetch_array.As for getting the total you can just sum in your loop like so:
You may also want to add sanity checking and the like too.
Edit:
Also just noticed your foreach loop has the variables the wrong way, it should be:
I always remember this as a sentence: “for each of the items in $parentcourses name $parentcourse”
Edit2:
Your $parentcourses variable isn’t actually an array you need to grab each row in a similar way to your outer while loop something like: