0

我正在尝试在我的 sitewide-variables.php 中创建一个函数,该函数将在页面头部回显一些额外的 css(不需要包含在每个页面中),同时包含 file.php(例如,如果页面为 1,则在页面的正文部分中使用头部所需的 css,显然,如果页面为 2,则根本不需要回显或包含任何内容。

类似于以下内容

<head><?php include("/variables/sitewide-variables.php"); ?>
<link href="css/style_new.css" rel="stylesheet" type="text/css">
<?php $number = 1; echo $css?>
</head>
<body>
<?php $number = 1; include $file.php?>
</body>

我对此的理解是在 sitewide-variables.php 中创建类似于以下内容的内容

<?php 
$css[$number] = 'selected';
echo isset($css[1]) ? $css['some css code'] : '' 
?>

这是我能做到的。

我知道以上所有可能只是一个巨大的错误,但我希望这足以解释我实际想要实现的目标。

您可能已经猜到了,我对 php 的了解非常有限。

4

4 回答 4

0

似乎有点多余,为什么不简单地有一个预设的 css 位数组:

$css = array(
   'style1' => '...',
   'style2' => '...',
   ...
);

作为您的“css存储”,然后在页面的适当位置,您只需

echo $css['styleX'];

无需设置更多全局变量来访问其他全局变量。

于 2012-12-11T15:02:01.510 回答
0

如果我理解得很好,你有一个带有数字的变量,并且根据这个数字,你想在你的标题中添加一个 CSS。

您可以使用各种方法,例如 IF 或 SWITCH 函数。关于您想要处理它的方式,我会在“sitewide-variables.php”中添加一个函数,如下所示:

function mycss($number) {
    switch($number) {
        case 1 : $css = ' here goes css instructions for page 1 '; break;
        case 2 : $css = ' here goes css instructions for page 2 '; break;
        default : $css = ''; break; // no page, no css
    }
    return $css;
}

这个函数将返回一个带有你的 CSS 的字符串,所以在你的页面中你可以添加这个:

<head><?php $number = 1; include("/variables/sitewide-variables.php"); ?>
<link href="css/style_new.css" rel="stylesheet" type="text/css">
<?php echo mycss($number); ?>
</head>
于 2012-12-11T15:32:33.440 回答
0

这会解决你的问题吗?

<head>
<?php include("/variables/sitewide-variables.php"); ?>
<link href="css/style_new.css" rel="stylesheet" type="text/css">
<?php echo $css?>
</head>
<body>
<?php include ('file.php'); ?>
</body>

然后在您的“sitewide-variables.php”文件中:

$css = "<style> ['insert css code here'] </style>";
于 2012-12-11T15:31:36.397 回答
0

我已将以下内容添加到 sitewide-variables.php

if ($number = 1)
    {$slider_css = '<style>
/* ~~ Slider ~~ */
.slider_panel {height:270px;margin-top:11px;clear:both;}
.slider {width:950px; height:250px; padding-top:10px;overflow:hidden;}
#fader {width:950px;height:250px;}
#fader ul {list-style:none;margin:0px;padding:0px;height:215px;width:950px;}
#fader ul li {width:950px;height:250px;display:none;}
#fader ul li.active {display:block;}
#faderNav {position:relative;float:right;margin-right:20px;width:150px;}
#faderNav a {background: none repeat scroll 0 0 #eee;border-radius: 3px 3px 3px 3px;-webkit-border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;-o-border-radius: 3px 3px 3px 3px;display:block; float:left;height: 15px;margin: 10px 4px;width: 15px;outline:none;}
#faderNav a.active {background:#06222e;}
</style>';
$slider = $getPath . '/var/www/vhosts/mysite.com/httpdocs/trial/includes/top_slider.php';}
     else
     {$slider_css = ' ';
     $slider = ' ';}

以及我网站上每个页面的以下内容

<head><?php $number = 1; include("/variables/sitewide-variables.php"); ?>
<link href="css/style_new.css" rel="stylesheet" type="text/css">
<?php echo $slider_css?>
</head>
<body>
<?php include $slider ?>
</body>

这一切都开始以我需要的方式工作。

感谢所有试图帮助我的人!!!

于 2012-12-11T18:49:59.353 回答