user1752759
user1752759

Reputation: 643

Output to CSS multiple backgrounds using PHP

On my template I have a number of fields where the user(s) can choose and select from few options to use as a background on their site.

There are two separate backgrounds, called from the ID's of background-one and background-two, stored in separate arrays.

To output the first background:

<?php $background = of_get_option('background-one'); {  
    if ($background['color'] && $background['image']) {
    echo 'body {
    background: ' . $background['color'] . ' url(' . $background['image']. ') ' .$background['repeat']. ' ' .$background['position']. ' ' .$background['attachment']. ';';
echo '
}';
    }

    else if ($background['color']) {  
    echo 'body {
    background: ' . $background['color']. ';';
    echo '
}';         
    }

    else if ($background['image']) {
    echo 'body {
    background: ' . 'url(' . $background['image']. ') ' .$background['repeat']. ' ' .$background['position']. ' ' .$background['attachment']. ';';
    echo '
}'; 
    };
}
?>

However, as I'm extremely new to PHP, my question is how would I be able to output the second background?

Upvotes: 0

Views: 195

Answers (1)

GrayB
GrayB

Reputation: 1000

<?php 
    $backgroundArray1 = of_get_option('background-one');
    $backgroundArray2 = of_get_option('background-two'); 
    $backgroundText = false;

    if($backgroundArray1['image'] && $backgroundArray2['image'])
    {
      $backgroundText = 'background: url(images/' . $backgroundArray1['image'] . ') no-repeat top center scroll, url(images/' . $backgroundArray1['image'] . '.jpg) repeat top left scroll;';
    }
    else if($backgroundArray1['color'] && $backgroundArray2['color']) 
    {
       $backgroundText = 'background: linear-gradient(to bottom, #'.backgroundArray1['color'].', #'.backgroundArray2['color'].')';
    }
    else if($backgroundArray1['color'] && $backgroundArray1['image']) 
    {
      $backgroundText = 'background: ' . $backgroundArray1['color'] . ' url(' . $backgroundArray1['image'] . ') ' . $backgroundArray1['repeat']. ' ' .$backgroundArray1['position']. ' ' .$backgroundArray1['attachment']. ';';
    }
    else if($backgroundArray1['color']) 
    {  
      $backgroundText = 'background: ' . $backgroundArray1['color']. ';';
    }
    else if($backgroundArray1['image']) 
    {
      $backgroundText = 'background: ' . 'url(' . $backgroundArray1['image']. ') ' .$backgroundArray1['repeat']. ' ' .$backgroundArray1['position']. ' ' .$background['attachment']. ';';
    }

    if($backgroundText)
    {
      echo "body { " . $backgroundText . " } ";
    }
}
?>

Upvotes: 2

Related Questions