Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Replace variables in data retreived from MySql data

gballou65gballou65 Posts: 4Member
I'm looking for a way to replace variables place in data stored in a mysql table.

Specifically I am storing links for buttons for advertisers in a db table. Here is an example. Assume that banner_link in my table contains a value of

Hello $msg_txt

<?php
include ("config.php");
include ("opendb.php");

$msg_text = "World";

$query = "SELECT text_link, banner_link, company_name FROM advertisers order by company_name";
$result = mysql_query($query, $cxn) or die ("Couldn't execute query.");

/* Display results in a table */
echo "<table cellspacing='10' border='1' cellpadding='2'
width='100%'>";
while($row = mysql_fetch_assoc($result))
{
/* display row for each advertiser */
echo "
";
echo " {$row["banner_link"]}


";
}
echo "
";
include ("closedb.php");
?>


In this example I want Hello World echoed out but what I get is Hello $msg_txt.

Any help would be appreciated.


Greg

Comments

  • evangozalievangozali Posts: 4Member
    Hello Greg,

    If you want to echo "Hello world" i think you should put the $msg_txt in the php bracket, not outside. What you write outside the php bracket will be printed as is. So the code should look like:

    [code]<?php
    include ("config.php");
    include ("opendb.php");

    $msg_text = "World";

    echo "Hello $msg_text"; //print here;
    $query = "SELECT text_link, banner_link, company_name FROM
    advertisers order by company_name";
    $result = mysql_query($query, $cxn) or die ("Couldn't execute
    query.");

    /* Display results in a table */
    echo "<table cellspacing='10' border='1' cellpadding='2'
    width='100%'>";
    while($row = mysql_fetch_assoc($result))
    {
    /* display row for each advertiser */
    echo "
    ";
    echo " {$row["banner_link"]}


    ";
    }
    echo "
    ";
    include ("closedb.php");
    ?>[/code]


    CMIIW. And one more thing, i think you should declare $msg_text before you print it.
  • gballou65gballou65 Posts: 4Member
    The problem though is that "Hello $msg_txt" is actually the string that is in banner_link that I am selecting.

    So the output from the line;

    echo " {$row["banner_link"]}


    is coming out "Hello $msg_txt" but I want it to echo out as "Hello World". In other words I have a variable buried inside of another variable.

    Greg


    : Hello Greg,
    :
    : If you want to echo "Hello world" i think you should put the
    : $msg_txt in the php bracket, not outside. What you write outside the
    : php bracket will be printed as is. So the code should look like:
    :
    : [code]: <?php
    : include ("config.php");
    : include ("opendb.php");
    :
    : $msg_text = "World";
    :
    : echo "Hello $msg_text"; //print here;
    : $query = "SELECT text_link, banner_link, company_name FROM
    : advertisers order by company_name";
    : $result = mysql_query($query, $cxn) or die ("Couldn't execute
    : query.");
    :
    : /* Display results in a table */
    : echo "<table cellspacing='10' border='1' cellpadding='2'
    : width='100%'>";
    : while($row = mysql_fetch_assoc($result))
    : {
    : /* display row for each advertiser */
    : echo "
    ";
    : echo " {$row["banner_link"]}

    :
    ";
    : }
    : echo "
    ";
    : include ("closedb.php");
    : ?>[/code]:
    :
    :
    : CMIIW. And one more thing, i think you should declare $msg_text
    : before you print it.
    :

  • gballou65gballou65 Posts: 4Member
    Ok, I figured it out. The str_replace() function does the trick. In my data I will use MSGTEXT instead of $msg_txt (ie "Hello MSGTEXT")

    In code I can then do the following;

    $msg_txt = "World";

    $banner_link = $row["banner_link"];
    $newBanner = str_replace("MSGTEXT", $msg_txt, $banner_link);
    echo " $newBanner
    ";

    This will correctly display

    Hello World

  • evangozalievangozali Posts: 4Member
    : Ok, I figured it out. The str_replace() function does the trick. In
    : my data I will use MSGTEXT instead of $msg_txt (ie "Hello MSGTEXT")
    :
    : In code I can then do the following;
    :
    : $msg_txt = "World";
    :
    : $banner_link = $row["banner_link"];
    : $newBanner = str_replace("MSGTEXT", $msg_txt, $banner_link);
    : echo " $newBanner
    ";
    :
    : This will correctly display
    :
    : Hello World

    :

    There is another way to do it, you can use eval function. See [link=http://id2.php.net/manual/en/function.eval.php]here[/link] for example.
Sign In or Register to comment.