dongpa9277 2015-11-13 18:17
浏览 7
已采纳

如何使用php创建通知列表

I have created a notification list in my html page using php.

I need to load relevant data when clicking on the list item.(ex: when clicking on "[customer_name] needs to register" , it should load all the other data under that customer name, in a separate page)I have tried with following code but didn't get desired outcome.

Could you please help me to solve the problem.

<?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>";
    } 
}
?>
  • 写回答

2条回答 默认 最新

  • dougou1127 2015-11-13 18:46
    关注

    I have just polished the code little bit.

    index.php : Here you will show the notifications.

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

    CusRegReport.php : Here you will show the details of the Customer with the particular id.

    <?php
    
        $dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
    
        function details(){
            global $dbc;    // we need this in all our functions. Then only we will be able to do the db operations within the function
    
            $id = $_GET['id'];  // we are using the id that was passed in the URL
    
            $id = $mysqli->real_escape_string( $id );   // clean the id that the user passed via url, so that we could use it in our SQL query. Otherwise it is unsafe to use directly because it can lead to SQL injections
    
            $sql = "SELECT * FROM `customer` WHERE `customer_id` = '$id'";
            $r = mysqli_query($dbc,$sql);
            while($line=mysqli_fetch_array($r))
            {
                echo "<br/>" . $line['customer_name'] . "<br/>" . $line['ad_line_one'] . "</a>";        
            }
        }
    ?>
    <!doctype html>
    <html>
        <body>
            <?php details(); ?>
        </body>
    </html>
    

    Things to remember:

    • You have declared the mysqli object $dbc at the very top of the page. So if you want to use that object to access the db, from inside a function, you need to use the global keyword like this: global $dbc;

    • Avoid excessive use of the keyword global. You were using it to declare each variable! That's not needed.

    • When you echo the <a> links to the customer details page, include the customer_id in the url after a ?. The data we pass like that, will be available in the $_GET array at the resulting page.

    • Always try to use an integer value to identify the customer. Because more than one customer can have the same name! Since you are already having the details of the customer in your db, I believe you have used a PRIMARY KEY for that table, which might be customer_id, I guess. So use that.

    • Avoid unwanted concatenations. It could give confusions to you later. In your first echo line, you have used lots of concatenations even though you were concatenating just the strings.

    • Try to escape the data that you accept from the user, before using it in your SQL queries. Because the data that the user provides may cause sql injection attacks. So always clean the user input.

    These are just quick basic things for you.

    Hope it will help. NOTE: I only polished your code a little bit. No testing or anything done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题