简体   繁体   English

在PHP中添加margin-left脚本

[英]Add margin-left, script inside php

The problem is. 问题是。 I want to inset margin-left: 20%; 我要插入左边距:20%; to maincontent, when the user is logged in. 当用户登录时显示为maincontent。

No i dont have a problem with syntax, i can get it done in js, but im told to use php. 不,我在语法上没有问题,我可以在js中完成,但是我被告知要使用php。 So i also added a class in css and to my body. 因此,我还在CSS和我的身体中添加了一个类。

<?php
if ($_SESSION['username']){


include("/view/leftmenu.php");

}
?>

How do i activate css in php? 如何在php中激活CSS?

As revealed by the syntax highlighting, you're trying to put a single quote inside a single-quoted string. 正如语法高亮显示的那样,您试图将单引号放在单引号字符串中。

Your options are: 您的选择是:

  1. Escape the single quotes with \\' . \\'转义单引号。
  2. Use double quotes instead. 请改用双引号。
  3. Use ?> ... <?php instead of echo . 使用?> ... <?php而不是echo
  4. Don't do this with JavaScript at all! 根本不用JavaScript做到这一点! You control the server side; 您控制服务器端; why not just add a class to the body like logged-in , and have a CSS rule like body.logged-in #maincontent { margin-left: 20%; } 为什么不只是像logged-in一样向主体添加一个类,并像body.logged-in #maincontent { margin-left: 20%; }这样拥有CSS规则body.logged-in #maincontent { margin-left: 20%; } body.logged-in #maincontent { margin-left: 20%; } ? body.logged-in #maincontent { margin-left: 20%; }

(For what it's worth your JavaScript is invalid too; you need to quote the 20% . Percentages aren't legal JS.) (对于您的JavaScript而言,这也是无效的;您需要引用20% 。百分比不是合法的JS。)

Put your JavaScript in double quotes, with single quotes inside. 将JavaScript放在双引号中,并在其中加上单引号。 You may want to run your include first as well. 您可能还想先运行您的include include does not need () . include不需要() Your code could look more like: 您的代码可能更像:

<?php
session_start(); // has to be run before any headers are sent
if(isset($_SESSION['username'])){
  include '/view/leftmenu.php';
  echo "<script type='text/javascript'>$('#maincontent').css('margin-left', '20%');</script>";
}
?>

The better solution however would look more like: 但是,更好的解决方案更像是:

<?php
session_start(); $marginLeft = '0'; // $marginLeft should be your default
if(isset($_SESSION['username'])){
  $marginLeft = '20%';
  include '/view/leftmenu.php';
}
echo "<!DOCTYPE html><html><head><style type='text/css'>".
"#maincontent{margin-left:$marginLeft;}</style></head><body>".
"<div id='maincontent'>content</div></body></html>";
?>

A better approach yet, would look like: 更好的方法如下:

<?php
session_start(); $className = 'withoutMargin'; // $marginLeft should be your default
if(isset($_SESSION['username'])){
  $className = 'withMargin';
  include '/view/leftmenu.php';
}
?>
<!DOCTYPE html>
<html>
  <head>
    <style type='text/css'>
      .withoutMargin{
         margin-left:0;
       }
       .withMargin{
         margin-left:20%;
       }
    </style>
  </head>
<body>
<?php echo "  <div class='$className'>"; ?>
  <!-- your content here -->
  </div>
</body>
</html>

Note: Your code above does not have to look exactly like this. 注意:上面的代码不必看起来完全像这样。 This just illustrates concept. 这仅说明了概念。 You would have more code testing for submit to be set and so forth. 您将进行更多的代码测试以设置提交等等。 Also, with the second example, which I recommend, I would use external CSS, so it is cached by the user's Browser. 另外,在第二个示例中,我建议使用外部CSS,因此它由用户的浏览器缓存。

20% needs to be in quotes: 20%需要用引号引起来:

$('#maincontent').css('margin-left', '20%');

This also applies to px, em, pt, and any other form of CSS unit of measurement. 这也适用于px,em,pt和任何其他形式的CSS度量单位。

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

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