我正在寻找在用户个人资料中实现列可见性的最佳方法。
想象一下以下场景:
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'
*/
}
编辑:已解决 - &gt;但这不是最好的解决方案,请参阅@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>";
}
}
}
答案 0 :(得分:0)
我认为这取决于你的设计:
我会创建一个表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