how to find percentage and save it to mysql
$ar1=array("Mobile","shop","software","hardware");
$arr2=arry("shop","Mobile","shop","software","shop")
i want to compare the elements of arr2 to arr1 i.e
$counts=array();
foreach($arr2 as $val)
{
if(in_array($val, $arr1))
{
array_push($counts,$val);
// here will my insert query to insert data in mysql table.
}
} //end of foreach loop
$specific_fields = array_count_values($counts); /* Array ( [shop] => 3 [software] => 1 [Mobile] => 1)
$total_fields=count($arr2); // output will be 5
Now he开发者_如何学Gore first i want to find percentage for each element
i.e $per1=$specific_fields['shop']/$total_fields;
$per2=$per1*100;
$percentage=number_format($per2,0);
when i find the percentage how can i update below query with the percentage values of all elements.
$query="update table_name set shop='$percentage_value',Mobile='$percentage_value'.......";
}
Is there a dynamic way to update all the fields automatically.Have a look at PDO and prepared statements, and also at MySQL prepared statements. http://php.net/manual/en/pdo.prepared-statements.php and http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
Use the COUNT function. Example:update table_name set shop=(COUNT(*) FROM table_name WHERE column meets condition)/(COUNT(*) FROM table_name) * 100, Mobile = ...
http://www.tizag.com/mysqlTutorial/mysqlcount.php
$specific_fields = array_map(function($val, $total_fields){
return $val / count($total_fields)
}, $specific_fields, [$arr2]);
print_r($specific_fields);
For update sql statement should be like
$str_temp = '';
foreach ($specific_fields as $key => $f)
{
$str_temp .= "$key=$f"
}
$sql = "UPDATE ... SET $str_temp WHERE ID = <ID>"
加载中,请稍侯......
精彩评论