2012-03-14 23 views
0

我使用this php code代碼來寫出我的css與填充變量。在一個php文件中設置數組並訪問另一個php文件問題

我想默認的陣列搬出這個代碼,並將其提升到settings.php文件爲我的網站,但是當我做我收到此錯誤信息:

<b>Warning</b>: extract() [<a href='function.extract'>function.extract</a>]: First argument should be an array in <b>/home/drawapl1/public_html/ezamazon/demo/css/stylesheet.php</b> on line <b>19</b> 

我的代碼如下:

在index.php文件:

<?php 
include("settings.php"); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<link href="css/stylesheet.php" rel="stylesheet" type="text/css" media="screen" /> 

在stylesheet.php:

/* get the stylesheet */ 
$stylesheet = @is_file($_GET['stylesheet']) && strtolower(substr(strrchr($file_name,'.'),1)) == 'css' ? $_GET['stylesheet'] : 'global.css'; 

/* set the header information */ 
//will be output as css 
header('Content-type: text/css'); 
//set an expiration date 
$days_to_cache = 10; 
header('Expires: '.gmdate('D, d M Y H:i:s',time() + (60 * 60 * 24 * $days_to_cache)).' GMT'); 

//red css variable information 
//$red = array(
    //'body_font_size' => '10px', 
    //'body_text_color' => '#f00' 
//); 

/* extract the propery array's information */ 
extract($_GET['theme'] && ${$_GET['theme']} ? ${$_GET['theme']} : $defaultCSS); 

/* load in the stylesheet */ 
$content = preg_replace('/\$([\w]+)/e','$0',@file_get_contents($stylesheet)); 

/* spit it out */ 
echo $content; 

在settings.php配置:

$defaultCSS = array( 'body_background_colour' => 'white', 'body_text_colour' => 'black', 'body_font_size' => '1em', 'body_font_family' => 'Arial, Helvetica, sans-serif', 'h1_text_colour' => 'black', 'h2_text_colour' => 'black', 'h3_text_colour' => '#E47911', 'a_text_colour' => '#004B91', 'a_hover_text_colour' => '#2A70FC', 'news_block_background_colour' => '#EAF3FE', 'left_nav_border_colour' => '#C9E1F4', 'left_nav_h2_background_colour' => '#EAF3FE', 'button_text_colour' => '#FFFFFF', 'button_background_colour' => '#414953', 'button_hover_background_colour' => '#999999', 'price_colour' => '#990000', 'price_heading_colour' => '#666666', 'nav_links_colour' => 'black', 'nav_hover_links_colour' => '#E47911', 'hr_divider_colour' => '#DDDDDD', 'pagination_highlight_colour' => '#645538' );

任何想法的問題是什麼?

在此先感謝。

+0

爲什麼你不會使用實際的代碼**? – zerkms 2012-03-14 00:37:47

+0

你的代碼是什麼樣的,'settings.php'會在樣式表php之前加載嗎? – jeroen 2012-03-14 00:39:04

+0

'$ {$ _ GET ['theme']}'不是數組。你能否迴應'$ _GET ['theme']'的價值並在這裏發佈? – augustknight 2012-03-14 00:39:28

回答

0

確保您在使用include()之前包括settings.php

這不起作用的最可能的原因是該文件不知道看到一個$default變量,併爲該參數創建一個新的null變量。

試試這個:

//settings.php 

    /* set the dynamic information */ 
    //default css variable information 
    $default = array(
    'body_font_size' => '16px', 
    'body_text_color' => '#00f' 
); 

//main_php_file.php 

    include('settings.php'); // This is the important part 

    /* extract the propery array's information */ 
    extract($_GET['theme'] && ${$_GET['theme']} ? ${$_GET['theme']} : $default); 

也可以嘗試print_r($default)include('settings.php');後,確保$default設置正確。

0

包括index.php文件的settings.php,然後從你的HTML文檔將無法通過變量的settings.php中調用stylesheet.php到stylesheet.php

您需要包括的settings.php中stylesheet.php能夠在那裏使用它的變量。

編輯

在index.php文件

<link href="css/stylesheet.php?vars=<?=urlencode(json_encode($defaultCSS))?>" rel="stylesheet" type="text/css" media="screen" /> 

在stylesheet.php添加頂部

$defaultCSS = json_decode(urldecode($_GET['vars'])); 

你也可以使用序列化/反序列化,而不是json_encode - 但爲什麼不只是包括它,它更清潔,以保持所有服務器端

+0

有沒有辦法通過會話或其他東西? – 2012-03-14 01:34:34

+0

查看我編輯的一種方法,將它們作爲GET參數傳遞 – 2012-03-14 01:36:26