PHP配置文件列可见性+MySQL
我正在寻找在用户配置文件中实现列可见性的最佳方法 想象一下以下场景:PHP配置文件列可见性+MySQL,php,mysql,visibility,Php,Mysql,Visibility,我正在寻找在用户配置文件中实现列可见性的最佳方法 想象一下以下场景: 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 有了这个,我有两个表:pro
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\u visibility
侧面图
纵断面能见度
除了id\U user和tinyint数据的id之外,profile\U可见性的字段都是
现在,我想对所有列进行一个循环,以检查该列是否可见,而不是设置条件,因为我有更多的列。比如:
$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 solution
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>";
}
}
}
我认为这与你的设计有关: 我将创建一个表profile_字段,在其中定义可能的数据字段 配置文件字段:字段id、字段名称、字段位置 我的另一个表包含一个字段对于一个名为user\u的用户字段是否可见的信息 用户\字段:用户\字段\ id,用户\ id,字段\ id,可见 现在,您可以轻松地仅查询用户可见的字段:
SELECT field_id, field_name
FROM profile_fields
LEFT JOIN user_fields USING (field_id)
WHERE user_id=$user_id AND visible=true
您的值为$visibility[$i]['name'],即'1'。该值是否不被视为实际表示“名称可见”?如果值为“0”,它是否会被视为不可见。事实上,$visibility[$i]是完全错误的..因为我只从一个用户接收数据..所以创建多维数组毫无意义..我已将其更改为仅$visibility['name']和$profile['name']。实际上,您的解决方案是最好的做法。我已经通过PHP代码实现了这一点,但我必须承认你的答案是正确的。
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>";
}
}
}
SELECT field_id, field_name
FROM profile_fields
LEFT JOIN user_fields USING (field_id)
WHERE user_id=$user_id AND visible=true