hi guys.
I got problem here. I built the system of weighted average method calculation based on php. Now, I'm the last part which sum of the value in html table. Basically, this is my coding:-

<?php
session_start();
  $con = mysql_connect("localhost","root","");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("supplier", $con);
  
$criteria_weight = isset($_REQUEST['criteria_weight']) ? $_REQUEST['criteria_weight'] :""; 
$criteria_weight2 = isset($_REQUEST['criteria_weight2']) ? $_REQUEST['criteria_weight2'] :""; 
$criteria_weight3 = isset($_REQUEST['criteria_weight3']) ? $_REQUEST['criteria_weight3'] :""; 
$criteria_weight4 = isset($_REQUEST['criteria_weight4']) ? $_REQUEST['criteria_weight4'] :""; 
$criteria_weight5 = isset($_REQUEST['criteria_weight5']) ? $_REQUEST['criteria_weight5'] :""; 
$criteria_weight6 = isset($_REQUEST['criteria_weight6']) ? $_REQUEST['criteria_weight6'] :""; 
$criteria_weight7 = isset($_REQUEST['criteria_weight7']) ? $_REQUEST['criteria_weight7'] :""; 
$criteria_weight8 = isset($_REQUEST['criteria_weight8']) ? $_REQUEST['criteria_weight8'] :""; 
$criteria_weight9 = isset($_REQUEST['criteria_sweight9']) ? $_REQUEST['criteria_weight9'] :"";  

 
$profile = mysql_query("SELECT * FROM `supplier_criteria` "
. "WHERE `criteria_weight` = '" . $_SESSION['weights1'] . "'"
. " AND `criteria_weight2` = '" . $_SESSION['weights2'] ."'"
. " AND `criteria_weight3` = '" . $_SESSION['weights3'] ."'"
. " AND `criteria_weight4` = '" . $_SESSION['weights4'] ."'"
. " AND `criteria_weight5` = '" . $_SESSION['weights5'] ."'"
. " AND `criteria_weight6` = '" . $_SESSION['weights6'] ."'"
. " AND `criteria_weight7` = '" . $_SESSION['weights7'] ."'"
. " AND `criteria_weight8` = '" . $_SESSION['weights8'] ."'"
. " AND `criteria_weight9` = '" . $_SESSION['weights9'] ."';");



  $query = "SELECT MAX(details_value) as processor FROM supplier_details WHERE details_status = 'in process'"; 

  $result = mysql_query($query) or die(mysql_error());
  
   $query2 = "SELECT MIN(details_value) as processor2 FROM supplier_details WHERE details_status = 'in process' "; 

  $result2 = mysql_query($query2) or die(mysql_error());
  
  while($row = mysql_fetch_array($result)){
  
	$max = $row['processor'];
  }
   while($row2 = mysql_fetch_array($result2)){
   $min = $row2['processor2'];
 }
 
  
  echo "<table border='1'>"; 
$result = mysql_query ("SELECT * FROM supplier_details WHERE details_status='in process'");
	for($i=0; $i<$result; $i++) 
{ 
    
	while($row = mysql_fetch_array($result)){
{ 	   
      
       echo '<td width="40" valign="center">';   
  
      
	   $i = 0;
	   while ($i<'1')
	   {
	   if ($row['details_threshold']=='min'){
	    $avg_min = @( $row[14]-$min)/($max-$min);
		
		 $i++;
      	echo round($avg_min,2); 
		}
		else{
		$avg_max = @($max-$row[14])/($max-$min);
		$i++;
		echo round($avg_max,2);
	  	}
		}
	   echo "</td>";
    } 
  }
}
$query = "SELECT MAX(details_value2) as processor FROM supplier_details WHERE details_status = 'in process'"; 

  $result = mysql_query($query) or die(mysql_error());
  
   $query2 = "SELECT MIN(details_value2) as processor2 FROM supplier_details WHERE details_status = 'in process' "; 

  $result2 = mysql_query($query2) or die(mysql_error());
  
  while($row = mysql_fetch_array($result)){
  
	$max = $row['processor'];
  }
   while($row2 = mysql_fetch_array($result2)){
   $min = $row2['processor2'];
 }
 
  
  echo "<table border='1'>"; 
$result = mysql_query ("SELECT * FROM supplier_details WHERE details_status='in process'");
	for($i=0; $i<$result; $i++) 
{ 
    
	while($row = mysql_fetch_array($result)){
{ 	   
      
       echo '<td width="40" valign="center">';   
  
       	   $i = 0;
	   while ($i<'1')
	   {
	   if ($row['details_threshold']=='min'){
	    $avg_min = @( $row[15]-$min)/($max-$min);
		//echo $row[15];
		 $i++;
      	echo round($avg_min,2); 
		}
		else{
		$avg_max = @($max-$row[15])/($max-$min);
		$i++;
		echo round($avg_max,2);
	  	}
		}
	   echo "</td>";
    } 
  }
}
?>

all of this went at the right direction which the result will be:

0.62 1 0
0 1 1
total 0.62 2 1

but the things is I don't know how to make the value of total. can anyone help me?
p/s:- I'm just newbie in php.

Recommended Answers

All 21 Replies

What do you mean you can't make the value of total? You have a total listed there, are you trying to add something else together?

What do you mean you can't make the value of total? You have a total listed there, are you trying to add something else together?

no...the output that I can make it, just

0.62 1 0
0 1 1

I dont know how to make the value of
0.62 + 0 = 0.62,
1 + 1 = 2,
0 + 1 = 1.

$total = 0.62+0 ;
echo "".$total."";

You can also assign your values to a variable and add the variables together or load your values into an array and add that way, eg.

$value= array(2,5,6,8,9); 
echo "Sum of values = ".array_sum($value)."";

no....if u notice the total is depends on value of each row of criteria that stored in database. meanwhile the value of 0.62 and 0 can be change anytime because it's also calculate based on the value of criteria that user put in database.
I mean that it has the several process:-
1) user define the value of weight and max/min for the criteria. the criteria is fix to only 9 criteria.
2) user put the several supplier value of criteria which can be many supplier and this supplier should compare to select the best supplier.
3)the calculation is based on each of the criteria for all supplier.
4)the calculation is show in my coding which is i.e

$avg_min = @( $row[14]-$min)/($max-$min);
$avg_max = @($max-$row[14])/($max-$min);

5) this result of calculation will not stored in database but will display to the user as u can see the output of 0.62, 0 and 1.
5) then, get the total of all criteria for each supplier.

to make it clear.
criteria supplier 1 supplier 2 supplier 3
processor 0.62 0 1
hard disk 0 1 1

so, basically, my question, is it any method to get value for each row in the html table which the value is not stored in the database?
after that, sum of the value?

-sorry, u can ask again if u don't understand and not clear coz I dont know the best way to explain my problem.-

That's what I meant about assigning your values to a variable. Your row data eg. $row[14] will change from user to user.

$value1=$row[14];
$vaule2=$row[15];

$total = "".$value1." + ".$value2."";

$value1 and $value2 will always hold the values of rows 14 and 15 regardless of what their values are.

That's what I meant about assigning your values to a variable. Your row data eg. $row[14] will change from user to user.

$value1=$row[14];
$vaule2=$row[15];

$total = "".$value1." + ".$value2."";

$value1 and $value2 will always hold the values of rows 14 and 15 regardless of what their values are.

If I do like u said just now, the total will grab the value in the database, rite?
But I've already calculate with this

//this is based on user define as max or min. so it must do if else
$avg_min = @( $row[14]-$min)/($max-$min); 
//or
$avg_max = @($max-$row[14])/($max-$min);

so the total result should based on avg_min or avg_max result, not the first value in the database like row[14] or row[15]. I mean avg_min or avg_max will not stored in the database but they just grab value in the database.


the total result now we cannot see in the database, but shud be depends on what the avg_min and avg_max is displayed.

Yes, $row[14] etc. would come from the database but you can add other values such as $value=$_POST['value'] which would come from a form where the user input the value, it don't have to come from the database. It looks like your $min and $max avg are coming from the database. What numbers do you want to add, all the numbers in a particular column on your table?

$query3 = "SELECT SUM(details_value) as total FROM supplier_details WHERE details_status = 'in process'"; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$total = $row3['total'];
  }

That will give you the total of all numbers of 'details_value' in the 'supplier details' table.

yes...the value of $min and $max is coming from database. It is the max and min value of the criteria for all supplier. i means if there is 7 supplier, it must be 7 value of the processor. then, find which is the max and min of processor.
I want to add the result after calculate of the avg_min or avg_max supplier. It means when I calculate the min and max, I have to compare according to criteria. But the result is adding all the criteria for each supplier.
means for
supplier 1: avg_max/avg_min processor + avg_min/avg_max hard_disk and so on.
supplier 2: avg_min/avg_max processor + avg_min/avg_max hard_disk and so on...

Okay, I'm officially lost here. I have no clue what values you are trying to add together. Where is 'hard_disk' coming from? It's extremely easy to add values you just need to know what you want to add and I can't for the life of me figure out what you are trying to do.

$query3 = "SELECT SUM(details_value) as total FROM supplier_details WHERE details_status = 'in process'"; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$total = $row3['total'];
  }

That will give you the total of all numbers of 'details_value' in the 'supplier details' table.

okes, details_value = processor
details_value2= hard_disk

If I do like u said, it will total of processor for all supplier.

again,the sum of value cannot grab the value that is stored in database because the value is already calculate in avg_max/avg_min.
or...I'm tried to do something that is impossible?

Not impossible at all, it's almost as simple as figuring out what two numbers you want to add an sticking a + between the two. What I'm trying to find out is what you are trying to add. The way I understood it it just used numbers from the database to calculate a min and max avg. For example if you have 1,1,1,1,1 all stored under a particular column in you database the AVG would be 1 and the TOTAL 5. The code performs the calculation and returns the results but does not change the info in the database unless you tell it to.

Let's try it this way. EXACTLY what numbers do you want to add and where are you getting these numbers from?

Not impossible at all, it's almost as simple as figuring out what two numbers you want to add an sticking a + between the two. What I'm trying to find out is what you are trying to add. The way I understood it it just used numbers from the database to calculate a min and max avg. For example if you have 1,1,1,1,1 all stored under a particular column in you database the AVG would be 1 and the TOTAL 5. The code performs the calculation and returns the results but does not change the info in the database unless you tell it to.

Let's try it this way. EXACTLY what numbers do you want to add and where are you getting these numbers from?

details_value=processor
details_value2=hard-disk
the numbers that I'm trying to add is avg_max/avg_min processor + avg_max/avg_min hard_disk and so on. In this case, the number of avg_max is using this formula:
$avg_min = @( $row[details_value]-$min)/($max-$min);
details_value=processor
details_value2=hard_disk

let say if I have 7 suppliers...
let say the criteria for each of supplier is 2.
the value of criteria that I assigned is:-
details_value=processor
details_value2=hard_disk
so, in my database it shoud have 7 details_value and 7 details_value2 becoz of I have 7 suppliers,rite?

so, the figure of max is for each of supplier. it means that I have to find the max of details_value in 7 suppliers and min of details_value in 7 suppliers.
then, I calculate......
when I calculate...everthing went right which the result is as u see: 0.62 for avg_min processor for supplier1
1 for avg_min processor for supplier2
0 for avg_min processor for supplier3

then
0 for avg_min hard_disk for supplier1
1 for avg_min hard_disk for supplier2
1 for avg_min hard_disk for supplier3

the thing that I want to add is the processor of supplier1 + hardisk supplier1...and same to supplier2...and so on.

$query3 = "SELECT details_value FROM supplier_details WHERE supplier = ' $supplier_id ' "; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$total1 = $row3['details_value'];

$query4 = "SELECT details_value2 FROM supplier_details WHERE supplier = ' $supplier_id ' "; 
$result4 = mysql_query($query4) or die(mysql_error());
while($row4 = mysql_fetch_array($result4)){
 
	$total2 = $row4['details_value2'];
        
        $total = "".$total1." + ".$total2."";

echo "".$total."<br />";
  }}

Not sure what i was thinking, you can do it this way.

$query3 = "SELECT details_value, details_value2 FROM supplier_details WHERE supplier = ' $supplier_id ' "; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$total1 = $row3['details_value'];
        $total2 = $row3['details_value2'];
 
        $total = "".$total1." + ".$total2."";
 
echo "".$total."<br />";
  }

thank you my dear friend. But still, the output is not what I want.
I cannot do select sum coz it will sum the original value.
what i want to sum is the value after i do the calculation which is
avg_min/avg_max.
ok, let me put the real value.
details_value that put by user:
supplier1=2.8,threshold=min(user can also select max)
supplier2=3.4,threshold=min(user can also select max)
supplier3=1.8,threshold=min(user can also select max)
details_value2 that put by user:
supplier1=80,threshold=min(user can also select max)
supplier2=160,threshold=min(user can also select max)
supplier3=160,threshold=min(user can also select max)

this details_value and details_value2 will be put in the database.
so, based on this data, I have to calculate the avg_min or avg_max.
so, if user select min:-
$avg_min = @( $row[14]-$min)/($max-$min);
supplier1, avg_min= ((2.8-1.8)/(3.4-1.8));
supplier2, avg_min= ((1.8-1.8)/(3.4-1.8));
supplier3, avg_min= ((3.4-1.8)/(3.4-1.8));

same goes to details_value2
$avg_min = @( $row[14]-$min)/($max-$min);
supplier1, avg_min= ((80-80)/(160-80));
supplier2, avg_min= ((160-80)/(160-80));
supplier3, avg_min= ((160-80)/(160-80));

so, the result is
supplier1=0.62
supplier2=0
supplier3=1

details_value2
supplier1=0
supplier2=1
supplier3=1
I want to add and get stuck when I want
0.62 + 0=0.62
0+1=1
1+1=2

if I do like u said just now, the value will become:-
1.8+3.4+2.8+80+160+60, rite?
so, its not the output of what I want.
can u understand? or I still can't make u understand it?

Rename one of your variables for $avg_min and add the two after the calculation.

$total = "".$avg_min." + ".$avg_min1."";
echo "".$total."";

Rename one of your variables for $avg_min and add the two after the calculation.

$total = "".$avg_min." + ".$avg_min1."";
echo "".$total."";

it only calculate the last supplier which is supplier 3.
the output now is 1+1=2.
how to make it calculate every supplier?

You need to loop through your result set.

query3 = "SELECT details_value, details_value2 FROM supplier_details WHERE supplier = ' $supplier_id ' "; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$avg_min1 = @( $row3[details_value]-$min)/($max-$min);
        $avg_min2 = @( $row3[details_value2]-$min)/($max-$min);
 
        $total = "".$avg_min1." + ".$avg_min2."";
 
echo "".$total."<br />";
  }

You need to loop through your result set.

query3 = "SELECT details_value, details_value2 FROM supplier_details WHERE supplier = ' $supplier_id ' "; 
$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
 
	$avg_min1 = @( $row3[details_value]-$min)/($max-$min);
        $avg_min2 = @( $row3[details_value2]-$min)/($max-$min);
 
        $total = "".$avg_min1." + ".$avg_min2."";
 
echo "".$total."<br />";
  }

the avg_min or avg_max also depends on the threshold that user put. the things is I don't know how to modify if it's using my coding. The coding that you provide here is if user only choose min as threshold. what if user choose max as threshold? if user choose 1 max and 1 min as threshold? n dats why i put if else in my coding. then i loop it for each details_value. the things is i dont know where to loop my result as each of the details_value i have to put if else.

Sounds like you need your queries to execute based what the user picks.

if(user picks min){
run this query } 
else { run other query }

You just need to figure out what conditions need to be in place for it to run.

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.