Advertisement
nailacrooc12

teams

Jun 9th, 2023
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. <?php
  2.     include 'connection.php';
  3.  
  4.     $condition = "";
  5.     $keyword = "";
  6.     if(isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  7.         //* QUESTION #6.d TODO here
  8.         $keyword = $_GET['keyword'];
  9.         $condition = "WHERE teams_tbl.name LIKE '%$keyword%' OR teams_tbl.abbr LIKE '%$keyword%'";
  10.     }
  11.  
  12. //* QUESTION #6.a TODO here
  13. // Prepare the query statement
  14. $statement = "SELECT teams_tbl.id, teams_tbl.name, teams_tbl.abbr, COUNT(players_tbl.id) AS player_count
  15.              FROM teams_tbl
  16.              JOIN players_tbl ON teams_tbl.id = players_tbl.team_id
  17.              $condition
  18.              GROUP BY teams_tbl.id, teams_tbl.name, teams_tbl.abbr, teams_tbl.division
  19.              HAVING COUNT(players_tbl.id) > 0";
  20.  
  21.  
  22. // Run the query statement
  23. $teamResults = mysqli_query($conn, $statement);
  24.  
  25. //* QUESTION #6.b TODO here
  26. // Get the number of teams through the result set returned
  27. $teamCount = mysqli_num_rows($teamResults);
  28. ?>
  29. <!DOCTYPE html>
  30. <html lang="en">
  31.     <head>
  32.         <meta charset="utf-8">
  33.         <meta name="viewport" content="width=device-width, initial-scale=1">
  34.         <link href="assets/css/bootstrap.min.css" rel="stylesheet">
  35.         <link href="assets/css/custom.css" rel="stylesheet">
  36.         <title>NBA Players</title>
  37.     </head>
  38.     <body>
  39.         <?php
  40.             include_once 'partials/header.php';
  41.         ?>
  42.         <div class="container-fluid mt-3">
  43.             <div class="container">
  44.                 <nav style="--bs-breadcrumb-divider: url(&#34;data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Cpath d='M2.5 0L1 1.5 3.5 4 1 6.5 2.5 8l4-4-4-4z' fill='%236c757d'/%3E%3C/svg%3E&#34;);" aria-label="breadcrumb">
  45.                     <ol class="breadcrumb">
  46.                         <li class="breadcrumb-item"><a href="index.php">Home</a></li>
  47.                         <li class="breadcrumb-item active" aria-current="page">Teams</li>
  48.                     </ol>
  49.                 </nav>
  50.             </div>
  51.         </div>
  52.         <div class="container-fluid mt-4">
  53.             <div class="container">
  54.                 <!-- //* QUESTION #6.b MAKE THIS TEAM COUNT DYNAMIC BASED ON THE TOTAL TEAMS THAT HAVE AT LEAST 1 PLAYER STORED ON THE DATABASE -->
  55.                 <h3 class="mb-3">List of active teams (<?php echo $teamCount;?>)</h3>
  56.                 <form class="mb-3" action="?" method="GET">
  57.                     <div class="d-flex flex-row align-items-center">
  58.                         <div class="form-group me-2">
  59.                             <label>Search</label>
  60.                         </div>
  61.                         <div class="form-group me-2">
  62.                             <input type="text" class="form-control" style="width: 350px;" placeholder="Search team name or abbreviation..." name="keyword" value="<?php echo $keyword;?>">
  63.                         </div>
  64.                         <div class="form-group me-2">
  65.                             <label></label>
  66.                             <button class="btn btn-primary">Search</button>
  67.                         </div>
  68.                         <?php
  69.                             if(isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  70.                                 echo '
  71.                                    <div class="form-group">
  72.                                        <label></label>
  73.                                        <a class="btn btn-secondary" href="teams.php">Clear search</a>
  74.                                    </div>
  75.                                ';
  76.                             }
  77.                         ?>
  78.                     </div>
  79.                 </form>
  80.                 <table class="table table-light table-striped">
  81.                     <thead>
  82.                         <tr>
  83.                             <th class="table-dark" scope="col">#</th>
  84.                             <th class="table-dark" scope="col">Abbreviation</th>
  85.                             <th class="table-dark" scope="col">Name</th>
  86.                             <th class="table-dark text-center" scope="col">Total Players</th>
  87.                             <th class="table-dark" scope="col"></th>
  88.                         </tr>
  89.                     </thead>
  90.                     <tbody class="table-group-divider">
  91.                         <?php
  92.                             if($teamCount) {
  93.                                 $i = 1;
  94.                                 while($team = mysqli_fetch_array($teamResults, MYSQLI_BOTH)) {
  95.                                     $teamLogo = file_exists('assets/teams/' . $team['abbr'] . '.png') ? 'assets/teams/' . $team['abbr'] : 'assets/logo';
  96.                                     //* QUESTION #6.c TODO here
  97.                                     // SHOW ALL TEAMS HERE USING TEMPLATES ABOVE
  98.                                     echo '<tr>
  99.                                            <th scope="row">'.$i.'</th>
  100.                                            <td><img src="'.$teamLogo.'.png" height="36">' . $team['abbr'] . ' </td>
  101.                                            <td>' . $team['name'] . '</td>
  102.                                            <td class="text-center">' . $team['player_count']  . '</td>
  103.                                            <td class="text-center">
  104.                                                <a class="btn btn-primary" href="team_profile.php?id=' . $team['id'] . '">View Team</a>
  105.                                            </td>
  106.                                        </tr>';
  107.                                     $i++;
  108.                                 }
  109.                             }
  110.                         ?>
  111.                     </tbody>
  112.                 </table>
  113.             </div>
  114.         </div>
  115.     </body>
  116.     <script src="assets/js/popper.min.js"></script>
  117.     <script src="assets/js/bootstrap.min.js"></script>
  118. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement