SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Weird Problem

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,898
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Weird Problem

    Hey,
    I ahve this in an include file:

    PHP Code:
    echo $get[B]ride[/B]data['[B]ride[/B]_review']; ?> 
    Basically the words in bold should be replaced with whatever a variable is set to in the page which is including the other page. Forexample on a page i have this:

    PHP Code:
    $typelower 'area'
    I need a way for $typelower to be put in inplace of the words in bold when the first bit of code is included within the main page where $typelower is defined.

    How can this be done?

    Thanks

    Neil

  2. #2
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,898
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil View Post
    Hey,
    I ahve this in an include file:

    PHP Code:
    echo $get[B]ride[/B]data['[B]ride[/B]_review']; ?> 
    Basically the words in bold should be replaced with whatever a variable is set to in the page which is including the other page. Forexample on a page i have this:

    PHP Code:
    $typelower 'area'
    I need a way for $typelower to be put in inplace of the words in bold when the first bit of code is included within the main page where $typelower is defined.

    How can this be done?

    Thanks

    Neil


    The bold bits don;t seem to be working within php brackets but you can see where the bold bits are meant to be!

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you can do something like this but I'm not sure so good luck.
    PHP Code:
    $name "get" $typelower "data";
    $string $typelower "_review";
    echo $
    $name[$string]; 
    Kayzio - We don't hesitate, we accelerate.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    substitution

    Clear as mud, I know, but maybe something like
    PHP Code:
    $included_var "get%sdata['%s_review']"
    then
    PHP Code:
    $insert_val 'area';
    $synth sprintf($included_var$insert_val$insert_val);
    print_r($$synth); 
    EDIT: to see what $$synth will look look like (should be getareadata['area_review']) use
    print_r($synth);
    instead.
    Last edited by Mittineague; Jun 1, 2007 at 17:49.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    working example

    It took me a while to get a working example. Some of the ways I tried that I thought might have worked didn't. but this does.
    PHP Code:
    <?php
    $arr_name 
    "get%sdata";
    $arr_key "%s_review";
    $insert_val 'area';
    $synth_arr sprintf($arr_name$insert_val);
    echo 
    $synth_arr "<br />";
    $synth_key sprintf($arr_key$insert_val);
    echo 
    $synth_key "<br />";
    $
    $synth_arr = array($synth_key => 'test result');
    var_dump($$synth_arr);
    echo 
    "<br />";
    var_dump($getareadata['area_review']); 
    ?>

  6. #6
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,898
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Thanks for that. Could you explain what each line does so I know how it works?

    Thanks

    Neil

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    variable variable array

    It's a shame I can't get this to work
    PHP Code:
    $included_var "get%sdata['%s_review']"
    $insert_val 'area';
    $synth sprintf($included_var$insert_val$insert_val);
    var_dump($synth);//gives getareadata['area_review']
    $$synth 'test result';
    var_dump($getareadata);//gives NULL
    var_dump($getareadata['area_review']);//gives NULL 
    The %s -s are "string place holders" for the sprintf function. Similar to the concatenation of Kayzio's example, except you don't need access to the inserted variable first this way.
    The $$ is a "variable variable" that makes a variable with a name that is the value of the second variable.
    The technique works fine for simple variables but I could not get it to work for an array the same way. For example, this does work
    PHP Code:
    $included_var "get%sdata%s_review"
    $insert_val 'area';
    $synth sprintf($included_var$insert_val$insert_val);
    var_dump($synth);//gives getareadataarea_review
    $$synth 'test result';
    var_dump($getareadataarea_review);//gives 'test result' 
    Using a concatenation example
    PHP Code:
    <?php
    $insert_val 
    'area';
    $arr_name "get" $insert_val "data";
    $arr_key $insert_val "_review";
    $
    $arr_name = array();
    $
    $arr_name['{$arr_key}'] = 'test result';
    var_dump($getareadata);//gives array(0) 
    var_dump($getareadata['area_review']);//gives NULL 
    ?>
    shows that there is some problem with using variable variable and trying to assign a value to an array key at the same time during it's construction. I tried using curly braces in different places, but I didn't find a way that worked.
    However, where
    PHP Code:
    $$synth 'test result'
    and
    PHP Code:
    $$arr_name['{$arr_key}'] = 'test result'
    don't work
    PHP Code:
    $$synth_arr = array($synth_key => 'test result'); 
    does. The only problem is that it requires the array name and the key value to be worked with seperately so it's not as "elegant" as
    PHP Code:
    $$synth 'test result'


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
  •