Forum Moderators: coopster

Message Too Old, No Replies

Creating a New MySQL Database

How to achieve optimal design

         

mn1dbp

7:28 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



I run a small horse racing based website that has a 'fantasy league' type competition each season. Last year i looked after all the entries, leaderboards etc.. using excel and copying and pasting to webpages each week.

However this year i am going to attempt to use MySQL and PHP to have it automatic bar result entry. I have built a basic php and MySQL site before with the usual insert, edit etc.. But this one will need features im not sure how i go about making.

Basically there will be a list of horses (around 150) divided into 4 grades. Entrants pick 2 horses from each grade so a total of 8 horses. Winning horses score 5, 2nd = 3 and 3rd =1.

I was thinking of having the following 2 tables - one for the horses - listing their name and grade. One for entrants - the peoples contact details and their chosen horses.

However from there im not sure how to able to enter the latest results and how i can create a leaderboard page and list the points each horse has. I was thinking of a new table with a row for each result showing horse name and points scored but i then dont't know how this table can be used to create a column on the horse table that lists each horses total points. And also how it can be used to create the total points each entranant has got so far (i.e. the total point for all 8 of their horses)

ironik

9:23 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



I think you may need 4 tables to get exactly what you are after:

HORSES
horse_id
horse_name
horse_grade

ENTRANTS
entrant_id
entrant_name
horse_grade_1_1
horse_grade_1_2
horse_grade_2_1
horse_grade_2_2
horse_grade_3_1
horse_grade_3_2
horse_grade_4_1
horse_grade_4_2

RACES
race_id
race_info <whatever other info relevant to the result>

RESULTS
result_id
race_id
horse_id
result_points

Then if you want to find which horse has the highest amount of points you can use SELECT and GROUP BY and query multiple tables

mn1dbp

11:37 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



Many thanks for replying. I think it may be a slow process.

I have created the first two tables. However i have come across my first problem. I assume in the ENTRANTS table for horse_grade_1_1 etc... we want the horse_id from the first table rather than the actual horses name.

How is it best to create a page to insert data into the ENTRANTS table. For example at the moment i have the code (within a form and already connected to the DB):

<select name="horse_1">
<option selected></option>
<?
$query = "select * from a where horse_grade='bn' ";
$result = mysql_query($query) or die("");
$i=0;
while ($row= mysql_fetch_array($result))
{$horse=mysql_result($result,$i,"horse_name");
$id=mysql_result($result,$i,"horse_id");?>

<option>
<? echo "$id" ;
echo " $horse";
echo "</option>";
++$i;
}?>
</select>

This however of course sends both the horse_id and horse_name forward as $horse_1. I guess what we are looking for is displaying the horse name to users but only forwarding the ID for the insert statement.

ironik

12:30 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Change this portion and populate the 'value' attribute for the option tag:

<option value="<?php echo $id;?>"><?php echo $horse?></option>

Then when you submit the form the $_POST variable will be submitted the id, but the users only see the horse name.

Try to use the <?php opening tag instead of the <? shorthand tag wherever possible. As far as I am aware the shorthand version doesn't receive 100% support on by different parsers.

mn1dbp

2:30 am on Mar 15, 2005 (gmt 0)

10+ Year Member



Many thanks once again.

That has worked a treat. I will carry on with table three and the next stage tommorow. I shall no doubt get stuck again at some point.

Will also try to remember to use <?php tag in future. Quick replace and find has sorted the old page.

mn1dbp

11:58 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



I am still stuck with the next table.

I have tried to figure out what table i need to create where i can input the 3 horses that score points in each race. Would i need to insert each horse on a different row with the number of points it got. e.g. do 3 inserts for each race?

ironik

1:14 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I'd probably do it like that, with the results for each horse stored seperately, however you could add a column for each horse id and points (need 6 columns). If you know that there are only 3 horses that will score points in each race, then you could use that method quite safely, I'm not sure if it follows best practice for normalisation though.