0

Is it possible to select an array to use in a foreach statement can't figure it out :/

My Code:

<?php
$selmon = $_GET['m'];
$selectmon = ('$' . $months[$selmon-1]);
foreach ($selectmon as $day){
if ($day % 7 == 0){
echo ("<td>" . $day . "</td>");
echo ("</tr>");
echo ("<tr>");
}else{
echo ("<td>" . $day . "</td>"); 
}}
?>

My arrays basically contain a list of all the days in the month e.g.?

$september = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);

Am I going about this the wrong way or is it even possible?

Also I have an array of the month names:

$months = array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");

and if i use

echo $selectmon;

If i use localhost/index.php?m=1 it returns

$january 
Jordan Newton
  • 119
  • 1
  • 10

2 Answers2

4

You were close. You need to do ${$months[$selmon-1]} and this is called accessing a variable dynamically:

<?php
$selmon = $_GET['m'];
$selectmon = ${$months[$selmon-1]};
foreach ($selectmon as $day){
  if ($day % 7 == 0){
    echo ("<td>" . $day . "</td>");
    echo ("</tr>");
    echo ("<tr>");
  }else{
    echo ("<td>" . $day . "</td>"); 
  }
}
?>

also, it's considered not the best practice to access variables in this way. Consider what would happen if you had a different scenario: echo ${$_GET['v']}. Now, if a user went to webpage.php?v=MYSQL_PASSWORD the code would read echo $MYSQL_PASSWORD. I'm sure you can imagine how this technique may reveal a security risk given certain circumstances. The way you're using dynamic variables wont run into this problem because in the worst case (where $months[#] is null) you are only exposing $null which does not matter so much.

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • 1
    You helped following them the wrong path, congrats :-) – zerkms Nov 18 '15 at 23:57
  • @zerkms he wanted to know how to access variables dynamically and I told him. Saying he's going to "end up with the wrong calendar" is very melodramatic. If OP wants to do this with `strtotime` or by using one of the *hundreds* of other methods then nothing is stopping him... – David Zorychta Nov 18 '15 at 23:59
  • @zerkms how come? Could you point me in the right direction of how to do this please? – Jordan Newton Nov 19 '15 at 00:00
  • @Macmee "he wanted to know how to access variables dynamically and I told him" --- and that's where the experience comes into play: it's best to teach people how to do it right, not feed them with solutions that they chose (and that are simply wrong) "then nothing is stopping him" --- it's you with your experience that might have (but haven't since - hey I'm a QA-robot) – zerkms Nov 19 '15 at 00:01
  • @JordanNewton see the other answer. – zerkms Nov 19 '15 at 00:01
  • @Macmee fyi your method did help me achieve what i wanted to do. I'm new to this not really sure about best practices etc. Thanks for your help. – Jordan Newton Nov 19 '15 at 00:02
  • 1
    @JordanNewton so the proper answer would start with: You should **NEVER EVER** deal with variable variables (what Macmee suggested you here). It's a sign that the solution is already flawed. – zerkms Nov 19 '15 at 00:03
  • @zerkms earlier today I answered a question and was criticized for moving the OP towards a different solution. Now you're criticizing me for doing the opposite. The signals being sent here are unclear, but what IS clear is that my answer solves OP's problem, and the answer below mine says how to solve the problem in another way. I'll add such a disclaimer into my answer but again, OP asked a question and I gave him the correct answer to his question. – David Zorychta Nov 19 '15 at 00:05
  • @Macmee it is welcomed if you taught OPs the right things (even if they asked something else due to lack of knowledge/experience). Could you point to the other answer please btw (I haven't found anything suspicious among your latest)? – zerkms Nov 19 '15 at 00:07
  • @zerkms http://stackoverflow.com/a/33793100/309641 was just answering a question and this happened again. I guess the right answer is to always answer OP *and* point out dangerous solutions! – David Zorychta Nov 19 '15 at 00:27
  • @Macmee well, I think you have a point there and I agree with you. – zerkms Nov 19 '15 at 00:37
2

I'd recommend looking into the date() function instead. It's a lot easier to do rather than hardcoding your month lists, and your code will be a lot cleaner as well. Something like this:

$month = $_GET['m']-1;
$numDays = date("t", strtotime("2015-$month-01"));

will always display the number of days in the selected month. You can then write something like

for($day = 1; $day < $numDays + 1; $day++)
{
  echo "<td>$day</td>";
  if ($day % 7 == 0)
  {
    echo "</tr><tr>";
  }
}

You can also display month names, days of the week, or handle leap years, all with no arrays needed!

A. Sim
  • 166
  • 6
  • Additionally, moving the line in which you echo the day within a table cell outside of the if statement will cut down unnecessary duplication :) – A. Sim Nov 18 '15 at 23:54
  • Thank you. Didn't even realise I could do that that saves 12 line of unnecessary code. The if statements were for formatting the date into rows of 7. Im trying to make a calendar. Thanks for your help. – Jordan Newton Nov 18 '15 at 23:57
  • @JordanNewton you cannot fit the calendar into 12 lines. Hint: february – zerkms Nov 18 '15 at 23:58
  • Jordan Newton, I've updated the code a bit to show how you'd cleanly handle the if statements and the loop with an integer value for $numDays instead of an array. – A. Sim Nov 19 '15 at 00:00
  • @A.Sim Ah i get what you were saying now i was looking at as in just using the numdays variable in the code i already had. That looks much cleaner i was just trying to do it with a while loop but using for is much better. Thank you. – Jordan Newton Nov 19 '15 at 00:08