PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Replace variables in data retreived from MySql data Posted by gballou65 on 3 Dec 2008 at 2:06 PM
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 "<tr>\n";
echo " <td>{$row["banner_link"]}</td>\n
</tr>\n";
}
echo "</table>\n";
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
Report
Re: Replace variables in data retreived from MySql data Posted by evangozali on 4 Dec 2008 at 1:30 AM
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:

<?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 "<tr>\n";
    echo " <td>{$row["banner_link"]}</td>\n
    	</tr>\n"; 
   }
   echo "</table>\n";
   include ("closedb.php");
 ?>



CMIIW. And one more thing, i think you should declare $msg_text before you print it.
Report
Re: Replace variables in data retreived from MySql data Posted by gballou65 on 4 Dec 2008 at 9:05 AM
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 " <td>{$row["banner_link"]}</td>\n

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:
:
:
: <?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 "<tr>\n";
:     echo " <td>{$row["banner_link"]}</td>\n
:     	</tr>\n"; 
:    }
:    echo "</table>\n";
:    include ("closedb.php");
:  ?>
:
:
:
: CMIIW. And one more thing, i think you should declare $msg_text
: before you print it.
:

Report
Re: Replace variables in data retreived from MySql data Posted by gballou65 on 4 Dec 2008 at 1:27 PM
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 " <td>$newBanner</td>\n";

This will correctly display

<td>Hello World</td>\n
Report
Re: Replace variables in data retreived from MySql data Posted by evangozali on 10 Dec 2008 at 1:43 AM
: 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 " <td>$newBanner</td>\n";
:
: This will correctly display
:
: <td>Hello World</td>\n
:

There is another way to do it, you can use eval function. See here for example.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.