Jump to content

Using multiple variables to create an array...


Jim R
Go to solution Solved by Jim R,

Recommended Posts

I'm passing variables passed through the URL for year and team.  

 

For example:  $year = 19, $team = 2

 

I'm joining those variables and creating a value which will correspond to team's page on a site.

 

$number = 192

 

The question is how do I set up my array to use that value 192 to make $calendar = 2019roots ?

$year = $_GET['&year'];
$team = $_GET['&team'];

$number = $year.$team;

echo $number;

$calendar = array (
'192' =>'2019roots',
'212' =>'2021roots',
'191' =>'2019grind',
'201' =>'2020grind',
'211' =>'2021grind',
'221' =>'2022grind',
'231' =>'2023grind'
);

I'm going to use that value to call up the corresponding calendar based on which team's page the User is viewing. 

 

(I suppose I could create a series of if/else statements, but I'd rather learn how to do this.  I've tried searching for examples, but I'm not even sure how to concisely query the search.)

 

 

Link to comment
Share on other sites

I might've made the above too complicated, which is to say I was way off.  I've changed the code, but now it just prints 'array'.

$year = $_GET['&year'];
$team = $_GET['&team'];


$calendar = $year.$team;


$calendar = array (
'192' =>'2019roots',
'212' =>'2021roots',
'191' =>'2019grind',
'201' =>'2020grind',
'211' =>'2021grind',
'221' =>'2022grind',
'231' =>'2023grind'
);

echo $calendar;
Link to comment
Share on other sites

  • Solution

This works. 

$year = $_GET['&year'];
$team = $_GET['&team'];

$number = $year.$team;

// echo $number;

$calendars = array (
'192' =>'2019roots',
'212' =>'2021roots',
'191' =>'2019grind',
'201' =>'2020grind',
'211' =>'2021grind',
'221' =>'2022grind',
'231' =>'2023grind'
);


echo $calendars [$number];

echo do_shortcode( '[calendarizeit calendar="'. $calendars [$number] .'"]' );
Link to comment
Share on other sites

An alernative would be to keep the $year and $team separate

$cal_arr =  [  1 => [ 19 => '2019grind' ,
                      20 => '2020grind' ,
                      21 => '2021grind' ,
                      22 => '2021grind' ,
                      23 => '2021grind'     ] ,
            
               2 => [ 19 => '2019roots' ,
                      21 => '2021roots'     ]
            ];
            
$calendar = $cal_arr[$team][$year];

Which could be further simplified to

$team_cal = [ 1 => 'grind',
              2 => 'roots'  ]; 

$calendar = '20' . $year . $team_cal[$team];

Link to comment
Share on other sites

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.