简体   繁体   English

使用PHP,Ajax和MySQL

[英]Using PHP, Ajax and MySQL

I am currently working on a leaderboard of sorts for a website. 我目前正在为网站建立某种排行榜。 What is happening is I am pulling out records from a MySQL DB and echoing them into a table to be displayed to the user. 发生的事情是我正在从MySQL数据库中提取记录,并将它们回显到表中以显示给用户。 Sounds simple enough right? 听起来很简单吧? No what I also need to do is be able to sort both in ASC and DESC or depending on the state aa picture or a button persay. 我也不需要做的就是能够同时在ASC和DESC中进行排序,或者根据图片或按钮的状态进行排序。 What I have currently is an image which when clicked just rotates and a class is toggled through Jquery ATM. 我目前所拥有的是一张图像,当单击该图像时,它会旋转并通过Jquery ATM切换一个类。

I want to have it when one of the images is clicked it not only changes the image but also grabs a new function, or grabs a new query (using ASC or DESC order of course). 我想在单击其中一张图像时拥有它,它不仅可以更改图像,而且还可以获取新功能或获取新查询(当然使用ASC或DESC顺序)。 And is then displayed as such. 然后按原样显示。 I was trying to use AJAX to be able to connect the javascript aspects to the PHP part of my Database connection. 我试图使用AJAX来将javascript方面连接到数据库连接的PHP部分。

My current way of sending the data from the DB is through a function, 我目前从数据库发送数据的方法是通过一个函数,

<?php
function dbConnect()
{
    // make  & test connection
    $mysqli = new mysqli("localhost", "root", "password", "dbName");
    if (mysqli_connect_errno()) 
    {
        $err = 'Connection Failed';
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    // return
    return $mysqli;
}
function copTbl($mysqli)
{
    // prepare statement
    $stmt = $mysqli->prepare('
    SELECT `name`, `coplevel` FROM `players` WHERE `coplevel`<>"0" ORDER BY `    players`.`coplevel` DESC
    ');
    // execute query
    $stmt->execute();
    // bind result
    $stmt->bind_result($copName,$copLevel);
    $O ="";
    $x = 1;
    $O.="<h1>List Of Cops</h1>";
    $O.= "<form id=\"form1\">
                <div class=\"div-table\">
                    <div class=\"div-table-row\">
                        <div class=\"div-table-col head0\"><span class=\"col-name\">Player Name</span><img id=\"arrow0\" class=\"down\" src=\"img/arrow.png\" alt=\"\"></div>
                    <div class=\"div-table-col head1\"><span class=\"col-name\">Level</span><img id=\"arrow1\" class=\"down\" src=\"img/arrow.png\" alt=\"\"></div>
                </div>";
while($stmt->fetch())
{
    if($x%2)
    {
        $O.= "<div id='".$x."' class=\"div-table-row\">
                    <div class=\"div-table-col\">".$copName."</div>
                    <div class=\"div-table-col\"><img src='img/cl".$copLevel.".png' alt=''></div>
                </div>";
        $x++;
    }
    else
    {
        $O.= "<div id='".$x."' class=\"div-table-row\">
                    <div class=\"div-table-col white\">".$copName."</div>
                    <div class=\"div-table-col white\"><img src='img/cl".$copLevel.".png' alt=''></div>
                </div>";
        $x++;
        }
    }
    $O.= "</div></form>";
    // close statement
    $stmt->close();
    return $O;
}
?>

The function is only returning the values from the Mysql SELECT statement. 该函数仅从Mysql SELECT语句返回值。 Is there way at all to use ajax or other type of DOM based code to achieve this shorting affect? 有没有办法使用ajax或其他类型的基于DOM的代码来实现这种短路影响? I would like to be taught how to use it better as aoppsed to just given code, if you would explain it for me that would be much appreciated. 我想被教导如何更好地使用它,就像给定的代码一样,如果您能为我解释它,将不胜感激。

I think Data table will be easier and quicker way to tabulate the data . 我认为, 数据表将是一种更简便,更快速地将数据制成表格的方法 And if you get stuck at any place, can expect forum support bcoz most of the scenarios are covered and tried in this. 而且,如果您遇到任何麻烦,都可以期待论坛支持bcoz涵盖并尝试了大多数方案

There are many other similar plugins you can check out these links 还有许多其他类似的插件,您可以查看这些链接

I would also suggest Datatables. 我也建议使用数据表。

I would also suggest taking a look at Laravel, building that HTML with PHP looks a bit "old school". 我还建议您看一下Laravel,用PHP构建HTML看起来有点“老派”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM