473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

I am trying to write a database for my cub scout unit..

I am trying to build a database for my scout pack that will allow me to search each scouts awards and achievements and list those achievements on an website... Each scout has multiple achievements and awards and I want to list each one with scout name / award name / date earned / date received..

I am trying to get it to look something like this

Expand|Select|Wrap|Line Numbers
  1. Name            Award Name          Date Earned    Date Received
  2. Ryan James      Webelos Badge       2009-05-22     2009-06-14
  3.                 Bobcat Badge        2007-04-09     2009-05-04
  4.                 Arrow of Light      2010-02-10     2010-02-22
  5.  
  6. Mike Toliver    Bobcat Badge        2007-07-21     2007-10-14

and so on... I just need the information to be pulled from different tables

How do I organize the tables to let me access a scout name and then every award that scout has earned...

Please help!!!

Thanks for your assistance in this matter

Bill
May 9 '10 #1
5 2737
Atli
5,058 Expert 4TB
Hey.

I would recommend a typical many-to-many (N:M) structure, like this:
Expand|Select|Wrap|Line Numbers
  1. +--------+      +-----------------+     +---------+
  2. | scouts |      | scout_awards    |     | awards  |
  3. +--------+      +-----------------+     +---------+
  4. | id (PK)|>---->| scout_id (PK,FK)|  |-<| id (PK) |
  5. | name   |      | award_id (PK,FK)|<-|  | name    |
  6. | etc... |      | earned          |     | etc...  |
  7. +--------+      | received        |     +---------+
  8.                 +-----------------+
It allows you to simply list all the scouts in the "scouts" table, all the awards in the "awards" table, and then link each scout to their respective awards via the intermediary table "scout_awards".

You can then list all the awards each scout has by joining the three tables. Somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT  s.`name` AS 'scout',
  2.         a.`name` AS 'award',
  3.         sa.`earned`,
  4.         sa.`received`
  5. FROM    `scouts` AS s
  6. INNER JOIN `scout_awards` AS sa
  7.     ON  sa.`scout_id` = s.`id`
  8. LEFT JOIN `awards` AS a
  9.     ON  a.`id` = sa.`award_id`
Which would give you a simple table of data:
Expand|Select|Wrap|Line Numbers
  1. +--------------+----------------+---------------------+---------------------+
  2. | scout        | award          | earned              | received            |
  3. +--------------+----------------+---------------------+---------------------+
  4. | Mike Toliver | Bobcat Badge   | 2007-07-21 00:00:00 | 2007-10-14 00:00:00 |
  5. | Ryan James   | Webelos Badge  | 2009-05-22 00:00:00 | 2009-06-14 00:00:00 |
  6. | Ryan James   | Bobcat Badge   | 2007-04-09 00:00:00 | 2009-05-04 00:00:00 |
  7. | Ryan James   | Arrow of Light | 2010-02-10 00:00:00 | 2010-02-22 00:00:00 |
  8. +--------------+----------------+---------------------+---------------------+

You would then use whatever front-end language you are using for you website development to format this the way you want it to be. For example, using PHP, you could do this to get the exact plain/text format you posted above:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Connect to database
  3. $dbLink = new mysqli('localhost', 'user', 'passwd', 'dbName');
  4. if(mysqli_connect_errno()) {
  5.     die("Failed to connect to database: " . mysqli_connect_error());
  6. }
  7.  
  8. // Fetch all the data
  9. $sql = "SELECT  s.`name` AS 'scout',
  10.                 a.`name` AS 'award',
  11.                 sa.`earned`,
  12.                 sa.`received`
  13.         FROM    `scouts` AS s
  14.         INNER JOIN `scout_awards` AS sa
  15.             ON  sa.`scout_id` = s.`id`
  16.         LEFT JOIN `awards` AS a
  17.             ON  a.`id` = sa.`award_id`";
  18. $result = $dbLink->query($sql);
  19.  
  20. // Fetch the data and put it into an array
  21. // grouped by the scout name.
  22. $data = array();
  23. if($result && $result->num_rows > 0)
  24. {
  25.     while($row = $result->fetch_assoc())
  26.     {
  27.         $name = array_shift($row);
  28.         $data[$name][] = $row;
  29.     }
  30.     $result->close();
  31. }
  32. $dbLink->close();
  33.  
  34. // Print the data.
  35. header('Content-type: text/plain; charset=UTF-8');
  36. echo "Name            Award Name          Date Earned     Date Received    \n";
  37. foreach($data as $_name => $_awards)
  38. {
  39.     $printName = true;
  40.     foreach($_awards as $_award)
  41.     {
  42.         // Name column. Make it empty for all
  43.         // but the first award row.
  44.         if($printName) {
  45.             printf("%-16s", $_name);
  46.             $printName = false;
  47.         }
  48.         else {
  49.             printf("%-16s", "-");
  50.         }
  51.  
  52.         // Award data
  53.         printf("%-20s", $_award['award']);
  54.         printf("%-16s", date('Y-m-d', strtotime($_award['earned'])));
  55.         printf("%-16s", date('Y-m-d', strtotime($_award['received'])));
  56.         echo "\n";
  57.     }
  58. }
  59. ?>
Which prints:
Expand|Select|Wrap|Line Numbers
  1. Name            Award Name          Date Earned     Date Received    
  2. Mike Toliver    Bobcat Badge        2007-07-21      2007-10-14      
  3. Ryan James      Webelos Badge       2009-05-22      2009-06-14      
  4.                 Bobcat Badge        2007-04-09      2009-05-04      
  5.                 Arrow of Light      2010-02-10      2010-02-22      
Hope that helps.
May 17 '10 #2
Thank you for your help.. I will try this code out as soon as I can.. It looks like it will do what I need it to... again thank you..

Ok I had some trouble getting the code to work so I re-wrote the database and put all the information into one table

Now I would just like to fix the syntax of the PHP page to make the dates be -- Example--> January 30, 2010

$f2=mysql_result($result,$i,"scout_birthdate");

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2 ?></font></td>

how can I change the syntax to give me F-j-Y... thank you again for all your help.
May 17 '10 #3
code green
1,726 Expert 1GB
Take a look at the date() function.
It can format dates in many ways
May 19 '10 #4
Atli
5,058 Expert 4TB
A combination of the strtotime and the date functions can do that.
Expand|Select|Wrap|Line Numbers
  1. $formatted = date("F-j-Y", strtotime($dateFromMysql));
The date function converts a Unix timestamp into a formatted string, based on the pattern. The strtotime converts most known date formats into a Unix timestamp, including the format MySQL uses.
May 19 '10 #5
Thanks guys that really did the trick for me.. I appreciate everyones help...

You guys are the best...
May 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
1
by: Bryan Ax | last post by:
We have some code that's having problems when multiple applications running as different identities try to hit this code - the code creates a Mutex, and when the second identity tries to access it,...
3
by: Diego Park | last post by:
Hello, I am trying to generate tests for unmanaged C++ code, but I fail. Is that possible or am i doing something wrong? If so, where can I find a sample code? Thanks in advance. Best wishes,...
4
by: ChrisC | last post by:
I am a relative newbie to .NET - two weeks, using everyday. My problem is this. I have my asp.net application where the web.config contains the database connection string. Normally i access this...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
0
by: sagarp86 | last post by:
How to write code for Database connection in order to avoid bad effect of traffic on database access. How to make connection fast for each user/client in ASP. Please help me....
9
by: fniles | last post by:
When using VB6 and ADO, if I only do a Read, I will open a recordset with Forward Only cursor and Read Only lock, thus it will be faster than a non Read recordset. In VB.NET, when only need to do...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
0
by: dalaimanoj | last post by:
I have a form with buttons 'View', 'Save', 'Delete', 'Edit' and some textboxes to input data.. On clicking each buttons the database is connected and the respective action is done. For example...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.