Solved Joining two Variables

September 15, 2011 at 16:55:07
Specs: Windows XP SP3, AMD Samphron 3000+, 1536 RAM
Hello! I need some help with this little issue. I want to echo two variables joined. How could I do that.

Here it is :

$plan_1['days'] = "15";
$selectedPlan = "1";


echo $plan_X['days']; // I want to place the $selectedPlan in the X place


I will be very glad for your help. Thanks mates.


See More: Joining two Variables

Report •


#1
September 16, 2011 at 12:47:24
$plans  = array('plan_1' => 15, 'plan_N' => N);
$selectedPlan = "1"

echo $plans['plan_' . $selectedPlan];

or maybe

$plans = array('plan_1' => array('days' => 15), 'plan_N => array('days'  => N));
echo $plans['plan_' . $selectedPlan]['days'];

you might also check here for variable variables.

HTH


Report •

#2
September 16, 2011 at 12:57:57
Thank you. I will copy more of the Script, Shutat. Can you please make an expample again, for this one ? :)

$planPrefix = 'S';

$plan_1['title'] = "STARTER";
$plan_1['percent'] = "1.9";
$plan_1['days'] = "10";

$plan_2['title'] = "ADVANCED";
$plan_2['percent'] = "2.1";
$plan_2['days'] = "20";

if($_GET[$planIdName] == $planPrefix."1") { $selectedPlan = "1"; }
if($_GET[$planIdName] == $planPrefix."2") { $selectedPlan = "2"; }
if($_GET[$planIdName] == $planPrefix."3") { $selectedPlan = "3"; }
if($_GET[$planIdName] == $planPrefix."4") { $selectedPlan = "4"; }

echo $plan_X['days']; // Hot build it like this, in this situation, Or how to make the variables correctly for you rmethod ?


Report •

#3
September 17, 2011 at 09:13:13
✔ Best Answer
Hello,

I'm not sure on the variable variables as I've never used them; I did try the following on my test server... hopefully you can use some variation to satisfy your need.

<?php   

   $plans = array('plan_1' => array('title' => 'STARTER',  
                                    'percent' => '1.9', 
                                    'days' => '10'),
                  'plan_2' => array('title' => 'ADVANCED', 
                                    'percent' => '2.1', 
                                    'days' => '20'));

   if(preg_match('/^(s\d{1})$/i', $_GET['plan']) == 1) {       

      $selectedPlan = preg_replace('/s/i', '', $_GET['plan']);
      settype($selectedPlan, "integer");

      if($selectedPlan < 1 || $selectedPlan > 2) {
         echo "plan range is invalid<br />";
      } else {
         foreach($plans['plan_' . $selectedPlan] as $key => $val) {
            echo $key . " = " . $val . "<br />";
         }
      } 

   } else { ?>

<a href="<?php echo $_SERVER["PHP_SELF"];?>?plan=S1">starter</a>
<a href="<?php echo $_SERVER["PHP_SELF"];?>?plan=S2">advanced</a>

<?php

   }
?>

hope that helps


Report •

Related Solutions

#4
September 23, 2011 at 05:05:37
Thank you very much. I will try this.

Report •


Ask Question