我需要将一种方法组合在一起,使我可以量化用户连续填充了多少个字段。
并返回一个值,该值将反映用户记录的完成程度。这样
as:
User 1 = 100% complete
User 2 = 80% complete
User 3 = 60% complete
这里是相同的问题Counting how many MySQL fields in a row are filled (or empty)
但是如果我需要从ID为4的表中查询怎么办?
我这样做是尝试
<?php
$userId = $pro->id;
$completeProfile = $db->prepare("SELECT * FROM hired_person_info WHERE id=?");
$completeProfile = $db->prepare("SELECT * FROM hired_education_info WHERE id=?");
$completeProfile = $db->prepare("SELECT * FROM hired_job_info WHERE id=?");
$completeProfile = $db->prepare("SELECT * FROM hired_ts_info WHERE id=?");
$completeProfile->bind_param('i', $userId);
if ($completeProfile->execute()) {
$result = $completeProfile->get_result();
while ($row = $result->fetch_row()) {
$empty_count = 0;
$count = count($row);
for ($i = 0; $i < $count; $i++)
if ($row[$i] === '' || $row[$i] === 'NULL')
$empty_count++;
$com = ((int)(100 * (1 - $empty_count / ($count - 1))));
if ($com > 50) {
echo "<span class='f_left width_autor textLeft GreenFont padding_8R'>".((int)(100 * (1 - $empty_count / ($count - 1))))."%</span>";
} else {
echo "".((int)(100 * (1 - $empty_count / ($count - 1))))."%";
}
}
}
?>
但是,当我尝试运行代码时,即使我尝试完成一些配置文件以使其达到60%,它也总是会提供8%的完成率,如果我完成配置文件,它不会仅仅给出8%或100%
现在我该如何编写查询以使其按预期工作的正确方式。
谢谢。
最佳答案
@Mikky试试这个
<?php
$personProfile = $db->prepare("SELECT * FROM hired_person_info AS t1
JOIN hired_education_info AS t2
ON t1.id=t2.uid
WHERE t1.id=? GROUP BY t1.id ");
if ($completeProfile->execute()) {
$result = $completeProfile->get_result();
while ($row = $result->fetch_row()) {
$empty_count = 0;
$count = count($row);
for ($i = 0; $i < $count; $i++)
if ($row[$i] === '' || $row[$i] === 'NULL')
$empty_count++;
$com = ((int)(100 * (1 - $empty_count / ($count - 1))));
if ($com > 50) {
echo "<span class='f_left width_autor textLeft GreenFont padding_8R'>".((int)(100 * (1 - $empty_count / ($count - 1))))."%</span>";
} else {
echo "".((int)(100 * (1 - $empty_count / ($count - 1))))."%";
}
}
}
?>