SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help echoing a variable inside a variable

    content_col="some suff,$city,some other stuff"
    content_col is a mysql db column

    Retrieved via php as
    PHP Code:
    $city=$_GET['city'];// city value passed thru url
    --- mysql query retrieving content_col here ----
    $content_col=$_SESSION['content_col'];
    echo 
    "$content_col"
    the output i get is:
    some suff,$city,some other stuff.
    The value for $city is never rendered because it inside another variable($content_col).
    No matter what i do,it does not seem to work.
    I tried to wrap it inside single quotes,braces,^^ but nothing worked

    any ideas will be much appreciated,thank you in advance

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Tikila
    there is no way to store php code in the mysql column. That's why it is not rendered.
    You should not store php variables in the database. code must be always separated from data.
    You have to reorganize your app.

    If you tell us more about these variables, we could advise you how to do it.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or save a parameter in the cell:

    "some suff,%s,some other stuff"
    and when you get the variable from an url:

    PHP Code:
    if( isset( $_GET['city'] ) )
    {
    $content_col=$_SESSION['content_col'];
    printf($content_col ,  htmlspecialchars$_GET['city'] ) );  
    // and the inserted value is in the string , and make sure you filter the user inputs


  5. #5
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all your replies

    Here's what am trying to accomplish
    I have a page that lists local news for a bunch of cities
    PHP Code:
    echo "$city news"
    I want to add some relevant links below the news like:
    PHP Code:
    $city weather 
    $city events
    $city shows 
    So i hoped to include an extra column in db(content_col) containing rss feeds for weather,events and shows and once retrieving the news,content_col would just pick the value of the city already echoed for the news

    content_col="$city weather,$city events, $city shows" // db column

    I implemented the %s suggestion by djjjozsi but it only worked on one city instance

    Thanks again for all your replies

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you're doing sounds a bit strange, but this should work
    PHP Code:
    content_col="{city} weather, {city} events, {city} shows";
    echo 
    str_replace('{city}'$_GET['city'], $content_col); 

  7. #7
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi hash,
    I just just did that,just a bit different
    For anyone who's looking for this kinda solution,here's my final version of it


    content_col="%s weather,%s events, %s shows" // column in db
    PHP Code:
    $content_col str_replace("%s",$city,$content_col); 
    Pmw57:


    Thank you all for your help,I really appreciate your care and time invested

    little note for Pmw57:

    Nice article on your blog,however i encountered some syntax errors when i tried the codes below:

    PHP Code:
    $literal = 'that will not change';
    $statement = 'a string $literal.';
    $output = eval(sprintf('<?php return <<<OUTPUT
    %s
    OUTPUT; ?>', 
    $statement));
    and

    PHP Code:
    $statement = <<<'TEXT'
    I will not be $expanded.
    Whitespace is still your friend.
    TEXT; 

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tikila View Post
    little note for Pmw57:

    Nice article on your blog,however i encountered some syntax errors
    Sorry to say but that's not my blog. It's just a pae I found while googling that appeared to best answer the issue.

    Quote Originally Posted by Tikila View Post
    ... when i tried the codes below:

    PHP Code:
    $literal = 'that will not change';
    $statement = 'a string $literal.';
    $output = eval(sprintf('<?php return <<<OUTPUT
    %s
    OUTPUT; ?>', 
    $statement));
    and

    PHP Code:
    $statement = <<<'TEXT'
    I will not be $expanded.
    Whitespace is still your friend.
    TEXT; 
    These are more recent features and can depend on the version of PHP that you are using.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tikila, don't try this code. It a most stupid thing that can be ever done. You can ruin whole site with it.

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As hash suggested,
    PHP Code:
    if( isset( $_GET['city'] ) )
    {
    content_col="[city] weather,[city] events, [city] shows" // db column
    print str_replace("[city]" htmlspecialchars$_GET['city']  ,  $content_col );


  11. #11
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Not sure why shrap is using this tone. It is late so I am forgetting the term, but it is a shortcut so they dont need a php tag to hold a closing bracket...
    PHP Code:
    <?php
     
    if(blah){ <<<AAA
    <form ...></form>
    AAA;} ?> #it may be END AAA
    // is the same as
    <?php
     if(blah){?>
     <form ...></form>
     <?php } ?>
    I never liked using the shortcut.. The value after the three <'s can be any string by the way
    Ryan B | My Blog | Twitter

  12. #12
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heredoc

    It's just another way to use strings, nothing to do with closing brackets.

  13. #13
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heredoc is all right. I meant other part of that code.

    but heredoc won't solve the problem (if any). it's just different form of string syntax. nothing new with variables expanding.

  14. #14
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    heredoc

    It's just another way to use strings, nothing to do with closing brackets.
    ^5 hash.
    I have seen it used to output html - like I was trying to show in my example
    Ryan B | My Blog | Twitter

  15. #15
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    ^5 hash.
    I have seen it used to output html - like I was trying to show in my example
    Ah, I see, that's no different to using echo as far as I am aware. Guess it looks better, but personally I don't like heredoc style.
    PHP Code:
    echo <<<AAA
    <h1>asdf</h1>
    AAA;

    echo 
    "<h1>asdf</h1>"

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to print array values or unescaped " sign...

  17. #17
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to print array values
    PHP Code:
    echo "$hash[string] and $list[0]"
    what I am doing wrong?


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
  •