【发布时间】:2014-10-20 08:22:55
【问题描述】:
我需要组合一个方法,让我可以量化用户连续填写了多少字段。 并返回一个反映用户记录完成程度的值。这样的
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)
但是如果我需要从 4 个带有 id 的表中查询呢?
我这样做是为了尝试
<?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%
现在我怎样才能以正确的方式编写查询以使其按预期工作。 谢谢。
【问题讨论】:
-
我不是 PDO 的专业人士,但正如我所见,您正在覆盖
$completeProfile变量,因此当$completeProfile->execute()发生时,您只会得到最后的查询结果。 -
所以你认为我应该更改每个查询的名称并再次执行每个查询,如
$completeProfile1->execute()和$completeProfile2->execute()我理解你的意思吗