Guest User

Untitled

a guest
Jul 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <!--
  2. This example uses jquery to hide/show the teams players without refreshing the page.
  3. All the data is retreived at the 1st execution and is then shown/hidden by clicking on the team name.
  4. Would probably need some formating etc to make it look good :D
  5. -->
  6. <?php
  7.   //Connect to your database
  8.   $sql_server='localhost';
  9.   $sql_db='teams';
  10.   $sql_user='root';
  11.   $sql_pass='23011809';
  12.   mysql_connect($sql_server,$sql_user,$sql_pass);
  13.   mysql_select_db($sql_db);
  14.  ?>
  15. <html>
  16.   <head>
  17.     <title>Teams</title>
  18.     <script src="jquery-1.4.4.js"></script>
  19.   </head>
  20.   <body>
  21.     <?php
  22.     //Select every row from teams
  23.       $sql = mysql_query("
  24.                         SELECT *
  25.                         FROM teams
  26.                     ") or die(mysql_error());
  27.       //Loop through each row
  28.       while($row = mysql_fetch_array($sql))
  29.       {
  30.       ?>
  31.       <!--Make a jscript for toggling the players -->
  32.         <script>
  33.           $(document).ready(function(){
  34.             $('.<?php echo $row['team']; ?>').click(function(){
  35.                 $('.<?php echo $row['team']; ?>_players').toggle();
  36.             })
  37.           });
  38.         </script>
  39.         <?php
  40.         //Echo the team name
  41.         echo '<a class="'.$row['team'].'">'.$row['team'].'</a><br />';
  42.         //Echo the players (Will be hidden on page load)
  43.         echo '<p class="'.$row['team'].'_players" style="display:none;">';
  44.         //Turn the cvs from db into an array.
  45.         $players=explode(',',$row['players']);
  46.         //Because players are stored as a cvs in database, loop through each player from the new array.
  47.         foreach($players as $player) echo $player .'<br />';
  48.         echo '</p>';
  49.       }
  50.       ?>
  51.   </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment