user1752759
user1752759

Reputation: 643

PHP: Array Function

I have a select-menu on my page, that lists the following system fonts in an array:

$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'
);

When the user selects one of these fonts from the drop-down/select-menu, the font-face is output into the CSS head section of my site, like so:

p {font: 12px Arial;}

This works perfectly fine, but what I am hoping to do is change the CSS to also output fall-backs:

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

As I'm fairly new to PHP, using a comma and the fall-backs like the script below won't work and I was wondering how would I be able to add another font or fall-back to the array?

$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'
);

Upvotes: 0

Views: 262

Answers (2)

KodeFor.Me
KodeFor.Me

Reputation: 13511

In your options panel, use the following code:

// 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>

The above will generate in your admin panel an HTML select element that will look like the following:

<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>

Then in your CSS use the following code:

<?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; ?>;}

Finally, in case you are using a PHP file as your CSS file for dynamic content don't forget to add this code on the very very top part of your file:

<?php
    header('Content-Type: text/css');
?>

Upvotes: 1

macl
macl

Reputation: 995

Maybe you should exchange keys for values and it will do what you want, like this:

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

And that should do the trick. You normally search arrays by indexes and return values. So searching $default['Arial'] will yield "Arial, sans-serif". I hope this is what you wanted.

Upvotes: 0

Related Questions