Jump to content
Larry Ullman's Book Forums

Antonio Conte

Members
  • Posts

    1084
  • Joined

  • Last visited

  • Days Won

    126

Posts posted by Antonio Conte

  1. (Actually, the truth is, I don't know any way beyond using a while loop to go through MySQL query results in PHP. Larry, perhaps you, or someone else could enlighten us on this.)

     

    The query result is always in array form. That is why it's nice to use while loops. As mysql(i)_fetch_array will return NULL when there's no more results, this is the same code in practice:

     

    while ( ($row = mysql_fetch_array($result, MYSQL_ASSOC)) != NULL ) 
    {
      echo $row['name'];
    }

     

    A For-loop could be constructed this way:

     

    $num = 1; // number of times
    
    for ($i=0; $i<$num; $i++) 
    {
      $row = mysql_fetch_array($result, MYSQL_ASSOC)
      echo $row['name'];
    }

  2. In short, a third table. Players, Games, Game_stats.

     

    - On for players

    - On for games (foreign keys for clubs, league and season)

    - On for stats (foreign keys from players and games

     

    What I do, is keep all tables (around 15) on INF3 or Boyce Codd, allow for very little redudancy, and make sure the data has integrity. (Non to little chance mispelling etc)

     

    Then I write large views that join all these pieceses of information together for me.

     

    The view for game stats has season id, season name, league id, league name, home and away team, player id, player name and match time. That way I can write really easy select queries with what I want to display, and still query things like WHERE (season_id = 8) AND (player_id = 10) ORDER BY gametime desc LIMIT 5. (the posibilities are many)

     

    I actually list very many of my tables in a post in this thread. It does not show relationship, but the foreign keys give you a lot of info of how it is done. :)

     

    -------------------------------------

     

    By the way. If you have many-to-many relationships, that is often a sign that you need another table. Redudancy also gives you away. (I have a small table with nicknames for football players. It's only five-six rows of info, and uses the same player_id as foreign key to the players table)

  3. Anyway, if Larry or someone else wouldn't mind piping in, I'm still curious as to the best way to handle many-to-many relationships, like the one that exists here between players and games (i.e., a player can be in multiple games (matches), and a game can have multiple players).

     

    Sorry. Don't quite understand what you are trying to say here. But I'll make a shot at it.

     

    You could create some very much alike my table structure, but use an incremented ID, and use the player and match id as unique. That would allow you to save ANY information about that player in that match.

     

    Understand? Besides that, there is really not much to do to improve the structure.

     

    Eighter way. Keep it simple.

  4. A common technique used is to create an error array. This can later be used BOTH to check if the user can PROCEED to the next page AND to DISPLAY errors.

     

    This is how you do it:

     

    $errors = array(); // Create the array in the computers memory
    
    if (!isset($_POST[something]) or empty($_POST[something]) 
    {
      $errors[] = "Error message";
    }
    else 
    {
      // All is good. Data is validated.
    }
    
    if ( $errors == null ) // There is no breakage of the validation rule. Data and LIFE is good!
    {
      // The errors array has NO errors. Therefor we can redirect the user with header. 
    }
    else // This will display all errors from the bad data
    {
      foreach ( $errors as $msg ) {
     	echo $msg; 
      }
    }

  5. Everyone starts out a noob. I could't understand why the error messages were even important in the beginning. They made no sense to me...

     

    After reading this book by Larry, I think you will evolve very much as a PHP programmer. It's not the most technical. It does not give you wings to fly with. But it's a very good book that makes you understand PHP. That is why no-one even come close to Larry as a writer for me, even though I've learned more advanced stuff elsewhere too. Larry's advanced book also teaches you to handle a lot of common problems and gives you a nice poker hand of solutions than may come in handy.

     

    Read his books one time, try some of it, read it quicker a second time, and you will see progress.

     

    It's actually a promise...

  6. You and I both have a low amount of post here. But as it's quite a small forum, I see the same users contributing over and over again. All of them seem to be nice people - that is - if you are polite, follow the rules, etc. I believe, as Jonathon, that no-one here is checking your domain for the wrong reason.

     

    I like this forum at lot. Small number of members, a very active group of users that talk a bit personal. Might be exactly nerdy enough to fit this forum but;

     

    "Part of the ship, part of the crew..." :P

     

    You tend to stay here after some post because of the nice and very throughly help!

  7. When getting the information inside the while-loop, do you use something like:

     

    $row0[0] or something like that? $row0 is the name of the variable and as your fetch_array uses numbers, that is the only option.

     

    With mysqli_fetch_array($r0, MYSQL_ASSOC) you could use the names of the coulmns queried from the DB. I don't know if this is your problem, but it's an easy mistake.

     

    If you use MYSQL_BOTH with mysql_fetch_array, you are allowed to use both representations

    Edit: Might also be a good idea to drop the @-symbol to see any errors generated from mysqli_query.

  8. I actually solved it myself!

     

    Thanks anyway Hartleysan. You have had some really good suggestions and questions that helped me a great deal. :)

     

    ------------------------------------

     

    The script:

     

    To get the game:

    echo '<select name="giornata">';
    
    while($games = mysql_fetch_array($game_result, MYSQLI_ASSOC)) 
    {
    	echo '<option value="'.$games[id].'"> '.$games[hometeam] .' - '. $games[awayteam].'</option>';
    }
    
    echo '</select>';

    To get the players ID and output form to save stats to them:

     

    $i = 0;
    
    while( $row = mysql_fetch_array( $result, MYSQLI_ASSOC ) ) 
    {			
    	echo '
      		<input type="checkbox" name="player['.$i.'][id]" value="'.$row[id].'"  /> '.$row[name].'
    	<input type="text" name="player['.$i.'][time]" value="" maxlength="2" />
    	<select name="player['.$i.'][sub]">
    		<option value="0">Full match</option>
    		<option value="1">Subbed Inn</option>
    		<option value="2">Subbed Out</option>
    	</select>';
    	$i++;
    }

     

    To display (or insert) the data from the players:

     

    	
      foreach ($_POST[player] as $player) {
    	echo  'INSERT INTO abc_players_matches ( match_id, player_id, min, sub) VALUES  ('.$_POST[giornata].', '.$player['id'].', '.$player['time'].',  '.$player['sub'].'); <br />';
    }
    

    What it now produces (it's correct!!!):

    INSERT INTO abc_players_matches ( match_id, player_id, min, sub) VALUES (857, 1, 30, 0);

    INSERT INTO abc_players_matches ( match_id, player_id, min, sub) VALUES (857, 13, 40, 1);

    INSERT INTO abc_players_matches ( match_id, player_id, min, sub) VALUES (857, 30, 50, 2);

    etc...

     

    -----------------------------------

    The database

     

    ...is quite large with a lot of many-to-one and many-to-many relationships. That is not a problem.

     

    Seasons:

    season (id, name)

    Leagues:

    league (id, name, season*, total_rounds, games_per_round)

     

    Clubs:

    clubs (id*, country*, name, logo, stadium_name)

     

    Games:

    games (id, league_id*, round, club1_id*, club2_id*, kickoff_time, goals_club1, goals_club2)

     

    Players:

    players (id, number, firstname, lastname, birthplace, country*, birthdate, position)

     

    Game stats by players:

    stats_players_games (match_id*, player_id*, league_id*, time, sub)

     

    + A lot of other tables for country names, country codes, player biographies, table calculation, etc. They are not really that interesting here.

    Views:

     

    To display game stats:

    view_players_games (match_id*, league*, season*, hometeam_name, awayteam_name, player_id*, player_name, sub, kickoff*)

     

    --------------------------------

     

    The views link together all information about seasons, leagues, clubs, games, players and everything else. I only need to fill stats_players_games with info about the players attending the match.

     

    The same script will be modified and used to save stats about (on a per-game-setting):

    - Goals

    - Yellow cards

    - Red cards

    - Goal assists

     

    It would be possible to save all information at once. But as that adds several layers of complexity, I will start with saving players in each match to start. Adding stats such as goals and cards are a lot less work.

    -----------------------------------

    I posted this anyway as someone else might need to do something similar. (Not that unlikely with movies, books, cars, or other stuff) :)

  9. First of all:

     

    Sorry for the long post. This can seem VERY much more complex than I think it is. English is not my main language, so there you go. Please ask if you need clarification of something. :)

     

    I am a little confused by what you want, but it seems like the while loop should handle everything for displaying the information, and you don't need a foreach loop.

     

    The while loop is working perfectly. But they are only needed to get the ID of the game and the SELECTED players ID from the form. This form will THEN create number of minutes and substitution status bases on input fields connected to every player.

     

    Do you want a separate form for each player, or do you want a single form for all the players?

     

    That is not important. As long as the input from the form can be linked with THAT spesific player, it does not matter.

     

    Also, is this for a back end that can be used for editing player stats?

     

    Exactly! :)

     

    The players ID and game ID is used for saving information regarding that player.

     

    If that's the case, I'd probably use standard input fields for both the player number and how many minutes they've played

     

    Then, I'd make a drop-down list for the three substitution options.

     

    Is that not what you want? Sorry, but a bit confused..

    The number of minutes is a standard input field (at least I thought so.) The players ID cannot be as those using the backend might not know the ID of every player (which may differ from their shirt number)

     

    The drop down list for substitution is genius though. Will definitely use that.

     

    I will write in list form. Probably make it a bit more clear.

     

    This is what I want:

    - Saving players stats over whole seasons.

    - I need the game ID, the players ID, the minutes played and substitution info on every player for every game of the season.

     

    The structure of the table look like this:

    stats (game_id*, player_id*, minutes, substitution)

     

    Underscore: Primary key

    Asterix (*): Foreign keys

    What I already have:

    - Tables for player information

    - Tables for league, clubs, teams and matches.

     

    What I want with the data: (And is possible through getting this I'm working on right)

    - Query the number of games of each player (witch will again be used to calculate other stats. Like goals per game, bookings per game, etc)

    - Query the number of times a player is substituted onto the pitch

    - Query how many minutes a player has had in a season, last five games, previous game

    - A lot more

     

    Screenshot of the backend:

     

    921511.jpeg921512.jpeg

  10. Actually. Not quite solved yet.

     

    This is the case:

     

    I need to create a multi-dimensional array with the following example information:

     

    $players = array(
    	0 => array("match_id" => 855, "player_id" =>  1, "time_played" => 90 , "sub" => 0), 
    	1 => array("match_id" => 855, "player_id" => 11, "time_played" => 45 , "sub" => 1),
    	2 => array("match_id" => 855, "player_id" =>  7, "time_played" => 45 , "sub" => 2),
    	3 => array("match_id" => 855, "player_id" =>  9, "time_played" => 90 , "sub" => 0)
    );

     

    The match_id is not important, but I need the players ID, How many minutes they've played (standard is 90), and their substitution status (0 is full match, 1 in subbed inn, 2 is subbed out).

     

    How can I manage this with input lists and checkboxes in Xhtml? The code for getting player id, time played and sub status looks like this:

     

    	// The player's ID is queried from the DB before this.
    
    while($row = mysql_fetch_array($result, MYSQLI_ASSOC)) {
    
    	echo '<div style="max-width: 300px width: 300px;"><input type="checkbox" name="player[][id]" value="'.$row[id].'"  /> '.$row[name].'</div>
    	<input type="text" name="player[][time]" value="90" maxlength="2" />
    	<div>INN? <input type="checkbox" name="player[][inn]" value="1" /></div>
    	<div>UT? <input type="checkbox" name="player[][out]" value="2" /></div>
    	<br />';
    
    }

     

    And how would the foreach work with this? I'm not (any) good with arrays.

  11. Hey

     

    I'm currently saving lots of football statistic in a database. I need to dynamically create INSERT QUERIES with the MATCH ID and PLAYER ID's of the footballers attending the match.

     

    There are possibly 14 players in a football match (11 starters and 3 substitues). I therefor need to query ALL players in the database 14 times

     

    The Player's ID's is selected from another table in the database. All Player ID'S are displayed in a html option list like this:

     

    $number_of_players = 14;
    
    echo '<form enctype="multipart/form-data" action="" method="post">';
    
    for ($i=0; $i<$number_of_players; $i++)
       {
    	$result = mysql_query($query, $connect);
    	echo '<select name="player.$i.'">';
    		while($row = mysql_fetch_array($result, MYSQLI_ASSOC)) 
           	{
    			echo '<option value="'.$row[id].'">'.$row[name].'</option>';
    		}
    	echo '</select> <br /> <br />';
    }
    echo '<input type="submit" name="submit" value="Find IDs" />
    </form>';

     

    This produces a list of 14 HTML Select with NAME player+$i (player0, player1, player2, player3, etc). This creates global $_POST["players'.$i.'"]

     

    for ($i=0; $i<$antall; $i++) {
      echo 'INSERT INTO abc_players_matches ( match_id, player_id, sub) VALUES ('.$match.', '.$_POST["player'.$i.'"].' , 0);' . '<br />';
    }

     

    The problem is that $_POST["players'.$i.'"] is empty. What am I doing wrong?

     

    The simple interface is looking like this:

  12. table promo_codes with discount codes. Each discount code only works for a specific item (client's request). So each row in the table consists of :

    id

    name (like 'SALE' or whatever)

    amount

    expires (datetime)

     

    Now, thinking ahead a little, chances are the client is going to name 75% of the codes with such original names like 'SALE' 'PRESALE' and 'EARLYBIRD'

     

    At first glance this looks like a violation of 2NF, because the table has many rows with repeating data in the same column (a non-key which is not dependent on the primary key).

     

    You could reduce redudancy and remove data integrity problems (even if it would not become and acutuall PROBLEM) this ways:

     

    promo_codes (id, amount, expires)

    promo_coupon_names (coupon_names_id, name)

    promo_names (code_id, coupon_names_id*)

     

    - All coupon names are stored in promo_coupon_names. promo_names are where you link the actual coupons with the names they should use.

     

    You are right about violation of 2NF. You are also right about why it's a violation.

     

    Here however, the multiple instances of the code 'PRESALE' are not actually related at all, they are different codes, on different items, which just happen to share a common name.

     

    Not buying this. Stick with your original thought. However, this is JUST a matter of redundancy, and something you absolutely can live with. My great teacher says you should strive to make things Boyce-Codd (INF3), but sometimes you accept a little redundancy because it makes your life easier. This falls under that category. Fix it if you like and want to, but it won't affect your system much. Coupons are small in numbers, so the extra data is not something to worry to much about.

     

    Does this mean the database is completely safe from data integrity problems even with 3000 (different) instances of 'PRESALE'?

     

    Perhaps, maybe not. If you are ever going to allow users to search for items that are on sale, a misspelled "PERSALE" could possibly not be found by the query. That is an integrity problem. I have a solution on have to fix this above, but it's of course a minor problem.

×
×
  • Create New...