Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MY SQL针对不同的用户提供不同的数据_Php_Sql - Fatal编程技术网

PHP/MY SQL针对不同的用户提供不同的数据

PHP/MY SQL针对不同的用户提供不同的数据,php,sql,Php,Sql,我想在一个定制的论坛上为不同的用户显示不同的数据,但我不确定最好的方式 以下是我正在使用的结构: CREATE TABLE IF NOT EXISTS `forums_forums` ( `forum_id` int(11) NOT NULL auto_increment, `forum_name` varchar(100) NOT NULL, `order_number` int(11) NOT NULL default '0', `posts` int(11) NOT NUL

我想在一个定制的论坛上为不同的用户显示不同的数据,但我不确定最好的方式

以下是我正在使用的结构:

CREATE TABLE IF NOT EXISTS `forums_forums` (
  `forum_id` int(11) NOT NULL auto_increment,
  `forum_name` varchar(100) NOT NULL,
  `order_number` int(11) NOT NULL default '0',
  `posts` int(11) NOT NULL default '0',
  `replies` int(11) NOT NULL default '0',
  `forum_description` text NOT NULL,
  `allow_topics` int(11) NOT NULL default '0',
  `admin_only` int(11) NOT NULL default '0',
  `team` int(11) NOT NULL,
  PRIMARY KEY  (`forum_id`),
  KEY `forum_name` (`forum_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=102 ;
默认情况下,每个用户都应该看到除管理员和特定于团队的论坛之外的所有论坛。一些用户将是团队的一部分,但其他人不会,因此团队中的用户应该看到所有论坛,包括他们的团队特定论坛,而不是管理员论坛

如果用户是团队的一部分,则用户表具有与论坛团队匹配的相同团队id

以下是我刚刚拥有的,但我无法找到改变它的最佳方法:

if ($GLOBALS["USER"]["team"] == 1) {
    $a = mysql_query("SELECT * FROM forums_forums ORDER BY order_number");
} else {
    $a = mysql_query("SELECT * FROM forums_forums WHERE admin_only=0 ORDER BY order_number");
}
WHILE ($b = mysql_fetch_array($a)) {
?>
<?if ($b["admin_only"] == 1) {?>
<tr bgcolor="#111111" height="100">
<?} else {?>
<tr bgcolor="#000000" height="100">
<?}?>
<td valign="top">
<blockquote style="margin:10px;">
<a href="/forums-view.php?forum_id=<?=$b["forum_id"]?>"><font color="#FFFFCC"><b><?=$b["forum_name"]?></b></font></a>
</blockquote>
</td>
<td align="center">&nbsp;<b><?=number_format($b["posts"])?></b>&nbsp;</td>
<td align="center">&nbsp;<b><?=number_format($b["replies"])?></b>&nbsp;</td>
<td valign="top">
<blockquote style="margin:10px;">
<?=$b["forum_description"]?>
</blockquote>
</td>
</tr>
<?}?>
</table>
if($GLOBALS[“用户”][“团队”]==1){
$a=mysql_查询(“从论坛中选择*论坛按订单编号排序”);
}否则{
$a=mysql\u查询(“选择论坛中的*论坛,其中管理员仅=0订单编号”);
}
而($b=mysql\u fetch\u数组($a)){
?>

首先,您确实不需要在每一行上都添加PHP标记,只需将所有内容放在一个块中即可。我认为您可以做的最好的事情是,因为forums表可能不会包含太多的行,将它们全部选中,然后添加显示内容的条件。此外,我假设没有组的论坛的id为0

$table = '';

$sql = "SELECT * FROM forums_forums ORDER BY order_number ASC";
$run = mysql_query($sql);
while($row = $mysql_fetch_assoc($run))
{
    if(($_GLOBALS['users']['team'] == 1 && $row['admin_only']) || ($row['team'] > 1 && $_GLOBALS['users']['team'] == $row['team']) || ($row['team'] == 0 && $row['admin_only']))
    {
        $table .= '
            <tr>
                <td>'.$row['forum_name'].'</td>
                <td>'.$row['replies'].'</td>
                <td>'.$row['posts'].'</td>
                <td>'.$row['forum_description'].'</td>
            </tr>';

    }
}

echo '<table>'.$table.'</table>';
$table='';
$sql=“从论坛中选择*论坛按订单号排序\u ASC”;
$run=mysql\u查询($sql);
while($row=$mysql\u fetch\u assoc($run))
{
如果($_GLOBALS['users']['team']==1和$row['admin'u only'])|($row['team']>1和$u GLOBALS['users']['team']==1和$row['team'])|($row['team'==0和$row['admin'u only']))
{
$table.='
“.$row[“论坛名称”]。”
“.$row[“回复”]”
“.$row['posts']”
“.$row[“论坛描述”]。”
';
}
}
回显“.$table.”;
但请记住,这是一种非常糟糕的做法。如果你想要更干净、更可导航的代码,那么就研究模板制作。你可以自己编写一个模板类,而不会有太多困难。你应该做的是在一行中有一个包含所有HTML的模板,并根据需要经常重复该行


此外,您还应该查看表的一些数据类型。例如,如果admin_只能是1或0,那么它应该是长度为1的TINYINT。

一个提示:根据admin status或my更改样式表比在HTML中内联添加条件以更改标记属性要好得多。