0

我有两个表,“客户”和“用户”。

在客户表中,我有一个标题为“名称”的列。这包含所有客户名称。

在我的用户表中,我有一个标题为“管理”的列。这有某些客户名称。

如果“管理”包含 来自“名称”的任何客户,我正在尝试显示内容。

这是一个例子......

“name”包含客户“applebees”、“johnny carinos”和“pizza hut”。

用户 rick 正在“管理”“applebees”和“pizza hut”。在他的行/列中,它看起来像“applebees,pizza hut”(我正在使用内爆函数,这就是为什么有一个,,)

所以,在我的客户页面上,我想让 rick 看到 applebees 和必胜客。我不想让他看到约翰尼·卡里诺斯,因为他没有管理它。

我怎样才能做到这一点?我试过 preg match & strpos 但失败得很惨。(我对 php 还是比较陌生)。这会更容易,因为它是单个值,所以我可以设置 value = value。不幸的是,拥有多个“管理”值真的让我很困惑!

希望这是有道理的。谢谢你的帮助!

这就是我现在所拥有的(是的,我知道,这可能是非常错误的。)

<?php


$sql = "SELECT * FROM customers";
$result = mysql_query($sql);
        $thecust = $_SESSION['user']['managing'];
    $thecustomers = htmlentities($row['name']);


    $check = strprk(wing, $thecust);
    if ($check) {

while ($row = mysql_fetch_array($result)) {
?>

    <tr> 
        <td><?php echo "<a href='customer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a>"?></td> 
        <td><?php echo htmlentities($row['contact_name'], ENT_QUOTES, 'UTF-8'); ?></td> 
        <td><?php echo htmlentities($row['contact_number'], ENT_QUOTES, 'UTF-8'); ?></td> 
        <td><?php echo htmlentities($row['contact_email'], ENT_QUOTES, 'UTF-8'); ?></td> 
    </tr> 

<? } ?>
4

1 回答 1

0

答案在于您的数据库架构和查询方式。你并没有真正进入你的数据库表结构,但我建议你使用这样的东西

users
-------
user_id INT, autoincrement, PRIMARY KEY
user_name VARCHAR
[other user-specific fields]

users_to_customers
-------
user_id INT
customer_id INT
[compound primary key across both fields]

customers
-------
customer_id INT, autoincrement, PRIMARY KEY
customer_name VARCHAR
[other customer specific fields]

注意我这里有三个表。这允许您将任意数量的用户与任意数量的客户相关联。现在要获取您正在寻找的信息,假设您知道user_id查看客户页面的用户。您的查询可能看起来像

SELECT c.customer_id, c.customer_name, [whatever other fields you need]
FROM users AS u
INNER JOIN users_to_customers AS uc
INNER JOIN customers AS c
WHERE u.user_id = ? [the user_id of the user viewing the page]

这将为您提供与用户相关的客户的客户信息。


您也不应该使用mysql_*已弃用的函数(请注意 PHP.net 文档中的大红色警告)。也许使用 mysqli_* 函数。

于 2012-12-08T00:48:51.060 回答