2017-04-14 18 views
0

私はゲームリーグ用のダイナミックHTMLテーブルを作成しています。私が抱えている問題は、その特定のゲームに投稿されたスコアがある場合にのみテーブルの列が描画されることです。スコアがない場合、トーナメントの初めに言うと、テーブルは全くありません。MYSQLクエリを2つの部分に分割しようとしています

スコアがあるかどうかにかかわらず、私のゲームリストからテーブルのヘッダーを作成するようにクエリを分割する必要があります。

// Write query to retrieve tournament games 
$strSQL = "SELECT nfojm_djc2_items.name AS game, nfojm_djc2_items.rom AS rom, nfojm_users.username AS name, submit_score.gamescore AS score, nfojm_comprofiler.avatar AS avatar 
FROM nfojm_djc2_items, nfojm_users, submit_score, select_game, nfojm_comprofiler 
WHERE submit_score.gameID = select_game.id 
AND select_game.gamecatalogID = nfojm_djc2_items.id 
AND submit_score.userID = nfojm_users.id 
AND submit_score.userID = nfojm_comprofiler.user_id 
AND submit_score.tournID = $tournID 
ORDER BY submit_score.gamescore DESC"; 

// Execute the query. 
$query = mysqli_query($con, $strSQL); 

while ($row = mysqli_fetch_array($query)) { 

    if (!in_array($row["game"], $games)) { 
     $i=0; 
     $indexes[$row["game"]] = 0; 
     $games[] = $row["game"];  
    } else {  
     $indexes[$row["game"]]++; 
     $i = $indexes[$row["game"]]; 
    } 

    if (!in_array($row["rom"], $roms)) { 
     $i=0; 
     $indexes[$row["rom"]] = 0; 
     $roms[] = $row["rom"];  
    } else {  
     $indexes[$row["rom"]]++; 
     $i = $indexes[$row["rom"]]; 
    } 

    $scores[$i][$row["game"]] ="<div style='float:left; margin-right:7px;'><img src='http://www.arcadeicons.com/images/comprofiler/" . $row["avatar"] . "' height='35' width='35' style='border-radius:50%'></div>" . $row["name"] . "<br><h3>" . $row["score"] . "</h3>"; 

} 
// Close the connection 
mysqli_close($con); 

print ('<table><tr><td><IMG SRC="http://www.arcadeicons.com/images/jem/venues/small/'.$loc_image.'" style="height:86px; margin-right:8px; border-radius:5%"></td>'); 
print ('<td><p style="text-align: left; margin-bottom:15px;"><h1 style="color:#222222;">'.$tourn_name.'</h1></p>'); 
print ('<p style="text-align: left;"><h3>'.$venue_name.' , '.$city_name.' , '.$state_name.' , '.$country_name.'</h2></p>'); 
print ('<p style="text-align: left;"><h3>'.$tourn_date.'</h2></p></td></tr></table>'); 


// PUT SCORES TO SCREEN 
// GAMES 
print('<table class="rounded" style="border:2px solid #999999;"><thead><tr>'); 

foreach ($roms as $rom) { 
    print("<th class='block'><div style=\"width:130px;height:35px;background-image: url(http://www.arcadeicons.com/media/marquees/{$rom}1.png);background-size:cover;background-position:center center;\"></div></th>"); 
} 
print("</tr></thead><tbody><tr>"); 
foreach ($games as $game) { 
    print("<td class='block' style='text-align:center; background-color:#444444; color:white;'><h4>{$game}</h4></td>"); 
} 

print("</tr>"); 

foreach ($scores as $score) { 

    print("<tr>");   

     foreach ($games as $game) { 

     if (isset($score[$game])){ 
      $ps = $score[$game]; 
     } else { 
      $ps = ""; 
     } 

     print("<td style=\"padding-left:8px;\" class=\"block\"><h4>{$ps}</h4></td>"); 
    } 
} 

print("</tr></tbody></table>"); 

結果が掲載されている場合は、このように表示されます。それはスコアのないものも含め、すべてのレコードを選択するように

picture of table when rendered

+0

あなたのコードは[** SQLインジェクション攻撃**](https://en.wikipedia.org/wiki/SQL_injection)の脆弱性が高い可能性があります。 [** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https://secure.php.net/)を使用してください。 manual/en/pdo.prepared-statements.php)は、[** this post **](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql - インジェクション - イン - php)。 –

+0

使用しているフォームではなく、JOINSを使用してクエリを書き換えます。レコードがない可能性のあるテーブル(つまりスコアがまだない)については、LEFT JOINを使用し、これらのテーブルのカラムに対しては、IFNULL関数を使用してデフォルト値(つまり、0または "no score")を返します。 –

答えて

0

私はそれを除いて、クエリを再書いたあなたのby句句と順序が両方のスコアテーブルから列を参照。

レコードを含めるために何を使用するかを決定し、where句を適切に変更し、order by句に追加する必要があります。

他のテーブルのいずれかでトーナメントを識別する欄があるとしますか?

SELECT i.`name` AS `game`, 
    i.`rom` AS `rom`, 
    u.`username` AS `name`, 
    IFNULL(s.`gamescore`,'No Score Yet') AS `score`, 
    p.`avatar` AS `avatar` 
FROM `nfojm_djc2_items` i 
JOIN `select_game` g 
    ON g.`gamecatalogID` = i.`id` 
JOIN `nfojm_users` u 
    ON s.`userID` = u.`id` 
JOIN `nfojm_comprofiler` p 
    ON u.`id` = p.`user_id` 
LEFT JOIN `submit_score` s 
    ON s.`gameID` = g.`id` 
     AND s.`userID` = u.`id` 
WHERE s.`tournID` = '$tournID' 
ORDER BY s.gamescore DESC; 
+0

ありがとうございました。私はMYSQLを使用していますが、私はphpMYAdminでこのクエリを実行しようとしましたが、構文エラーを投げかけています。これは別の形式ですか? –

+0

構文エラーとは何ですか?それがどこにあるか教えてください。 –

+0

phpAdminを使用している場合、値$ tournIDを値に置き換える必要があります。 –

関連する問題