简体   繁体   English

仅将特定数量的行从Json数组输出到HTML表

[英]Outputting only a certain number of rows from a Json array to a HTML table

This might be difficult to explain, I will try my best. 这可能很难解释,我会尽力而为。

I have two databases. 我有两个数据库。 One with country names, and one with athlete names. 一个带有国家名称,另一个带有运动员名称。 These two tables are linked by the attribute ISO_id, which is the country ID (ie FRA, JPN, USA, etc...) 这两个表通过属性ISO_id链接,该属性是国家/地区ID(即FRA,JPN,USA等)。

I am joining these two tables with the following SQL statement: 我使用以下SQL语句将这两个表连接在一起:

$sql="SELECT Cyclist.name, Country.ISO_id, Country.total, Country.gold, Country.silver, Country.bronze, Country.country_name 
FROM Country JOIN Cyclist ON Country.ISO_id=Cyclist.ISO_id 
WHERE Country.ISO_id = 'JPN' OR Country.ISO_id = 'USA' ";

I want to print a table with only two rows, one for USA and one for Japan, showing only the country attributes. 我想打印一张只有两行的表,一行用于美国,一行用于日本,仅显示国家/地区属性。 I will later need the cyclist attribute to create a table showing all cyclists in a country upon the click of a button. 稍后,我将需要cyclist属性来创建一个表,单击一个按钮,该表显示一个国家/地区中的所有自行车手。

The issue is that when printing out a country's attribute, they are put in the HTML table as many times as there are athletes in a country. 问题是,当打印出国家/地区的属性时,将它们放在HTML表格中的次数与一个国家/地区中运动员的次数相同。 If USA has 15 athletes, the country table will show 15 times the USA country attributes. 如果美国有15名运动员,则国家/地区表将显示美国国家属性的15倍。

How can I solve this? 我该如何解决?

My js code: 我的js代码:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
    var textArr='<?php echo $rowsArr?>';  // php write into javascript, as jsontext 
    $(document).ready(function(){ 
        var jsonArr = JSON.parse(textArr); //make jsontext into json array
        var tr;
        for(var i = 0; i < jsonArr.length; i++) { //there will be more countries, so i cant hardcode 2 as the loop end
            tr = $('<tr/>');
            tr.append("<td>" + jsonArr[i].country_name + "</td>");
            tr.append("<td>" + jsonArr[i].ISO_id + "</td>");
            tr.append("<td>" + jsonArr[i].gold + "</td>");
            tr.append("<td>" + jsonArr[i].silver + "</td>");
            tr.append("<td>" + jsonArr[i].bronze + "</td>");
            tr.append("<td>" + jsonArr[i].total + "</td>");
            $('table').append(tr);//appending to html table

        }
    });
</script>

My HTML code 我的HTML代码

<table style="border: 1px solid black">
                <tr>
                    <th>Country name</th>
                    <th>Country ID</th>
                    <th>Gold Medals</th>
                    <th>Silver Medals</th>
                    <th>Bronze Medals</th>
                    <th>Total Medals</th>
                </tr>           
            </table>

Given the Country table already contains how many medals of each type has each country won, I see no reason to join that table with the Cyclist one. 鉴于“国家/地区”表已经包含每个国家/地区已获得每种类型的奖牌数量,因此我认为没有理由与骑自行车的人一起加入该表。

At this moment, your JOIN has the effect of, as you said, repeating the total medals for each Cyclist a country has. 如您所说,此时,JOIN的作用是重复为一个国家/地区的每个骑单车的选手获得总奖牌。

You query should instead be: 您查询的应该是:

 SELECT 
     Country.ISO_id, 
     Country.total, 
     Country.gold, 
     Country.silver, 
     Country.bronze, 
     Country.country_name 
FROM Country 
WHERE Country.ISO_id = 'JPN' OR Country.ISO_id = 'USA'  

And your table shouldn't have a column for the cyclist name anymore. 而且您的表格中不应再有骑单车名称的列。

   for(var i = 0; i < jsonArr.length; i++) { 
        tr = $('<tr/>');
        tr.append("<td>" + jsonArr[i].country_name + "</td>");
        tr.append("<td>" + jsonArr[i].ISO_id + "</td>");
        tr.append("<td>" + jsonArr[i].gold + "</td>");
        tr.append("<td>" + jsonArr[i].silver + "</td>");
        tr.append("<td>" + jsonArr[i].bronze + "</td>");
        tr.append("<td>" + jsonArr[i].total + "</td>");
        $('table').append(tr);//appending to html table
    }

(and you should remove said column from the table headers as well :P) (并且您也应该从表标题中删除该列:P)

    <table style="border: 1px solid black">
            <tr>
                <th>Country name</th>
                <th>Country ID</th>
                <th>Gold Medals</th>
                <th>Silver Medals</th>
                <th>Bronze Medals</th>
                <th>Total Medals</th>
                <th>Athlete Name</th>
            </tr>           
        </table>

I think it's better you program two distinct queries for your problem - a query to the countries and other to the athletes - because the main table it's for the Country. 我认为最好为您的问题编写两个不同的查询-向国家/地区查询,向运动员查询-因为主表是针对国家/地区的。

Whatever, try to use the GROUP BY query. 无论如何,请尝试使用GROUP BY查询。

$sql="SELECT Cyclist.name, Country.ISO_id, Country.total, Country.gold, Country.silver, Country.bronze, Country.country_name 
FROM Country JOIN Cyclist ON Country.ISO_id=Cyclist.ISO_id 
WHERE Country.ISO_id = 'JPN' OR Country.ISO_id = 'USA' GROUP BY Country.ISO_id";

EDIT 编辑

Oh, I forgot to explain why program two queries. 哦,我忘了解释为什么要编写两个查询。 Just think about this: you want a query to give you only two rows but also want to get all athletes names after this. 试想一下:您希望查询仅给您两行,但同时也希望获得所有运动员的姓名。 I think it can't be achieved. 我认为这是无法实现的。 Do you agree? 你同意吗? But you can get all athletes names joined with countries infos with this: 但是,您可以通过以下方式获得所有运动员姓名以及国家/地区信息:

$sql="SELECT Cyclist.name, Country.ISO_id, Country.total, Country.gold, Country.silver, Country.bronze, Country.country_name 
    FROM Cyclist LEFT JOIN Country ON Country.ISO_id=Cyclist.ISO_id 
    WHERE Country.ISO_id = 'JPN' OR Country.ISO_id = 'USA'";

And, of course, that won't be only two rows. 而且,当然,不仅仅是两行。

A simple solution, is to just test what you last used as the country and then for each repeated county, you just output empty cells for the Country and ISO columns in your table. 一个简单的解决方案是仅测试您上一次用作国家/地区的名称,然后针对每个重复的县/地区,在表中的“国家/地区”和“ ISO”列中输出空单元格。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
    var textArr='<?php echo $rowsArr?>';  // php write into javascript, as jsontext 
    $(document).ready(function(){ 
        var jsonArr = JSON.parse(textArr); //make jsontext into json array
        var tr;
        var last_ctry = '';
        for(var i = 0; i < jsonArr.length; i++) { //there will be more countries, so i cant hardcode 2 as the loop end

            tr = $('<tr/>');
            if ( last_ctry != jsonArr[i].country_name ) {
                tr.append("<td>" + jsonArr[i].country_name + "</td>");
                tr.append("<td>" + jsonArr[i].ISO_id + "</td>");
                last_ctry = jsonArr[i].country_name;
            } else {
                tr.append("<td>&nbsp;</td>");
                tr.append("<td>&nbsp;</td>");
            }

            tr.append("<td>" + jsonArr[i].gold + "</td>");
            tr.append("<td>" + jsonArr[i].silver + "</td>");
            tr.append("<td>" + jsonArr[i].bronze + "</td>");
            tr.append("<td>" + jsonArr[i].total + "</td>");
            tr.append("<td>" + jsonArr[i].name + "</td>");
            $('table').append(tr);//appending to html table

        }
    });
</script>

it's me again. 又是我。 I learn a bit more and after read this topic again I have a better solution to your problem. 我学到了一些东西,再次阅读本主题后,我对您的问题有了更好的解决方案。 If you really want to get those names, you'll need to use the MySQL function GROUP_CONCAT (or anything similar in other languages). 如果您确实想获得这些名称,则需要使用MySQL函数GROUP_CONCAT(或其他语言的类似功能)。 Your query would be: 您的查询将是:

$sql="SELECT GROUP_CONCAT(Cyclist.name ORDER BY Cyclist.name ASC) AS `cyclist_names`, Country.ISO_id, Country.total, Country.gold, Country.silver, Country.bronze, Country.country_name 
FROM Country LEFT JOIN Cyclist ON Country.ISO_id=Cyclist.ISO_id 
WHERE Country.ISO_id = 'JPN' OR Country.ISO_id = 'USA' GROUP BY Country.ISO_id ";

You'll get all those names on a single string. 您将在一个字符串上获得所有这些名称。 To get that with javascript, you'll put this line somewhere in the Table Row factory. 要使用javascript来实现,请将此行放在Table Row工厂中的某个位置。

var names = jsonArr[i].names.split(','); // that will be an array

Hope everything is fine. 希望一切都很好。 If you actually found the answer, tell us, hehe. 如果您确实找到了答案,请告诉我们,呵呵。

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

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