i m having some trouble trying to find the sum of an queried variable however this variable is inside a loop and it is not summing it is just returning the value at that instant in time this is the code below

@$code= $_POST['coursecode_1'];
if( is_array($code))
{
		//$sum= array();
	while (list ($key, $val) = each ($code)) 
	{
		//echo "$val ";  //val contains the course code
		 
		
		$q="SELECT sum(credits) FROM courses WHERE course_code='$val'";
		if ($result_2=mysql_query($q))
						echo '<p>nice1</p>';
						else
						echo 'good shit';
		
		$row = mysql_fetch_row($result_2);
		
		echo "$row[0");	
				
	}
}

when i select 2 courses i just get the credits of each course separately

create and alias and call the alias.

$q="SELECT sum(credits) as youralias FROM courses WHERE course_code='$val'";

echo $row['youralias'];

Hi there, i cant quite understand what u are trying to achieve here.

1. Please explain where the coursecode_1 is got? from an input field or checkboxes in which case how does it become an array?
2. Your while statement states the array is associative (key and val)
3. How many courses are you trying to get the sum of credits for?

Please explain further so maybe i could help. Thanks.

i get the coursecode_1 from a form that had a multiple select drop down menu so the amount of courses that i am trying to sum can be any number but like i said it is not giving me the sum it is just giving me the credits for each course. all i want is the sum of the credits of all the courses that was selected in the multiple select drop down menu

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.