Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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创建通知列表_Php_Html_Mysql_Mysqli - Fatal编程技术网

如何使用php创建通知列表

如何使用php创建通知列表,php,html,mysql,mysqli,Php,Html,Mysql,Mysqli,我已经使用php在html页面中创建了一个通知列表 我需要在单击列表项时加载相关数据。(例如:当单击“[customer_name]需要注册”时,它应该在一个单独的页面中加载该客户名称下的所有其他数据)我已尝试使用以下代码,但没有得到期望的结果 你能帮我解决这个问题吗 <?php $dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error('')); function notifi

我已经使用php在html页面中创建了一个通知列表

我需要在单击列表项时加载相关数据。(例如:当单击“[customer_name]需要注册”时,它应该在一个单独的页面中加载该客户名称下的所有其他数据)我已尝试使用以下代码,但没有得到期望的结果

你能帮我解决这个问题吗

<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function notification() {
    global $dbc;
    $query = "SELECT `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
    $result = mysqli_query($dbc,$query);
    global $row;
    while($row=mysqli_fetch_array($result))
    {
        echo"<a href='CusRegReport.php'>"."<i class='fa fa-users text-red'>"."&nbsp;&nbsp;".$row['customer_name']."&nbsp;&nbsp;"."needs to register"."</i>"."</a>";
        global $x;
        $x = $row['customer_name'];
    }
}

function details(){
    global $x;
    $sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
    $r = mysqli_query($dbc,$sql);
    while($line=mysqli_fetch_array($r))
    {
        $d = "<br/>".$line['customer_name']."<br/>".$line['ad_line_one']."</a>";
    } 
}
?>

我刚刚稍微润色了一下代码

index.php
:这里您将显示通知

<?php
    $dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));

    function notification(){
        global $dbc;
        $query = "SELECT `customer_id`, `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
        $result = mysqli_query($dbc,$query);        
        while($row=mysqli_fetch_array($result))
        {
            echo "<a href='CusRegReport.php?id=" . $row['customer_id'] . "'><i class='fa fa-users text-red'>&nbsp;&nbsp;" . $row['customer_name'] . "&nbsp;&nbsp; needs to register</i></a>";        
        }
    }
?>
<!doctype html>
<html>
    <body>
        <?php notification(); ?>
    </body>
</html>

要记住的事情:

  • 您已经在页面顶部声明了mysqli对象
    $dbc
    。因此,如果您想使用该对象从函数内部访问db,您需要使用如下的
    global
    关键字:
    global$dbc

  • 避免过度使用关键字
    global
    。您使用它来声明每个变量!这是不必要的

  • 当您回显客户详细信息页面的
    链接时,请在url中的
    后面添加
    客户id
    。我们这样传递的数据将在结果页面的
    $\u GET
    数组中可用

  • 始终尝试使用整数值来标识客户。因为不止一个客户可以有相同的名字!由于您的数据库中已经有客户的详细信息,我相信您已经为该表使用了
    主键
    ,我想可能是
    customer\u id
    。所以用这个

  • 避免不必要的连接。这可能会让你以后感到困惑。在您的第一行
    echo
    中,您使用了很多串联,即使您只是串联字符串

  • 在SQL查询中使用之前,请尝试从用户处转义您接受的数据。因为用户提供的数据可能导致sql注入攻击。因此,请始终清理用户输入

这些对你来说只是一些简单的事情

希望这会有帮助。
注意:我只是稍微润色了一下你的代码。没有进行测试或任何操作。

我可以在这里看到两种方法:

客户类别

在这种情况下,我建议使用
$\u会话
全局数组,这样您就可以将数据从一个页面传递到另一个页面。整个过程可能是这样的:

  • 创建可用于有序管理客户数据的结构,例如,客户类
  • 一次获取所有客户信息,并将其存储在类的对象上。您的
    函数详细信息()
    似乎是正确的位置
  • 将该客户对象分配给$\u会话全局数组
  • 移动到另一个页面并从Customer对象加载数据
  • 简而言之,代码应该是:

    //Customer class
    class Customer
    {
        //All data and methods for the class to work
    }
    
    //...
    
    function details()
    {
        global $x;
        $currentCustomer = new Customer();
        $sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
        $r = mysqli_query($dbc,$sql);
        while($line=mysqli_fetch_array($r))
        {
            $currentCustomer->name = $line["name"];
            $currentCustomer->age = $line["age"];
            //etc...
        } 
        $_SESSION["CurrCustomer"] = $currentCustomer;
    }
    
    //... In some other page
    $customer = (Customer)$_SESSION["CurrCustomer"];
    echo "<br/>".$customer->name."<br/>".$customer->age." years</a>";
    
    customer.php

    //与您的details()版本非常相似
    如果(!isset($\u GET[“n”]))
    标题(“Location:index.php”);
    其他的
    {
    $customerName=$\u GET[“n”];
    $sql=“从'customer'中选择*,其中'customer\u name`='$customerName';
    $r=mysqli_查询($dbc,$sql);
    而($line=mysqli\u fetch\u数组($r))
    {
    $d=“
    ”。$line['customer\u name']。”
    “$line['ad\u line\u one']。”; } }
    我希望这对你有帮助。如果您有疑问或不清楚,请告诉我。
    祝你好运

    告诉我们结果,告诉我们expected@Ishara请具体说明你的问题。我们不需要你的全部,但是你的问题是@Drew-当调用函数details()时,它只给出列表的最后一个元素。使用上述代码。我希望为选定的客户名称显示“客户名称”和“广告行名称”。-它工作正常,非常感谢您宝贵的回答。所有问题都已更正。:)@伊沙拉拉:很高兴知道这有帮助:)
    //Customer class
    class Customer
    {
        //All data and methods for the class to work
    }
    
    //...
    
    function details()
    {
        global $x;
        $currentCustomer = new Customer();
        $sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
        $r = mysqli_query($dbc,$sql);
        while($line=mysqli_fetch_array($r))
        {
            $currentCustomer->name = $line["name"];
            $currentCustomer->age = $line["age"];
            //etc...
        } 
        $_SESSION["CurrCustomer"] = $currentCustomer;
    }
    
    //... In some other page
    $customer = (Customer)$_SESSION["CurrCustomer"];
    echo "<br/>".$customer->name."<br/>".$customer->age." years</a>";
    
    function notification()
    {
         //(Just as you have it)
    }
    
    //In case you want to move the user automatically to the other page:
    header("Location: customer.php?n=".$x);
    
    //OR, in case you want to use a link there
    echo "<a href='customer.php?n=".$x."'>Go there!</a>";
    
    //Something very similar to your version of details()
    if (!isset($_GET["n"]))
         header("Location: index.php");
    else
    {
        $customerName = $_GET["n"];
        $sql = "SELECT * FROM `customer` WHERE `customer_name` = '$customerName'";
        $r = mysqli_query($dbc,$sql);
        while($line=mysqli_fetch_array($r))
        {
            $d = "<br/>".$line['customer_name']."<br/>".$line['ad_line_one']."</a>";
        }
    }