weixin_33708432 2016-03-11 02:51 采纳率: 0%
浏览 16

使用PHP,Ajax和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. 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 a a picture or a button persay. What I have currently is an image which when clicked just rotates and a class is toggled through 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). 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.

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
", 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. Is there way at all to use ajax or other type of DOM based code to achieve this shorting affect? 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.

  • 写回答

2条回答 默认 最新

  • weixin_33717117 2016-03-11 11:30
    关注

    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.

    There are many other similar plugins you can check out these links

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度