Jump to content

Recommended Posts

4 hours ago, Barand said:

... and why do you want 16 keys for the same value?

Indiana has four Classes (4A, 3A, 2A, 1A) of teams based on enrollment in basketball.  They are broken up into 16 sectionals.  Teams in sectionals 1-16 are in 4A, 17-32 are in 3A, 33-48 are in 2A and 49-64 are in 1A.  The problem is, it's different for each sport, and there are Sectional changes every year.  

So I have a Schools table that has a BSectionals column (B = basketball), and I'll eventually have a column for each sport.  I could certainly have a Class column for each sport, but I figured it could be handled with code within each sport's part of the site.  

8 hours ago, requinix said:

There is a way to be clever about it, but...

What's the rest of the code? How is the array being used?

For the above array, there are four weekends in the state tournament in Indiana to determine a champion.  The array signifies Sectional winners, Regional winners, etc.  So I have a State column to determine how far they went in the tournament.   If the team won Sectionals, their State column would have a 1.  Regionals 2, Semi-state 3 and State champion 4.  If the cell is NULL it means you lost in the sectionals.  

 

Here is the code handling the output

if (!empty ($row['state'])) {
			
				if ($row['state'] == 1) {
					echo $state[$row['state']] . ' ' . $row['sectional'] .' Champions';
					}	
				else {			
					echo $state[$row['state']] . ' Champions';
					}	
			}
			else {
				echo 'Lost in Sectional';
			}

I figured I could do something similar to output which Class they are in based on which Sectional they are in.  

Have you considered multi-dimensional arrays? More information can be found here:
https://www.php.net/manual/en/language.types.array.php

Note that example 14, on the above page, shows an example of a multi-dimensional associative array:
https://www.php.net/manual/en/language.types.array.php#example-70

Just now, cyberRobot said:

Have you considered multi-dimensional arrays? More information can be found here:
https://www.php.net/manual/en/language.types.array.php

Note that example 14, on the above page, shows an example of a multi-dimensional associative array:
https://www.php.net/manual/en/language.types.array.php#example-70

Yes, but the point is to avoid typing all the numbers out.  Given the values and variances across each sport, I would be better off using if...else.

 

Right now, I'm checking out range().

 

 

21 minutes ago, Jim R said:

Right now, I'm checking out range().

And that may very well be what you end up using, but what we're trying to check out is whether (a) there is, in fact, something better to use, and (b) whether what we come up with not only solve the problem but not create additional problems to solve, be that now or in the future.

  • Like 1
1 hour ago, Jim R said:

So I have a Schools table that has a BSectionals column (B = basketball), and I'll eventually have a column for each sport.

Adding columns for each sport is not the way to design database tables.

From what you described above,

  • Each sport has several classes
  • Each class has several sectionals
  • Each sectional has several teams
  • Each school has several teams(sports)

so your data should reflect this structure

image.png.500089d1937a3c52866aad53cb93e002.png

35 minutes ago, Barand said:

Adding columns for each sport is not the way to design database tables.

From what you described above,

  • Each sport has several classes
  • Each class has several sectionals
  • Each sectional has several teams
  • Each school has several teams(sports)

so your data should reflect this structure

image.png.500089d1937a3c52866aad53cb93e002.png

Certainly there is way to handle it with code because the classes for some teams change year to year (or at least every other year).  

11 minutes ago, Barand said:

Are you saying you would prefer to rewrite your code every year instead of just changing the data?

The code would be stagnant.  We know every year in basketball, teams in sectional 1-16 are 4A.  What we don't know is which class or sectional every team is in from year to year.  I can produce *if...else* to handle it.  I just wondered if there was a way for an array to handle it, just as I did for tournament success.  Mostly in the guise of learning if it can work that way.  

Edited by Jim R
2 minutes ago, cyberRobot said:

Are you able to show more of the code? How is $state being populated now?

Right now it's coming from the State column I set up to reflect last year's results.  (Going forward it will come from game schedule/results.)  I was just showing the above array to illustrate how I did state.  The setup for that works.  

I'm just seeing if it makes sense to set up an array to handle the class each team is in, since it changes.  If code can't handle it, I would have to change their sectional and their class.  However, I know I can set up *if....else* statements to handle it.  Just trying to learn more about arrays.

10 minutes ago, Jim R said:

The code would be stagnant.  We know every year in basketball, teams in sectional 1-16 are 4A.  What we don't know is which class or sectional every team is in from year to year.  

In which case you have in intermediate file to record, each season, which sectional a team is in

image.thumb.png.4d670a82def84759b19629bf138ebffd.png

So an array, like what I used for state tournament success, using range() isn't feasible?  That's what I'm asking.  

Something that starts like this...

$class = array (
    4 => range(1,16),
    3 => range(17,32),
    2 => range(33,48),
    1 => range(49,64)
);

...then checking their Sectional value from the table against that array.

Edited by Jim R

if the purpose is to find the class given the sectional# then it would be easier if the array were inverted...

<?php

foreach (range(1,16) as $sect) $sectional_class[$sect] = 4;
foreach (range(17,32) as $sect) $sectional_class[$sect] = 3;
foreach (range(33,48) as $sect) $sectional_class[$sect] = 2;
foreach (range(49,64) as $sect) $sectional_class[$sect] = 1;

echo $sectional_class[25];                //--> 3
?>

or you can get them direct form the database without using array to look them up

SELECT t.team_name
     , s.sectional_id
     , c.class_id
FROM team t
     JOIN
     sectional_team st USING (team_id)
     JOIN
     sectional s USING (sectional_id)
     JOIN
     class c USING (class_id)
WHERE season = ?

 

  • Like 1
5 hours ago, Barand said:

if the purpose is to find the class given the sectional# then it would be easier if the array were inverted...


<?php

foreach (range(1,16) as $sect) $sectional_class[$sect] = 4;
foreach (range(17,32) as $sect) $sectional_class[$sect] = 3;
foreach (range(33,48) as $sect) $sectional_class[$sect] = 2;
foreach (range(49,64) as $sect) $sectional_class[$sect] = 1;

echo $sectional_class[25];                //--> 3

That worked perfectly, then I added the $sectional variable in $sectional_class[$sectional].  

That's what I was getting at.  

 

The sad part about this is it's only used four times on the whole site, but it's a nice touch for the readers who are fans of that team.

Edited by Jim R
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.