【发布时间】: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