Reputation: 201
I have the blow code saved in database
<script type="text/javascript" src="$url/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="$url/js/ui.selectmenu.js"></script>
I want to fetch this code from database and then assign it to a variable and then I want to use $url which is defined above in my code in file.
So here is the scenario
$url is defined in file example.php foreg $url = 'http://localhost';
fetch a piece of code from database (the code above)
assign it to a variable foreg $content = (code fetch from mysql is here)
and in the end $content should contain
<script type="text/javascript" src="http://localhost/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://localhost/js/ui.selectmenu.js"></script>
This code is just an example code and I have totally different code but scenario is kinda same. Anyone can please help.
P.S And my code could contain url() (a custom function I created in another file.)
Upvotes: 1
Views: 230
Reputation: 10599
You could do something like this:
$result = mysql_query("SELECT code FROM MyCodeTable WHERE MyColumn = 'something'");
if ($result && mysql_num_rows($result) > 0) {
$row = mysql_fetch_object($result);
$content = str_replace('$url', $url, $row->code);
}
Notes: Ideally you wouldn't fetch your data from MySQL like this, but use another method like PDO instead. Also, like Seagull illustrated in his example, using str_replace
is a safer way to do this rather than eval because you should not trust the data you store in your database.
Upvotes: 2