Howdy, Stranger!

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

Categories

Replace variables in data retreived from MySql data

gballou65gballou65 Member Posts: 4
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 Member Posts: 4
    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 Member Posts: 4
    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 Member Posts: 4
    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 Member Posts: 4
    : 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.