Jim R Posted August 26, 2019 Share Posted August 26, 2019 So I already have this below... $state = array ( 1 => 'Sectional', 2 => 'Regional', 3 => 'Semi-state', 4 => 'State' ); Can those numbers be a range -- 1-16, 17-32, 33-48 and 49-64 -- without just listing them all? Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 I could do a series of If...else Quote Link to comment Share on other sites More sharing options...
requinix Posted August 26, 2019 Share Posted August 26, 2019 There is a way to be clever about it, but... What's the rest of the code? How is the array being used? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 ... and why do you want 16 keys for the same value? Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 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. Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 26, 2019 Share Posted August 26, 2019 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 Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 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(). Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 26, 2019 Share Posted August 26, 2019 So where does the data you are using to populate the array come from? Is it from a database query? If so, what does your query look like? What are the database column names? Could you also provide a couple database records? Quote Link to comment Share on other sites More sharing options...
requinix Posted August 26, 2019 Share Posted August 26, 2019 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. 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 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 Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 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 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). Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 Are you saying you would prefer to rewrite your code every year instead of just changing the data? Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 (edited) 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 August 26, 2019 by Jim R Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 26, 2019 Share Posted August 26, 2019 (edited) Are you able to show more of the code? How is $state being populated now? Never mind, you showed that already. Edited August 26, 2019 by cyberRobot 1 Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 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 Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 (edited) 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 August 26, 2019 by Jim R Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 That array structure is held within the database Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 4 minutes ago, Barand said: That array structure is held within the database Now I need to check the Sectional# against the array. I'm asking if it can be done that way...trying to learn. Otherwise, I can just do the *if...else*. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2019 Share Posted August 26, 2019 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 = ? 1 Quote Link to comment Share on other sites More sharing options...
Jim R Posted August 26, 2019 Author Share Posted August 26, 2019 (edited) 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 August 26, 2019 by Jim R Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.