0

我的页面上有一个选择菜单,其中列出了数组中的以下系统字体:

$default = array(
    'Arial'             => 'Arial',
    'Courier'           => 'Courier New',
    'Georgia'           => 'Georgia',
    'Helvetica'         => 'Helvetica',
    'Impact'            => 'Impact',
    'Palatino'          => 'Palatino',
    'Tahoma'            => 'Tahoma, Geneva',    
    'Times'             => 'Times New Roman',
    'Verdana'           => 'Verdana, Geneva'
);

当用户从下拉/选择菜单中选择其中一种字体时,字体将输出到我网站的 CSS 头部部分,如下所示:

p {font: 12px Arial;}

这工作得很好,但我希望做的是改变 CSS 也输出后备:

p {font: 12px Arial, san-serif;}

由于我对 PHP 还很陌生,因此使用逗号和像下面的脚本这样的后备将不起作用,我想知道如何向数组添加另一种字体或后备?

$default = array(
    'Arial, sans-serif'             => 'Arial',
    'Courier New, monospace'        => 'Courier New',
    'Georgia, serif'                => 'Georgia',
    'Helvetica, sans-serif'         => 'Helvetica',
    'Impact, sans-serif'            => 'Impact',
    'Palatino, serif'               => 'Palatino',
    'Times New Roman, serif'        => 'Times New Roman',
    'Verdana, Geneva, sans-serif'   => 'Verdana, Geneva'
);
4

2 回答 2

1

在您的选项面板中,使用以下代码:

// This is the value you retrieve from your database.
// Change it according to your code needs
$current_font_family = $db_options_result['font_family'];

$default = array(
    'Arial, sans-serif'             => 'Arial',
    'Courier New, monospace'        => 'Courier New',
    'Georgia, serif'                => 'Georgia',
    'Helvetica, sans-serif'         => 'Helvetica',
    'Impact, sans-serif'            => 'Impact',
    'Palatino, serif'               => 'Palatino',
    'Times New Roman, serif'        => 'Times New Roman',
    'Verdana, Geneva, sans-serif'   => 'Verdana, Geneva'
);

?>
<!-- Change the attributes of the select element according to your -->
<!-- code needs -->
<select name="fontFamily">
<?php
    foreach($default as $font_k => $font_v)
    {
?>
<!-- Remember to change the $current_font_family to your script variable name -->
<option value="<?php echo $font_k; ?>"<?php echo ($current_font_family == $font_k ? ' selected="selected"' : ''); ?>><?php echo $font_v; ?></option>
<?php
    }
?>
</select>

以上将在您的管理面板中生成一个HTMLselect 元素,如下所示:

<select name="fontFamily">
    <option value="Arial, sans-serif">Arial</option>
    <option value="Courier New, monospace">Courier New</option>
    <option value="Georgia, serif">Georgia</option>
    <option value="Helvetica, sans-serif">Helvetica</option>
    <option value="Impact, sans-serif">Impact</option>
    <option value="Palatino, serif">Palatino</option>
    <option value="Times New Roman, serif">Times New Roman</option>
    <option value="Verdana, Geneva, sans-serif">Verdana, Geneva</option>
</select>

然后在您CSS使用以下代码:

<?php
    // This is the value you retrive from your database.
    // Change it according to your code needs

    $current_font_family = $db_options_result['font_family'];
?>
p {font: 12px <?php echo $current_font_family; ?>;}

最后,如果您使用PHP文件作为CSS动态内容的文件,请不要忘记在文件的最顶部添加此代码:

<?php
    header('Content-Type: text/css');
?>
于 2012-12-22T10:54:03.473 回答
0

也许你应该交换值的键,它会做你想做的,像这样:

$default = array(
    'Arial'             =>    'Arial, sans-serif',
    'Courier New'       =>    'Courier New, monospace',
   ...
);

这应该可以解决问题。您通常按索引搜索数组并返回值。因此搜索 $default['Arial'] 将产生“Arial, sans-serif”。我希望这是你想要的。

于 2012-12-22T10:33:32.043 回答