在php数组中计算的所有计数总数

时间:2012-04-02 10:26:18

标签: php mysql arrays foreach

代码选择部门,然后列出该部门的课程,然后我也想要回应链接课程(父母和子课程)的总资源量。

我想从php数组中获取foreach计数的总和。 请问有人帮我吗?

这是我的代码:

$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);
}

有没有人有任何想法/指导。 非常感谢。

3 个答案:

答案 0 :(得分:2)

我认为这就是你所需要的:

$sql = "SELECT
    course_meta.parent_course,
    COUNT(DISTINCT resource.id) AS count
FROM
    course_meta
INNER JOIN resource ON course_meta.parent_course = resource.course
WHERE
    course_meta.child_course = $childcourse
GROUP BY course_meta.parent_course";

$query = mysql_query($sql);

while ($result = mysql_fetch_assoc($query))
{
  echo 'Course "' . $result['parent_course'] . '" has " . $result['count'] . " resources.<br />';
}

警告提示,如果$childcourse是一个字符串,则需要将其用单引号括起来。

另外,你确定这里的命名是对的吗?当然,儿童课程只有一门课程(以及可能有很多儿童课程的家长课程?)。

答案 1 :(得分:1)

我不确定上面是否是伪代码,但mysql_query方法返回结果对象,因此您需要执行mysql_fetch_array之类的操作。

至于获得总数,你可以像你这样在你的循环中求和:

$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";

您可能还想添加健全性检查等。

编辑: 还注意到你的foreach循环变量的方式错误,应该是:

foreach($parentcourses as $parentcourse){

我一直记得这句话:“为$ parentcourses name $ parentcourse中的每一项”

EDIT2: 您的$ parentcourses变量实际上不是一个数组,您需要以与外部类似的方式获取每一行,而类似于:

$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";

答案 2 :(得分:0)

试试这个:

$result= mysql_query("SELECT parent_course FROM course_meta where child_course = $childcourse"); 

$parentcourses = mysql_fetch_array($result);