1

I am currently building some code that changes the background color of a number inside the array I have specified.

I can get the code to successfully change the background color of a specified number, when I am trying to apply the same code to an array of numbers it is changing the background color but it's then echoing the unformatted numbers afterwards.

<?php
 $month_end = '31';
 $start = '10';
  $end = '15';

  foreach (range($start, $end) as $trip_array) {


 foreach (range(1, $month_end) as $number) { ?> 
 <?php
 if($number === $trip_array) {
 ?>
 <td style="background-color: #ccc;"><?php echo $number; ?></td>
 <?php 
 } else { ?>
<td><?php echo $number; ?></td>
 <?php
     }
  }
 }
 ?>

I can't yet post an image so I hope I have supplied enough information with my problem and would really appreciate a fresh set of eyes on this :)

Thank you.

3
  • What are you trying to achieve? What should the output be? Commented Mar 25, 2015 at 9:31
  • Basically if the array is 10-15 (in this example) I want it to display 1 - 31 (which it's doing" but 10-15 have the different background-color to stand out, thanks for asking. Commented Mar 25, 2015 at 9:33
  • Incase that wasn't clear enough, i want it to display "1 2 3 4 5 6 7 8 9 [10] [11][12][13][14]15] 16 17 18 etc with the numbers in [] have the different background styling. Commented Mar 25, 2015 at 9:36

2 Answers 2

1

Give this a shot. You can change the background-color to any color you need. I didn't do much with the styling (padding, margin and so on), but that's a matter of HTML and CSS. I believe you can do that on your own.

<?php

$month_end = '31';
$start = '10';
$end = '15';

foreach(range(1, $month_end) as $days)
{
    if(in_array($days, range($start, $end)))
        echo "<p style=\"background-color: red;\">" . $days . "</p>";
    else
        echo "<p>" . $days . "</p>";
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Wow worked perfectly, thank you, I'll study what you sent me and learn what I did wrong.
@JCairns This algorithm is a bit different from yours, but that doesn't mean your approach was wrong. It just needed some refinements. But I did it my way.
0

Try this:

<?php

$month_end = '31';
$start = '10';
$end = '15';

foreach (range(1, $month_end) as $number) { ?> 
<?php
if($number >= $start AND $number <= $end) {
?>
<td style="background-color: #ccc;"><?php echo $number; ?></td>
<?php 
} else { ?>
<td><?php echo $number; ?></td>
<?php
}
}
?>

IdeOne example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.