Page 1 of 1

Need help with dynamic variables

Posted: Wed Jan 29, 2003 4:54 pm
by ruthsimon
I have a form for a user to enter event information. The form requests 3 separate dates (event start date, event end date and broadcast date). I call a function to display separate drop down lists for month/day/year on the form.

Code: Select all

function displayDateDropdown($Title,$Month,$Day,$Year)
{ 
     //Create an array of months
    $monthName = array (1=> "January", "February", "March", etc.

    // Build selection list for the month 
    echo "<select name='".$Title."_dateMO'>\n";
    $SelectMonth&#1111;$Month]=" selected"; 
    for ($n=1; $n<=12; $n++) &#123;
       echo "<option value=$n".$SelectMonth&#1111;$n].">$monthName&#1111;$n]</option>\n";
    &#125;
    echo "</SELECT>\n";
    etc..
I use the form in multiple instances (create, edit, copy an event), and in many cases, the start and end dates are the same day, while the broadcast date is always in the future.

Code: Select all

$today = Time();	
$start_dateMO = date("n", $today);       //current month 
$start_dateDAY = date("j", $today);      //current day 
$start_dateYR = date("Y", $today);       //current year
$end_dateMO = date("n", $today);       //same month 
$end_dateDAY = date("j", $today);      //same day 
$end_dateYR = date("Y", $today);        //same year
$broadcast_dateMO = 2;                    //later date
$broadcast_dateDAY = 10;                 
$broadcast_dateYR = 2003;


I would like to call the function with the same variables in all 3 places on the form, (i.e., displayDateDropdown ($$Title,$$Month,$$Day,$$Year), instead of displayDateDropdown("start_",$start_dateMO ,$start_dateDAY,$start_dateYR), etc.), but I can't quite get my hands around the dynamic variables.

Any help would be appreciated, Ruth

Posted: Wed Jan 29, 2003 5:07 pm
by daven

Code: Select all

<?php
$title = "Hello";
$$title= "World";

echo $title; // prints Hello
echo $$title; // prints World
echo $hello; // prints World
?>
Dynamic variables work by creating a variable out of a string. So for your code, try something like this:

Code: Select all

<?php
$title = "start_";
$Month="start_dateMO";
$Day="start_dateDAY";
$Year="start_dateYR";

$start_dateMO = date("n", $today);       //current month
$start_dateDAY = date("j", $today);      //current day
$start_dateYR = date("Y", $today); 

// this should function identically to DisplayDateDropdown("start_",$start_dateMO ,$start_dateDAY,$start_dateYR)
DisplayDropDown($$title, $$Month, $$Day, $$Year); 
?>

Posted: Wed Jan 29, 2003 5:13 pm
by ruthsimon
Thanks for the answer, but this looks like even more code.

Maybe I'm going in the wrong direction trying to use dynamic variables. I'm want to avoid using the same definitions for multiple variables--make the code more efficient.

Any ideas? Ruth