Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:41:16+00:00 2026-06-01T10:41:16+00:00

The code selects department, which then lists courses in that department, i then want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:41:18+00:00Added an answer on June 1, 2026 at 10:41 am

    I’m not sure if the above was meant as pseudo code but the mysql_query method returns a result object so you need to do something like mysql_fetch_array.

    As for getting the total you can just sum in your loop like so:

    $total = 0;
    foreach($parentcourse as $parentcourses)) {
        $thenewid = $parentcourse['parent_course'];
        $thecountrow = mysql_fetch_array(mysql_query("SELECT count(*) FROM resource where course=$thenewid"));
        $thecount = $thecountrow[0];
        $total += $thecount;
    }
    echo "Total is $total";
    

    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:

    foreach($parentcourses as $parentcourse){
    

    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:

    $parentcoursesres= mysql_query("SELECT parent_course FROM course_meta where child_course = $childcourse"); 
    $total = 0;
    while($parentrow = mysql_fetch_assoc($parentcoursesres)){
        $thenewid = $parentrow['parent_course'];
        $thecountrow = mysql_fetch_array(mysql_query("SELECT count(*) FROM resource where course=$thenewid"));
        $thecount = $thecountrow[0];
        $total += $thecount;
    }
    echo "Total is $total";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code selects the nodes, I want to work on...: <xsl:variable name=rootTextpageNode select=$currentPage/ancestor-or-self::node [@level
I have some code that first selects an option value in a dropdown menu
In the following code, the user selects a customer from the ComboBox and that
$(document).ready(function () { $(td:contains('1')).addClass('isOne'); }); I tried above code, but it also selects 11
I have executed a code SELECT CASE b.ON_LOAN when 'Y' then 'In Lib' when
I have a Devexpress Gridview that is linked to a delete, fetch and update
I have the following code which works FANTASTIC. Except - It is a sub-query
I have a question.Here is the code! f.select(:departments,Department.all.collect{|c|[c.name,c.id]},{},:size=>10,:multiple => ture) class Emergency has many
I want to hide the time frame (00:00:00) which is appearing in the result
I use below code which works fine for me in FF but Problem in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.