简体   繁体   中英

Grabbing part of a table from a database and displaying it on a website

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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