1

This is part of the PHP that is outputting HTML from a function I'm working on. I'm getting stuck on the if/else state where I need to check if a parameter is set to true or false.

If it's set to true, I have HTML I want to output, if it's false, nothing needs to happen.

<?php
/**
 * 
*
* @param array $settings
*
* @return string
*/

function PCStandardTextBlock( $settings = array() ) {

//---- Get Settings ----
//The functions default settings will be merged with what's passed in.

$settingsDefault = array(
    'small_heading' => '',
    'large_heading' => '',
    'text' => '',
    'text_mode' => 'basic', //or advanced
    'big_first_p' => false,
    'link_url' => '', 
    'link_text' => '',
    'link_new_tab' => false,
    'link_style' => 'action_link', //or button
    'photo' => '', //cloudinary id
    'image_alt_text' => '',
    'crop_gravity' => 'face',
    'icon' => '', //icn-class
    'color_scheme' => 'white', //or accent1, accent2, accent4
    'container_id' => '',
    'container_class' => '',
    'special_feature' => 'camplife_links', //or link_modal, hidden_content
    'modal_id' => '',
    'hidden_content_location' => 'another_block', //or in_this_block (see day-camps.php)
    'hidden_text' => '',
    'hidden_content_id' => '',
);

$settings = array_merge($settingsDefault, $settings);


//---- Set Variables ----
//These will allow the markup buildup to be as clean as possible.

$has_photo = strlen($settings['photo']) > 0;
$has_icon = strlen($settings['icon']) > 0;
$has_small_heading = strlen($settings['small_heading']) > 0;
$has_image_alt_text = strlen($settings['image_alt_text']) > 0;

//Note to Sal: If text mode is basic, wrap it in a <p> tag, if advanced, don't.

//If container_id is set, prepare the attribute
$has_container_id = strlen($settings['container_id']) > 0;
$possible_container_id_attribute = ($has_container_id) ? " id='{$settings['container_id']}'" : "";

//Handle the color scheme setting
switch( $settings['color_scheme'] ) {
case 'accent1': // blue
  $gcol_color_class = 'bg-color-accent1-C';
        $small_heading_color_class = 'color-white';
        $icon_color_class = 'color-white';
  break;

    case 'accent2': // green
  $gcol_color_class = 'bg-color-accent2-C';
        $small_heading_color_class = 'color-white';
        $icon_color_class = 'color-white';
  break;

case 'accent4': // yellow
  $gcol_color_class = 'bg-color-accent4-D';
        $small_heading_color_class = 'color-accent1-9';
        $icon_color_class = 'color-accent1-9';
  break;

    default; //white
        $gcol_color_class = ''; // empty
        $small_heading_color_class = 'color-base-A';
        $icon_color_class = 'color-accent1-9';
}

// Handle the special feature setting
switch( $settings['special_feature']) {

    case 'camplife_links':
        $special_feature_display = '<div class="spacer"></div>
        <div class="grid">
            <div class="gcol">
                <a target="_blank" href="https://itunes.apple.com/us/app/pine-cove-camplife/id991165025?mt=8&ign-mpt=uo%3D4">
                    <img class="width-100" src="/images/logos/badges/app-store-badge.png" alt="Download on the App Store" />
                </a>
            </div>
            <div class="gcol">
                <a target="_blank" href="https://play.google.com/store/apps/details?id=com.pinecove.camplife">
                    <img class="width-100" src="/images/logos/badges/google-play-badge.png" alt="Get it on Google Play" />
                </a>
            </div>
        </div>';
        break;

    default; // don't show anything
    $special_feature_display = ''; // Empty
}

$has_special_feature = strlen( $settings['special_feature'] ) > 0;

// Settings to call PCResponsiveImage with output set to false to save the results to a variable for inclusion in the output

$responsive_image_settings = array(
    'cloudinary_ID' => $settings['photo'],
    'aspect_ratios' => '1x1',
    'crop_gravity' => $settings['crop_gravity'],
    'screen_portion' => 'partial',
    'style_profile' => 'style4',
    'output' => false,
);

// If image alt text has been set, us it, otherwise don't set anything so responsive image can do it's thing (it sets the alt text to the filename).

if ( $has_image_alt_text ) {
    $responsive_image_settings['alt_text'] = $settings['image_alt_text'];
}

//Save PCResponsiveImage markup to a variable

$responsive_image_html = PCResponsiveImage( $responsive_image_settings );


//---- Build Output ----
//Line by line, concatenating strings with new line and tab characters.
$output  = "\n<!-- Standard Text Block -->";
$output .= "\n<div class='gcol-md-1-2 {$gcol_color_class} {$settings['container_class']}'{$possible_container_id_attribute}>";
$output .= "\n\t<div class='padbox-standard-content'>";

if( $has_photo ) {
        $output .= "\n\t<div class='small-heading-image'>";
        $output .= "\n\t\t{$responsive_image_html}";
        $output .= "\n\t</div>";
} elseif( $has_icon ) {
    $output .= "<span class='icn {$settings['icon']} $icon_color_class width-90'></span>";
}

if( $has_small_heading ) {
    $output .= "\n\t<h1 class='small-heading $small_heading_color_class'>{$settings['small_heading']}</h1>";
}

$output .= "\n\t<h2 class='heading-extended'>{$settings['large_heading']}</h2>";

if ( $settings['text_mode'] == 'basic' ) {
$output .= "\n\t\t\t\t<p>{$settings['text']}</p>"; // basic version
} elseif ( $settings['big_first_p'] === true ) {
    $output .= "\n\t\t\t\t<p class='big-p'>{$settings['text']}</p>";
}   else {
    $output .= "\n\t\t\t\t{$settings['text']}"; // advanced text mode; not big-p
}

if( $has_special_feature ) {
    $output .= "\n\t\t\t$special_feature_display";
}

$output .= "\n\t</div><!-- END padbox-standard-content -->";
$output .= "\n</div><!-- END gcol-md-1-2 -->";

//---- Return Output ----
return $output;
}

This is the function on the page where it calls the function (I've left off some parameters in the function call above to save space):

<?php echo PCStandardTextBlock(
            array(
                'small_heading' => 'Small Heading',
                'large_heading' => 'Large Heading',
                'text' => 'This is example text that is just basic text. If this was advanced text, then it would be multiple paragraphs.',
                'text_mode' => 'basic',
                'big_first_p' => true,
                'photo' => '', 
                'image_alt_text' => '',
                'icon' => '', 
                'color_scheme' => 'white',
                'special_feature' => '', 
                'container_id' => '',
                'container_class' => '',
            )
        ); ?>
13
  • Which parameter are you checking? You are checking two in the code above. Commented Jun 30, 2017 at 0:13
  • 1
    What is the issue with your current code? Questions about code should generally include the desired/expected behavior, the actual behavior, and how the two are different if you want to get helpful answers. Commented Jun 30, 2017 at 0:13
  • Your settings array is called settingsDefault, in your if-else statements, you are using something else as settings. Did you check that first? Commented Jun 30, 2017 at 0:14
  • I updated the code, sorry. I only pulled part of the whole function for this question. Commented Jun 30, 2017 at 0:18
  • @SalB What do you want your code to look like when it is functioning correctly? Besides the settingsDefault and settings I don't see anything wrong. Commented Jun 30, 2017 at 0:19

1 Answer 1

1

It looks like you were checking a non-existent array key: $settings['big_first_p']. Here it is corrected:

$settingsDefault = array(
  'big_first_p' => false,
);

if ( $settingsDefault['text_mode'] == 'basic' ) {
  $output .= "\n\t\t\t\t<p>{$settings['text']}</p>"; // basic version
} elseif ( $settingsDefault['big_first_p'] === true ) {
  $output .= "\n\t\t\t\t<p class='big-p'>{$settings['text']}</p>";
} else {
  $output .= "\n\t\t\t\t{$settings['text']}"; 
}

Update:

So, you are passing in 'text_mode' => 'basic',, which is why the basic version is getting appended to $output in your if/elseif/else statement. Your code is functioning correctly.

If you want the big-p class one to be chosen, you need to place that block first in your if/elseif/else clause, or change the text_mode value you are passing into the function initially to something other than 'basic'.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the $settings variable set to prior to using it in your array_merge line?
Thanks @kjones didn't realize the order of those if/elsif/else would affect it
Welcome, glad you got it sorted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.