简体   繁体   中英

How do I call PHP variables in mySQL?

My website has many pages. All these pages have the same exact header at the top, displaying the logged-in user's options and links to different sections of the website.

I realized that if I ever want to make changes to the appearance of this header, I'd need to go through and apply the changes to each and every page. This seems like an issue I could avoid, so I decided to store the code in a mySQL data point, and simply echo it back on each page.

So far, it worked out impressively well, but any PHP code I have within it (like displaying the user's name) only return the physical code itself, not the result.

Calling the code works just fine, so here's the actual mySQL entry that's being called:

<table width="1332" border="0" cellspacing="0"><tr>
<td width="1002" style="background-color:#4DDB4D;">
<a href="http://www.AGLgaming.com/login/home"><img src="http://i378.photobucket.com/albums/oo223/Abyssion180/AGLLogoAlternate.png" alt="AGL Logo" width=170 height=90 align="left" /></a>
<img src="http://i378.photobucket.com/albums/oo223/Abyssion180/AGLBanner.png" width=825 height=90 />
</td>
<td width="330" style="background-color:#4DDB4D" align="right">
<div class="ex1">
<a href="http://www.AGLgaming.com/login/home/profile"><font color="blue" size="5">' . $user[0][0] . '</font></a>,<br>
<font size="1">Logged in: </font><a href="http://www.AGLgaming.com/login/home/logout"><font size="2" color="blue">Logout</font></a>
</div></td></tr>
<tr><td valign="center" style="background-color:#6EFF6E;height:31px;">
<div align="left"> ||
<a href="http://www.AGLgaming.com/login/home/forums"><font color="blue" size="5">Forums</font></a> |||
<a href="http://www.AGLgaming.com/login/home/guides"><font color="blue" size="5">Guides</font></a> |||
<a href="http://www.AGLgaming.com/login/home/external"><font color="blue" size="5">External</font></a> |||
<a href="http://www.AGLgaming.com/login/home/members"><font color="blue" size="5">Members</font></a> |||
<a href="http://www.AGLgaming.com/login/home/tournament"><font color="blue" size="5">Tournament</font></a> |||
<a href="http://www.AGLgaming.com/login/home/constitution_of_the_AGL"><font color="blue" size="5">Constitution of the AGL</font></a></div>
</td>
<td valign="center" style="background-color:#6EFF6E;height:31px;">
<div class="ex1" align="right">
<a href="http://www.AGLgaming.com/login/home/profile"><font color="blue" size="3">Profile</font></a> |
<a href="http://www.AGLgaming.com/login/home/denarii"><font color="blue" size="3">Denarii</font></a>: ' . $user[0][7] . ' |
<a href="http://www.AGLgaming.com/login/home/messages"><font color="blue" size="3">Messages</font></a>: ' . $user[0][9]; if($user[0][10]>0)  echo '<font size="2"><b>(' . $user[0][10] . ' New!)</b></font>'; . ' |
<a href="http://www.AGLgaming.com/login/home/settings"><font color="blue" size="3">Settings</font></a>
</div>
</td></tr></table>

If i got this right, I would (don't want to be mean) really recomend you reconsider your code. Are you really querying HTML / PHP code from your MySQL Database? If so, let me recomend you another way to get some code for more than just onne site: Simply go and put that code into one extra php file - for example header.php and call that one with

<?php require_once('header.php');?>

Well it looks to me like what you are looking for is a sort of template system. The simplest method to achieve this is to use specific keywords inside your HTML that your PHP can then extract and replace.

For example, you could use the str_replace() function.

$processedHtml = str_replace('{__USERNAME__}',$userName,$htmlFromDB);

In this example, you would insert the string {__USERNAME__} everywhere you'd want the $userName variable to appear.


That being said, I think you are facing a rather peculiar situation and storing the HTML in your database might not be the best option. You might want to consider using PHP's include() function. This will allow you to insert an additional file into the current script. It will (as the name describes) simply include that code exactly where you put the include statement. This will allow you to centralize your shared header code in one file and simply "include" it everywhere you need and you only need to make edits to one file.

What you should be doing rather than using a database to so this would be php's include function. Suppose You had the following code in a file called header.php

Awesome header
<?php 
    echo "working code";
?>

In each of your other files you could put

<?php
    include "header.php";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM