PHP:数组函数
我的页面上有一个选择菜单,它以数组形式列出以下系统字体:PHP:数组函数,php,Php,我的页面上有一个选择菜单,它以数组形式列出以下系统字体: $default = array( 'Arial' => 'Arial', 'Courier' => 'Courier New', 'Georgia' => 'Georgia', 'Helvetica' => 'Helvetica', 'Impact' => 'Impa
$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'
);
也许您应该将键交换为值,它将执行您想要的操作,如下所示:
$default = array(
'Arial' => 'Arial, sans-serif',
'Courier New' => 'Courier New, monospace',
...
);
这就应该奏效了。通常按索引和返回值搜索数组。因此,搜索$default['Arial']将产生“Arial,sans serif”。我希望这就是您想要的。在选项面板中,使用以下代码:
// 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>
<?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; ?>;}
然后在CSS
中使用以下代码:
// 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>
<?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中得到任何错误吗?我已经尝试了你的确切清单,看起来工作正常。另外,你能在这里给出,选择选项的控制结果是什么?我的意思是,PHP脚本的最终输出是什么?您如何实际使用
$default
数组?如果您试图更改css文件中的字体系列,您可以将css文件扩展名重命名为.PHP,并使用变量而不是字体系列,如:{font family:;}。我不知道你要找的是什么,但你可以使用默认字体,并在$\u会话中进行检查,例如,如果字体未设置为显示默认字体,这就是他现在要找的。这不是他要的for@MerianosNikosOP到底在寻找什么?这个答案是我如何理解这个问题的,所以我有点困惑。我想他有一个简短的管理面板,允许最终用户选择应用程序前端使用的字体系列。然后,当最终用户选择字体系列时,该值存储在数据库中,然后每次生成首页时,都使用该选项。问题可能来自“选择选项”列表中的“管理”面板。这个html元素是以错误的方式生成的。如果要这样做,请不要忘记清理输入,最好在服务器端执行更多操作(例如,Arial
,然后用PHP更改:array('Arial'=>'Arial,sans serif')
等)。这是必须的。我在这里展示了它的方法。根据所使用的平台,可能已经内置了消毒功能。