2014-10-20 17 views
0

我需要组合一个方法,该方法将允许我量化一行中有多少个字段已被用户填充。 并返回一个反映用户记录完成程度的值。这种计算一行中有多少个MySQL字段从两个表中填充(或空)

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))))."%"; 
    } 
    } 
    } 
?> 

但是当我尝试运行代码,它总是给8%完成,即使我尝试完成一些配置文件,以使其60%,它没有它如果我完成配置文件,只需要提供8%或100%的配置文件

现在我该如何编写查询以使其按预期工作。 谢谢。

+0

我不亲的PDO,但我看,要覆盖'$ completeProfile'变量,所以你会得到只有最后querys结果时' $ completeProfile-> execute()'发生。 – vaso123 2014-10-20 08:27:21

+0

所以你认为我应该改变每个查询的名称,并执行每一个'$ completeProfile1-> execute()'和'$ completeProfile2-> execute()'我是否理解你 – Mikky 2014-10-20 08:31:30

回答

1

@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))))."%"; 
    } 
    } 
    } 
?> 
0

正如我在我的评论中提到的,我认为,因为你覆盖了你的$completeProfile而不是执行它们,只有最后一个。尝试像这样(未测试,因为我没有你的表结构,和你的自定义数据库处理函数)。

$userId = $pro->id; 

$query1 = "SELECT * FROM hired_person_info WHERE id=?"; 
showUserPercentage($userId, $query1, $db); 

$query2 = "SELECT * FROM hired_education_info WHERE id=?"; 
showUserPercentage($userId, $query2, $db); 

$query3 = "SELECT * FROM hired_job_info WHERE id=?"; 
showUserPercentage($userId, $query3, $db); 

$query4 = "SELECT * FROM hired_ts_info WHERE id=?"; 
showUserPercentage($userId, $query4, $db); 

function showUserPercentage($userId, $query, $db) { 
    $completeProfile = $db->prepare(); 
    $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)))) . "%"; 
      } 
     } 
    } 
} 
相关问题