1

I've decided to extract the params for my module directly from the database, to put them in my variable stylesheet. I know this is not the recommended way, but for learning sake I find this to be a good test.

I am doing this like this:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select($db->quoteName('params'))
    ->from($db->quoteName('#__modules'))
    ->where($db->quoteName('module') . ' LIKE ' . $db->quote('mod_ext_superfish_menu'));

$db->setQuery($query);
$results = $db->loadResult();
$params = json_decode($results);

var_dump($params);

This works well and I do get all the variables into my CSS. all the text that used to be in my list items, has now disappeared-- so, the styling is right, but the text that's supposed to be in the styled items has vanished. What have I done wrong?

To add, the styling is done as such:

.menu {
    line-height:    1.0;

    <?php // WIDTH
    if($params->menuWidth != "") {
        echo "width: ".$params->gmenuWidth."px;";
    } 
    ?>

    <?php // HEIGHT
    if($params->menuHeight != "") {
        echo "height: ".$params->menuHeight."px;";
    }
    ?>



    <?php //margin top & margin left 
    if($params->menuMarginLeft != "") {
    echo "margin-left: ".$params->menuMarginLeft."px; ";
}
if($params->menuMarginTop != "") {
    echo "margin-top: ".$params->menuMarginTop."px; ";
}
?>

}

2

You can't simply add CSS to your PHP file and expect it to work.

For example, the following will just NOT work:

<?php  
   .menu {
       font-size: 12px;
   }
?>

I have already in a previous answer explained how to use addStyleDeclaration(). I know you're trying to learn which is great, but when someone recommends a method, don't ignore it ;)

So, firstly, remove all your code and add the following:

$doc = JFactory::getDocument();

$css = '.menu { line-height: 1.0; }';

if($params->menuWidth != '') {
    $css .= '.menu { width: ' . $params->gmenuWidth . 'px; }';
}
if($params->menuHeight != '') {
    $css .= '.menu { height: ' . $params->menuHeight . 'px; }';
}
if($params->menuMarginLeft != '') {
    $css .= '.menu { margin-left: ' . $params->menuMarginLeft . 'px; }';
}
if($params->menuMarginTop != '') {
    $css .= '.menu { margin-top: ' . $params->menuMarginTop . 'px; }';
} 

$doc->addStyleDeclaration($css);

You will see above the first $css is as is. But the other ones inside the if statements have a . before the = symbol. This means that the variable name be be used multiple times without having an effect on eachother.

Finally, for adding CSS that has value based on variables/parameters, stick to this method and do not use any other method.

Hope this helps

-4

Actually it does work :) Here's what I did:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select($db->quoteName('params'))
    ->from($db->quoteName('#__modules'))
    ->where($db->quoteName('module') . ' LIKE ' . $db->quote('mod_ext_superfish_menu'));

$db->setQuery($query);

$results = $db->loadResult();

//var_dump($results);

$params = json_decode($results);

var_dump($params);


/*** MAIN MENU SETTINGS ***/

.<?php echo $module->module; //div name is equal to module ?> {

font-size: <?php if($params->fontSize != "") { echo "".$params->fontSize."px;"; } else { echo "14px;"; } ?>

background-color: <?php  
    if($params->entireMenuBgColor != "") {
        echo "".$params->entireMenuBgColor.";";
    }
?>

<?php 
    if($params->backgroundImage != "-1") {
        echo "background-image: url('". JURI::ROOT(false, "")."images/menu/".$params->backgroundImage."');";
    }

?>

background-repeat: repeat-x;

<?php if($params->fontStyle != "") {
    echo $params->fontStyle;    
} else { 
    echo "normal"; 
}   ?>

<?php if($params->fontFamily != "") {
    echo "fontFamily: ".$params->fontFamily."";
}   ?>

}


.sf-menu, .sf-menu * {
    margin:             0;
    padding:            0;
    list-style:         none;
    <?php if($params->textPushEffect == 1 ) {
        echo "text-shadow: 0px 1px 0px rgba(255,255,255,.3), 0px -1px 0px rgba(0,0,0,.7);";
    }  ?>
} etc etc
  • 2
    This is a comment to another answer, not an answer to the question. – GDP Nov 5 '14 at 15:48
  • -1 purely due to the bad coding practices having already provided you with the answer – Lodder Nov 5 '14 at 18:05
  • This is a comment, not an answer. – Seth Warburton Nov 7 '14 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.