简体   繁体   English

存储在php变量中的html代码中的php代码,这怎么可行?

[英]php code within an html code stored inside a php variable, how can this work?

I am currently making a simple website, I have a file called 'functions.php', where I store some simple functions and this piece of code 我目前正在创建一个简单的网站,我有一个名为'functions.php'的文件,其中我存储了一些简单的函数和这段代码

<div id=\"topContactPan\">

<div id=\"topMenuPan\">
  <div id=\"topMenuLeftPan\"></div>

  <div id=\"topMenuMiddlePan\">
<a href= <?Php if(!admin_class::isadmin()){ 
echo "\"userprofile.php\" >User Profile</a></li>";} 
else { echo "\"adminprofile.php\" >Admin</a>"; } ?> 

I needed to escape all the " so it would interpret properly since this is within a php file, all of this is stored inside a variable $top, so whenever i create a new page of my website, i just echo $top and echo $bottom and theres the top header and bottom footer, only the content is missing. 我需要逃避所有“所以它会正确解释,因为这是在一个php文件中,所有这些都存储在变量$ top中,所以每当我创建我的网站的新页面时,我只是回显$ top和echo $底部和顶部页眉和底部页脚,只有内容丢失。

but with the code above, I wanted one of the links on the navigation bar to switch where is is pointing to depending if the one who is logged in is an admin or a regular user. 但是使用上面的代码,我想要导航栏上的一个链接切换指向的位置,具体取决于登录的用户是管理员还是普通用户。 But it only seems that when I click the link, the header becomes http://localhost/YMMLS/<?Php . 但似乎只有当我点击链接时,标题http://localhost/YMMLS/<?Php变成http://localhost/YMMLS/<?Php

Any suggestions? 有什么建议么? thanks so much 非常感谢

make 2 files. 制作2个文件。

header.php and footer.php header.php和footer.php

inside of each of those write your html as you normally would - then on your index or what ever page just say 在每个人的内部写你的HTML通常 - 然后在你的索引或什么页面只是说

include('header.php');
<p>YOUR HTML PAGE AND DOC<p>
include('footer.php');

No need to go through escaping everything. 无需通过逃避一切。

I would do like this in your functions.php: 我会在你的functions.php中这样做:

<?php
function template() {
    ?>
    <div id="topContactPan">

    <div id="topMenuPan">
    <div id="topMenuLeftPan"></div>

    <div id="topMenuMiddlePan">

    <a href="<?php checklink(); ?>"> <?php checkuser(); ?></a>
    <?php   
}

function checklink() {
    if(!admin_class::isadmin()) {
        echo "userprofile.php";
    } else {
        echo "adminprofile.php";
    }
}
function checkuser() {
    if(!admin_class::isadmin()) {
        echo "User Profile";
    } else {
        echo "Admin";
    }
}
?>

Then in your file you just include it and call its function : 然后在您的文件中,您只需包含它并调用其函数:

<?php
include('functions.php');
template();
?>

It is not the best solution maybe. 它可能不是最好的解决方案。 :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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