doqw89029 2016-11-25 04:30
浏览 62

从数据库中抓取一部分表并将其显示在网站上

I want to grab part of a table from a database called "TKACONT".

When a user clicks the button that executes the tkakata() function I am grabbing the Firstname, Lastname , and kata (points), then displaying them into a table.

I display the table, sorted from highest to lowest points, in a <div> with the ID displayrank.

Here is the code:

kataajax.js

function tkakata(){
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
    var xmlHttp;
    if (window.XMLHttpRequest) 
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    else {
            // code for IE6, IE5
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

    if(!xmlHttp)
        alert("Something went wrong")
    else
        return xmlHttp
}

function process(){
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
        {
            xmlHttp.open("GET", "tkakatagrab.php", true)
            xmlHttp.onreadystatechange = handleServerResponse;
            xmlHttp.send(null);
        }
    else
        {
            setTimeout('process()',1000);
        }
}

function handleServerResponse()
{
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.data;
            document.getElementById("displayrank").innerHTML = message;
            setTimeout('process()',1000);
        }
        else
        {
            alert("Database not connecting!")    
        }
    }
}}

tkakatagrab.php

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

$con = mysqli_connect('MY_SERVER_IP','USERNAME','PASSWORD','TKACONT');
if(!$con){
die('Could not connect: ' . mysqli_error($con));
}

$sql="SELECT Firstname,Lastname,Kata FROM Contestant ORDER BY Kata DESC";
$result = mysqli_query($con,$sql);

echo '<response>';
echo "<table>
<tr>
<th>Points</th>
<th>Firstname</th>
<th>Lastname</th>";
//while loop right here
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Kata'] .  "</td>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] .  "</td>";
echo "</tr>";
}

echo "</table>";
echo '</response>';

mysqli_close($con);
?>
  • 写回答

1条回答 默认 最新

  • doupaimo8288 2016-11-25 04:34
    关注

    This part of your query:

    ORDER BY Kata DESC
    

    orders the results from highest to lowest (ie: DESCending), change the DESC to ASC (ASCending) if you want the reverse order (lowest to highest).

    Hope this helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line