doumiang2297 2012-12-08 00:37
浏览 9
已采纳

如果mysql值包含来自不同表的另一个mysql值,则为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> 

<? } ?>
  • 写回答

1条回答 默认 最新

  • 「已注销」 2012-12-08 00:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题