【问题标题】:PHP profile columns visibility + MySQLPHP 配置文件列可见性 + MySQL
【发布时间】:2014-01-25 00:58:25
【问题描述】:

我正在寻找在用户配置文件中实现列可见性的最佳方法。

想象以下场景:

Name   : stackoverflow   | [checkbox] visible = 1
Address: New York        | [checkbox] visible = 0
Phone  : 312 021 11      | [checkbox] visible = 1
Email  : stack@stack.com | [checkbox] visible = 1

有了这个,我有两个表:profile 和 profile_visibility

个人资料

ID
ID_User (joins with the User table)
Address
Phone
Email

profile_visibility

ID
ID_User (joins with the User table)
Address
Phone
Email

profile_visibility的字段都是tinyint数据的(id_user和id除外)。

现在,我想对所有列进行循环以检查该列是否可见,而不是创建条件(因为我有更多列)。比如:

$profile = $this->profile($id); // gets the info (as array) of profile
$visibility = $this->profile_visibility($id); // gets the visibility of fields

for($i = 0; $i <= sizeof($profile) - 1; $i++){
   /* I can't match $profile with $visibility because the values are different..

      $profile[$i]['name'] == $visibility[$i]['name']

      $profile[$i]['name'] > it's equal to: 'stackoverflow'
      $visibility[$i]['name'] > it's equal to: '1'

   */
}

编辑:已解决 -> 但这不是最好的解决方案,请参阅@niyou 解决方案。

foreach($visibility as $key => $val) {
    if($val == 1){
        if(array_key_exists($key, $profile)){
            $content .= "<tr>
                            <td class='text-align-right'>
                                <b>" . ucfirst($key) . "</b>:
                            </td>
                            <td class='width-100'>" . $profile[$key] . "</td>
                            <td>

                            </td>
                        </tr>";
        }
    }
}

【问题讨论】:

  • 你有$visibility[$i]['name']的值,就是'1'。这个值不被认为实际上是说“名字是可见的”吗?而如果值为'0',是否不被认为是不可见的。
  • 实际上$visibility[$i] 完全错误..因为我只收到一个用户的数据..所以制作多维数组没有意义..我已将其更改为仅$visibility['name'] 和@ 987654328@.

标签: php mysql visibility


【解决方案1】:

我认为这是你的设计问题:

我会创建一个表 profile_fields 我在其中定义我可能的数据字段

profile_fields: field_id,field_name,field_position,...

我的另一个表保存了一个字段是否对用户可见的信息 - 名为 user_fields

user_fields: user_field_id, user_id, field_id, visible

现在您可以轻松地仅查询对用户可见的字段:

SELECT field_id, field_name
FROM profile_fields
    LEFT JOIN user_fields USING (field_id)
WHERE user_id=$user_id AND visible=true

【讨论】:

  • 实际上,您的解决方案是最佳实践。我已经设法通过 PHP 代码让它工作了。但我必须接受你的答案是正确的。
猜你喜欢
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2012-06-04
  • 2012-10-18
  • 2012-08-01
  • 2010-09-18
相关资源
最近更新 更多