简体   繁体   中英

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".

User rick is "managing" "applebees" and "pizza hut". In his row/the column, it looks like "applebees,pizza hut" (i am using the implode function which is why there is a ,")

So, on my customers page, I want rick to see applebees and pizza hut. 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. (I'm still rather new to php). This would be much easier of it was a single value, so I can just set 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. 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). perhaps use mysqli_* functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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