이것은 설명하기 어려울 수도 있지만 최선을 다할 것입니다.Json 배열에서 HTML 테이블로 특정 수의 행만 출력
두 개의 데이터베이스가 있습니다. 하나는 국가 이름이고 다른 하나는 운동 선수 이름입니다.
$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' ";
: 이 두 테이블은 국가 ID 인 속성 ISO_id (즉, FRA, JPN, 미국 등)
나는 다음과 같은 SQL 문이 두 테이블을 조인하고 의해 연결되어
미국의 경우 하나, 일본의 경우 하나만 국가 속성 만 표시하는 두 행의 표를 인쇄하려고합니다. 나중에 버튼을 클릭하면 국가의 모든 자전거를 보여주는 표를 만들기 위해 자전거 속성이 필요할 것입니다.
국가의 특성을 인쇄 할 때 국가의 운동 선수 수만큼 HTML 표에 넣어집니다. 미국에 15 명의 선수가 있다면 country 테이블은 미국 국가 특성의 15 배를 표시합니다.
어떻게 해결할 수 있습니까?
내 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>
내 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>
테이블에 참여하지 않으려는 경우에도 문제가 해결됩니다. –