Jump to content

Unwanted Multiplication Help


imperium2335

Recommended Posts

Hi,

 

I have the following script:

 

<?PHP

$match = '/.*?\\d+.*?(\\d+)(\\s+)/is' ;

$dbh = new PDO('mysql:host=localhost;dbname=xxxx', 'xxxx', 'xxxx') ;

$result = $dbh->query("SELECT companyName, invoiceAddress FROM detail_sheets WHERE area = 'Germany'") ;
$codes = array() ;
$names = array() ;
$c0s = array() ;
$c1s = array() ;
$c2s = array() ;
$c3s = array() ;
$c4s = array() ;
$c5s = array() ;
$c6s = array() ;
$c7s = array() ;
$c8s = array() ;
$c9s = array() ;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {

if(preg_match($match, $row['invoiceAddress'], $matches)) {

	$codes[] = substr($matches[1], 0, 1) ;
	$names[] = $row['companyName'] ;
	//array_push($found, substr($matches[1], 0, 1), $row['companyName']) ;

}

}

$i = 0 ;
foreach($codes as $current) {

$currentName = $names[$i] ;	

$result = $dbh->prepare("SELECT (sum(partbaskets.total) - sum(pourbaskets.total)) AS Margin
					   FROM partbaskets, pourbaskets, enquiries, jobs
					   WHERE partbaskets.enquiryRef = enquiries.id
					   AND pourbaskets.enquiryRef = enquiries.id
					   AND jobs.enquiryRef = enquiries.id
					   AND jobs.status != 'Cancelled'
					   AND enquiries.enqState != 'Inert'
					   AND enquiries.companyName = ?
					   ") ;
$result->bindParam(1, $currentName, PDO::PARAM_STR) ;
$result->execute() ;

	while($row = $result->fetch(PDO::FETCH_ASSOC)) {

		if($current == 0 && $row['Margin'])
			array_push($c0s, $row['Margin']) ;
		if($current == 1 && $row['Margin'])
			array_push($c1s, $row['Margin']) ;
		if($current == 2 && $row['Margin'])
			array_push($c2s, $row['Margin']) ;
		if($current == 3 && $row['Margin'])
			array_push($c3s, $row['Margin']) ;
		if($current == 4 && $row['Margin'])
			array_push($c4s, $row['Margin']) ;
		if($current == 5 && $row['Margin'])
			array_push($c5s, $row['Margin']) ;
		if($current == 6 && $row['Margin'])
			array_push($c6s, $row['Margin']) ;
		if($current == 7 && $row['Margin'])
			array_push($c7s, $row['Margin']) ;
		if($current == 8 && $row['Margin'])
			array_push($c8s, $row['Margin']) ;
		if($current == 9 && $row['Margin'])
			array_push($c9s, $row['Margin']) ;
	}
	$result = NULL ;
	$i++ ;
}

echo "<table><tr><td>Area</td><td>Margin</td></tr>" ;
echo "<tr><td>Area 0</td><td>" . array_sum($c0s) . "</td></tr>" ;
echo "<tr><td>Area 1</td><td>" . array_sum($c1s) . "</td></tr>" ;
echo "<tr><td>Area 2</td><td>" . array_sum($c2s) . "</td></tr>" ;
echo "<tr><td>Area 3</td><td>" . array_sum($c3s) . "</td></tr>" ;
echo "<tr><td>Area 4</td><td>" . array_sum($c4s) . "</td></tr>" ;
echo "<tr><td>Area 5</td><td>" . array_sum($c5s) . "</td></tr>" ;
echo "<tr><td>Area 6</td><td>" . array_sum($c6s) . "</td></tr>" ;
echo "<tr><td>Area 7</td><td>" . array_sum($c7s) . "</td></tr>" ;
echo "<tr><td>Area 8</td><td>" . array_sum($c8s) . "</td></tr>" ;
echo "<tr><td>Area 9</td><td>" . array_sum($c9s) . "</td></tr></table>" ;
?>

 

But what is happening is that the Margin is being multiplied by the number of rows in partbaskets (which is the same amount of rows in pourbaskets).

 

So for instance, if there is one part per job for a companyName, there is no problem and I get the correct margin.

 

But, if there is say 4 parts for one of the jobs, then the margin for that job will be multiplied by 4, which is wrong.

 

I have tried using GROUP but it didn't help.

 

Can someone please give me the missing link in my SQL statement? :(

Link to comment
https://forums.phpfreaks.com/topic/246268-unwanted-multiplication-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.