如何使用php创建通知列表
我已经使用php在html页面中创建了一个通知列表 我需要在单击列表项时加载相关数据。(例如:当单击“[customer_name]需要注册”时,它应该在一个单独的页面中加载该客户名称下的所有其他数据)我已尝试使用以下代码,但没有得到期望的结果 你能帮我解决这个问题吗如何使用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
$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'>"." ".$row['customer_name']." "."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'> " . $row['customer_name'] . " needs to register</i></a>";
}
}
?>
<!doctype html>
<html>
<body>
<?php notification(); ?>
</body>
</html>
要记住的事情:
- 您已经在页面顶部声明了mysqli对象
。因此,如果您想使用该对象从函数内部访问db,您需要使用如下的$dbc
关键字:global
global$dbc代码>
- 避免过度使用关键字
。您使用它来声明每个变量!这是不必要的global
- 当您回显客户详细信息页面的
链接时,请在url中的
后面添加?
。我们这样传递的数据将在结果页面的客户id
数组中可用$\u GET
- 始终尝试使用整数值来标识客户。因为不止一个客户可以有相同的名字!由于您的数据库中已经有客户的详细信息,我相信您已经为该表使用了
,我想可能是主键
。所以用这个customer\u id
- 避免不必要的连接。这可能会让你以后感到困惑。在您的第一行
中,您使用了很多串联,即使您只是串联字符串echo
- 在SQL查询中使用之前,请尝试从用户处转义您接受的数据。因为用户提供的数据可能导致sql注入攻击。因此,请始终清理用户输入
注意:我只是稍微润色了一下你的代码。没有进行测试或任何操作。我可以在这里看到两种方法: 客户类别 在这种情况下,我建议使用
$\u会话
全局数组,这样您就可以将数据从一个页面传递到另一个页面。整个过程可能是这样的:
函数详细信息()
似乎是正确的位置//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>";
}
}