$ar1=array("Mobile","shop","software","hardware"); $arr2=arry("shop","Mobile","shop","software","shop")我想比较arr2和arr1的元素,即
$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 here 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'......."; }
是否有动态方式自动更新所有字段。
答案 0 :(得分:0)
查看PDO和准备好的语句,以及MySQL准备好的语句。 http://php.net/manual/en/pdo.prepared-statements.php和http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
答案 1 :(得分:0)
使用COUNT功能。例如: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
答案 2 :(得分:0)
$specific_fields = array_map(function($val, $total_fields){
return $val / count($total_fields)
}, $specific_fields, [$arr2]);
print_r($specific_fields);
对于更新sql语句应该像
$str_temp = '';
foreach ($specific_fields as $key => $f)
{
$str_temp .= "$key=$f"
}
$sql = "UPDATE ... SET $str_temp WHERE ID = <ID>"