using this query:

    $queryC = "SELECT * FROM  tbl_courses WHERE course_id='$course_id'";
        $resC = mysql_fetch_assoc(dbQuery($queryC));    
        $allUnits = $resC[course_unit];
print_r(array($allUnits));

returns:
Array ( [0] => 3 ) Array ( [0] => 3 ) Array ( [0] => 2 ) Array ( [0] => 2 )

but am expecting it to return

Array ( [0] => 3 ) Array ( [1] => 3 ) Array ( [2] => 2 ) Array ( [3] => 2 )

anybody whit usefull help please....

I need it as soon as possible

Thanks in advance

Recommended Answers

All 11 Replies

i tried it....i wanna sum the result return and use it in another calculation

Thanks for the reply

i am trying to get the course credit unit for cgpa calculation

<?php
$EnrollKey = $_GET['m'];
    db_connect();

    //  $sqlStud = "SELECT * FROM tbl_stud_registration WHERE stud_id='$EnrollKey'";
        //  $resStud = dbFetchArray(dbQuery($sqlStud));
            //$get_stud_id = $resStud['stud_id'];

            //echo $EnrollKey.'==>'.$get_stud_id;

    $stm = "SELECT * FROM enrolled_courses WHERE stud_id = '$EnrollKey'";
    $stmQuery = dbQuery($stm);


?>

<form id="formID" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<span class="clearfix">&nbsp;</span>
    <fieldset>
    <div class="enroll">
    <table cellspacing="0" width="100%">
      <thead>
        <tr>
          <th width="7%">course</th>
          <th width="28%">course title</th>
          <th width="5%">unit</th>
          <th width="7%">status</th>
          <th width="6%">score</th>
          <th width="6%">grade</th>
          <th width="3%">w.g.p</th>
          <th width="38%">&nbsp;</th>
        </tr>
      </thead>

      <tbody>
        <tr>
 <?php
//for ($i = 0; $i < $num_result; $i++) {
         while( $row = dbFetchArray($stmQuery)) {
             extract($row);      

        $querySes = "SELECT * FROM  tbl_registration_session WHERE session_id='$session'";
            $resSes = dbFetchArray(dbQuery($querySes)); 

        $queryC = "SELECT * FROM  tbl_courses WHERE course_id='$course_id'";
            $resC = mysql_fetch_assoc(dbQuery($queryC));    
            $allUnits = $resC[course_unit]; 
        //  foreach($allUnits as $values) {
            //  echo $values; } 

        $qr = "SELECT SUM(course_unit) FROM tbl_courses WHERE course_id='$course_id'";  
            $qr_res = dbFetchArray(dbQuery($qr));
            //echo $qr_res['SUM(course_unit)']; 

    //  $query_reg = "SELECT * FROM tbl_stud_registration WHERE stud_id='$get_stud_id'";
    //      $resReg = dbFetchArray(dbQuery($query_reg));

?>  

          <td><?php echo $resC[course_code] ; ?></td>
          <td><?php echo $resC[course_title] ; ?></td>
          <td><?php echo  $allUnits; ?></td>
          <td><?php echo $resC[course_status]; ?></td>
          <td><?php echo $score; ?></td>
          <td><?php $gd = __gradingSystem($score); echo $gd."PTS"; ?></td>
          <td><?php $wgpa = array($allUnits*$gd); echo $allUnits*$gd; ?></td>
          <td><?php print_r($wgpa); print_r($allUnits);?></td>
        </tr>
        <?php print_r(array($allUnits));?>
<?php } ?>
      </tbody>
    </table>
    <div class="width50 float-left text-align-right">
      Total Credit Unit: <strong><?php $cr = __sumCreditUnit($arr); echo $cr; ?></strong><br /><br />

To what i can debugg...i see that the array pocket[0] is holding all the values, if so how do i sum up everything in the array pocket or split it if possible to do my calculation

Member Avatar for diafol

You can use array_sum() to sum the contents of an array.
If the array is coming from an SQL query, depending on how you're using it, you could use SUM() along with a GROUP BY.

thanks diafol...but i have tried that too.....its just retruning the last value...but now i wanna use jquery to do it...

i have this code if you can help with..

$(document).ready(function(){
  $('input[name="units[]"]').each(function(){
      alert($(this).val());
  });

});

its ouptuting all the values but i wanna sum the value and put the summation in a textbox

you can sum the value from an array in a very simple style. eg;

<?php
$value;
// Array value contain 40 int;
$array_int[39];
// Now sum the stuff;

for($value=0;$value < count($array_int);$value++){

 // Sum the value from the array
 $value +=$array_int[$value];

}
echo "the Ssum is ";
echo $value;

?>
Member Avatar for diafol

You don't need javascript. That's not good.

thanks diafol...but i have tried that too.....

It's difficult to know what you're trying to do unless you explain which field(s) in the DB you're trying to sum.

If it's a number of fields for one record then, MYSQL can do it:

'SELECT field1, field2, field1 + field2 AS mysum FROM table1 WHERE field6 = 2319'

If it's a sum of values from mutliple records for one field:

'SELECT SUM(field7) FROM table1 WHERE field6 = 2319'

i guess the MYSQL SELECT SUM cant work if you are selecting from another database using the result from another database e.g

    $stm = "SELECT * FROM enrolled_courses WHERE stud_id = '$EnrollKey'";
    $stmQuery = dbQuery($stm);

             while( $row = dbFetchArray($stmQuery)) {
         extract($row); 
         //here course_id is extracted

    and



$qr = "SELECT SUM(course_unit) FROM tbl_courses WHERE course_id='$course_id'";  
            $qr_res = dbFetchArray(dbQuery($qr));
            echo $qr_res['SUM(course_unit)']; 

but its still not working......i have tried it several times and othere codes before posting my problem here

Member Avatar for diafol

but its still not working.....

When you say 'database', I'm assuming you mean 'table'. Either way it doesn't matter, all you need is a JOIN and a SUM.

$r = mysql_query("SELECT SUM(tc.course_unit) AS cunit FROM enrolled_courses AS ec INNER JOIN tbl_courses AS tc ON ec.course_id = tc.course_id WHERE ec.stud_id = '$EnrollKey' GROUP BY ec.stud_id");
//need a row num check here, anyway...
$d = mysql_fetch_assoc($r);
echo $d['cunit'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.