mysql 获取通知编号

dohp0rv5  于 2022-11-21  发布在  Mysql
关注(0)|答案(1)|浏览(143)

我有一个单独的PHP文件,包含一个有效的mysql查询,返回代表我的条件的行数(活动通知的数量),我有一个JQUERY代码来显示通知计数器,但我只能静态地使用它与.text('5 ')例如。我希望能够链接PHP MYSQL查询结果到浏览器上的JQUERY输出。
notfcounter.php:

<?php
function getNotCount() {
    require_once $_SERVER['DOCUMENT_ROOT'] . 'info.php';
    $conn   = new mysqli($hn,$user,$pass,$db) or die("Unable to connect");
    $count = $_POST['action'];
    $sqlquery = "SELECT did FROM users WHERE users.uid=".$_SESSION['id'];
    $result = $dbc->query($sqlquery);
    while ($row = $result->fetch_assoc()) {
        $rows = $row['did'];
    }
    $countquery = "SELECT noname, noaction FROM notfs WHERE notfs.did=".$rows." AND notfstatus = 0";
    $result2 = $dbc->query($countquery);
    if (mysqli_num_rows($result2)!=0)
    {  
       $count = mysqli_num_rows($result2);
       echo '<a href="#">'.$count.'</a>';
    }
    else
    {
       $count = 0;
       echo '<a href="#">'.$count.'</a>';
    }
echo $count;
}
?>

JQUERY外部文件:

$(document).ready(function () {
    // ANIMATEDLY DISPLAY THE NOTF COUNTER.
    $('#notfs_Counter')
        .css({ opacity: 0 })
        //.text('5') this is what I use to display a counter statically
        $.ajax({
        type: 'post',
        url: 'notfcounter.php',
        data: {action: 'getNotCount'},
        success: function(data) {
            alert(data);
        }
    })
        .css({ top: '0px' })
        .animate({ top: '0px', opacity: 1 }, 500);
});
jdgnovmf

jdgnovmf1#

在研究了更多关于 AJAX 的知识之后,我对它有了更好的理解,知道我错在哪里了。我不得不调整代码,重新定位AJAX success函数调用中的$(selector).命令,以正确显示我的数据。另外,我不需要在PHP代码中使用函数,我删除了该函数并回显了display $count所需的变量,并修改了AJAX操作以进行计数,代码如下:
AJAX 和JQUERY:

$.ajax({
            type: 'post',
            url: 'notfscounter.php',
            data: {action: 'count'},
            success: function(data) {
                //alert(data)
                $('#notfs_Counter').css({opacity: 0})
                $('#notfs_Counter').text(data)
                $('#notfs_Counter').css({ top: '0px'})
                $('#notfs_Counter').animate({ top: '0px', opacity: 1 }, 500);
            }
        });

PHP程式码:

<?php
    require_once $_SERVER['DOCUMENT_ROOT'] . 'info.php';
    $conn   = new mysqli($hn,$user,$pass,$db) or die("Unable to connect");
    $sqlquery = "SELECT did FROM users WHERE users.uid=".$_SESSION['id'];
    $result = $dbc->query($sqlquery);
    while ($row = $result->fetch_assoc()) {
        $rows = $row['did'];
    }
    $countquery = "SELECT noname, noaction FROM notfs WHERE notfs.did=".$rows." AND notfstatus = 0";
    $result2 = $dbc->query($countquery);
    if (mysqli_num_rows($result2)!=0)
    {  
       $count = mysqli_num_rows($result2);
       echo '<a href="#">'.$count.'</a>';
    }
    else
    {
       $count = 0;
       echo '<a href="#">'.$count.'</a>';
    }
echo $count;
?>

特别感谢@ADyson的鼓励和帮助。

相关问题