简体   繁体   English

如果mysql值包含另一个表中的另一个mysql值,请回显

[英]If mysql value contains another mysql value from a different table, echo

I have two tables, "customers" and "users". 我有两个表,“客户”和“用户”。

In the customers table, I have a column titled "name". 在客户表中,我有一列名为“名称”的列。 This has all customer names. 这具有所有客户名称。

In my users table, I have a column titled "managing". 在我的用户表中,我有一列名为“管理”。 This has certain customer names. 它具有某些客户名称。

I am trying to display content if "managing" CONTAINS ANY customer from "name". 我想,如果“管理” 包含 “名”客户的任何显示内容。

Here is an example... 这是一个例子

"name" contains customers "applebees", "johnny carinos", and "pizza hut". “名称”包含客户“ applebees”,“ johnny carinos”和“ pizza小屋”。

User rick is "managing" "applebees" and "pizza hut". 用户rick是“管理”“ applebees”和“披萨小屋”。 In his row/the column, it looks like "applebees,pizza hut" (i am using the implode function which is why there is a ,") 在他的行/列中,它看起来像是“ applebees,pizza小屋”(我使用的是implode函数,这就是为什么有一个,”)

So, on my customers page, I want rick to see applebees and pizza hut. 因此,在我的客户页面上,我希望rick看到applebees和披萨小屋。 I do NOT want to him to see johnny carinos, since he isn't managing it. 我不想让他见到约翰尼·卡里诺斯,因为他没有管理。

How can I do this? 我怎样才能做到这一点? I've tried preg match & strpos but failed miserably. 我尝试过preg match&strpos,但失败了。 (I'm still rather new to php). (我对php还是很陌生的)。 This would be much easier of it was a single value, so I can just set value = value. 如果是单个值,这会容易得多,所以我可以只设置value = value。 Unfortunately, having multiple values for "managing" really messes me up! 不幸的是,拥有多个用于“管理”的值确实让我感到困惑!

Hope this made sense. 希望这有道理。 Thanks for any help! 谢谢你的帮助!

Here's what I have now (yes, i know, it's terribly wrong, probably.) 这就是我现在拥有的(是的,我知道,这可能是非常错误的。)

<?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> 

<? } ?>

The answer lies in your database schema and the way you query it. 答案在于您的数据库模式和查询方式。 You didn't really get into your DB table structure but I would propose you use something like this 您并没有真正进入数据库表结构,但我建议您使用类似这样的内容

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]

Note I have three tables here. 注意我这里有三个表。 This allows you to relate any number of users to any number of customers. 这使您可以将任意数量的用户与任意数量的客户相关联。 NOw to get teh information you are looking for, let's assume you know the user_id of the user viewing the customers page. 现在获取要查找的信息,假设您知道查看客户页面的用户的user_id You query might look like 您的查询可能看起来像

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]

This will give you customer information for those customers related to the the user. 这将为您提供与该用户相关的那些客户的客户信息。


You also should not be using the mysql_* functions are they are deprecated (note the big red warning in PHP.net documentation). 您也不应该使用mysql_*函数,因为它们已被弃用(请注意PHP.net文档中的红色大警告)。 perhaps use mysqli_* functions. 也许使用mysqli_ *函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM