0

I have this php code foreach loop inside another foreach what I need is to set all the array value one by one on each loop(red, blue, green, black). I need to change $green in

data-percent="<?php foreach ($green as $row) { echo $row->ink_numbers; }?>">

with all the value in array one by one on each loop I try:

 data-percent="<?php foreach ('$'.echo $value as $row) { echo $row->ink_numbers; }?>">

but it does not work. PLEASE HELP

COMPLETE CODE:

<?php
 $color  = array( 0 => 'red' , 1 => 'blue', 2 => 'green', 3 => 'black' );
 foreach ($color as $key => $value) {

?>


                                                                                    <!-- make $green dynamic -->
<div class="<?php echo $value; ?> progress-pie-chart" data-percent="<?php foreach ($green as $row) { echo $row->ink_numbers; }?>">
  <div class="ppc-progress">
    <div class="<?php echo $value; ?> ppc-progress-fill"></div>
  </div>
  <div class="<?php echo $value; ?> ppc-percents">
    <div class="pcc-percents-wrapper">
      <span>%</span>
    </div>
  </div>
</div>
<?php } ?>
1
  • 1
    Where is $green defined? Right now, $green is null, so you won't get anything doing foreach($green... Commented Feb 27, 2015 at 16:12

1 Answer 1

1

You can use a variable name, like this:

<?php foreach ($$value as $row) { echo $row->ink_numbers; }?>"

So when $value = 'green' the foreach will loop through $green.

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

2 Comments

If he does that, $$value, when $value='green', is a string, not an array. So, he will be performing foreach on a string and coming right back here. He still needs to define $green=array(...) somewhere.
@Kainaw I'm assuming that he has $green defined somewhere that he's just not showing us. A lot of new people do that, define a variable elsewhere in the code and not include it in their SO question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.