0

I want to put this in a loop because it needs to be repeated 6 times, is it true that using variable variables is a bad practice? Do I need associative arrays?

Basically the 'c1' inside the variable needs to progressively change into 'c2', 'c3'...etc

<?php if ($pm_c1_djwd !== '') { ?>

<div>
   <span style="width:<?php echo $pm_width_c1;?>%"></span>
   <span><?php echo $pm_description_c1; ?></span>
</div>

<?php } ?>

Many Thanks

5
  • 1
    It would help to know where these variables are coming from. Commented Jan 28, 2013 at 23:45
  • you coud try var_dump($pm_c1_djwd) and post it here Commented Jan 28, 2013 at 23:51
  • Why? The variables are echoing the right data, I only need to know how to change the number at the end of each variable into '_c2', '_c3', '_c4' dynamically so I don't have to repeat the same code 6 times Commented Jan 28, 2013 at 23:54
  • Why do you have 6*3 variables to begin with? Just use nested arrays. Commented Jan 28, 2013 at 23:55
  • 2
    Right. Any time you feel the need to do this, it's an indicator that you should be using normal indexed arrays. Commented Jan 29, 2013 at 0:17

3 Answers 3

1

Why not try regular arrays?

<?php
// Warning: Typed raw in the textarea
$pm_width = array(100, 100, 100, 100, 100, 100);

$pm_description = array(
  "Gizmo",
  "Doodad",
  "Widget",
  "Dohicky",
  "Thing-me-a-bob",
  "Marvelous toy my father gave to me."
);

$pm_c1_djwd = "Snod";

if ($pm_c1_djwd !== '') {

  for ($i = 0; $i < count($pm_description); $i++) {
    $width = $pm_width[$i];
    $desc = $pm_description[$i];

    echo "<div>";    
    echo "<span style='width:${width}%'>$desc</span>";     
    echo '</div>';
    echo PHP_EOL;
  }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of having a variable for each field like $pm_description_c1, ..c2 and so on, put them in an associative array:

$pms = array(
    array('description' => 'your description', 'width' => '123px', 'djwd' = 'what'),
    array('description' => 'Second item', 'width' => '123px', 'djwd' = '')
);

Then loop through them:

<?php
foreach ($pms as $pm) {
    if ($pm['djwd' !== '') {
    ?>
        <div>
            <span style="width:<?php echo $pm['width'];?>%"></span>
            <span><?php echo $pm['description']; ?></span>
        </div>
    <?php
    }
}
?>

Comments

0

You can use variable variables for the thing you are asking...

<?php
$i = 0;
while( $i < 6 ){
$i ++;

$variable = "pm_c".$i."_djwd";
$variable2 = "pm_description_c".$i."";

 if (isset($$variable) && $$variable != '') { ?>

<div>
   <span style="width:<?php echo $$variable;?>%"></span>
   <span><?php echo $$variable2; ?></span>
</div>

<?php } 
}

1 Comment

I think this is still the best solution for my case and it works fine. Thanks everyone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.