SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the following snippet of code, I have a repeated pattern:
    Code:
    <?php
    if ($SESSION["show_report0"] == "1") { $report0_full_name = $report_full_name[0]; }
    if ($SESSION["show_report1"] == "1") { $report1_full_name = $report_full_name[1]; }
    if ($SESSION["show_report2"] == "1") { $report2_full_name = $report_full_name[2]; }
    if ($SESSION["show_report3"] == "1") { $report3_full_name = $report_full_name[3]; }
    ?>
    I'd like to put this into a loop somehow so that I can replace the incremented numbers with an incremented variable.. the problem is that I can't put a "variable within a variable".

    I know this isn't how to do it, but maybe this example will help to illustrate what I want to do. Compare with the above code.. I've bolded the part I can't figure out.
    Code:
    <?php
    
    $i = 0;
    while($i <= 3) {
         if ($SESSION["show_report$i"] == "1") { $report$i_full_name = $report_full_name[$i]; }
         i++;
         }
    ?>
    Thanks for any help.
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no PHP expert, but you'll probably want to make it like this:

    PHP Code:
    <?php
    $i 
    0;
    while(
    $i <= 3) {
         if (
    $SESSION["show_report$i"] == "1") { 
    $reportvarname 'report'.$i.'_full_name';
    $
    $reportvarname $report_full_name[$i]; }
         
    $i++;
         }
    ?>

  3. #3
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's pretty close, but I actually need to have a variable with the iteration number in it.

    In other words, I need the $report$i_full_name to be $report1_full_name, and then $report2_full_name, etc.. not just the value inside it..
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    In fact, you can have a "variable inside a variable" in php. You use "variable variables". You would have to do something like this:

    $x = "report" . $i . "_full_name";
    $$x=$report_full_name[$i];

    For an index value $i=0, the last line would give you:

    $report0_full_name=$report_full_name[0];

    You can echo the value of the variable variable $$x in two ways:

    echo ${$x};
    echo $report0_full_name;
    Last edited by 7stud; May 9, 2001 at 22:56.

  5. #5
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to draw attention to the double dollar-sign ($$) used with the variable variable, in case you didn't notice it.

    Example...
    PHP Code:
    // Set $variable_name to a string containing "some_variable"
    $variable_name "some_variable";
    // Set another variable with the name being whatever $variable_name contains
    $$variable_name "I am a variable variable!"// Note double dollar-signs 
    Ed Rands

  6. #6
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never knew about the $$. This is what I'd do-- not as elegant, but you could also use eval:

    PHP Code:
    for($i=0;$i<5;$i++) {
     eval(
    "if ($SESSION[\"show_report".$i."\"] == "1") { $report".$i."_full_name = $report_full_name[$i]; }

    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •