Support

Account

Home Forums Front-end Issues Conditional statement problem (expects parameter)

Solved

Conditional statement problem (expects parameter)

    • Cheyens

    • August 12, 2013 at 7:18 pm

    Hi,

    I am using Conditional statement (Checkbox rvalue is an array) function to get two outputs. But if “None” checkbox is ticket I get this error:

    “Warning: in_array() expects parameter 2 to be array, string given in…”

    How can I do an “if empty” check or otherwise write it properly?

    This is my code:

    <?php if( in_array( 'Materialet er tilgængeligt til download', get_field('materiale') ) ) {echo '<a href="' . get_field('download') . '" target="_blank" class="readmore">Download PDF →</a>';}?>
    
    <?php if( in_array( 'Materialet er tilgængeligt som eksternt link', get_field('materiale') ) ) {echo '<a href="' . get_field('link') . '" target="_blank" class="readmore" >Eksternt link →</a>';}?>

    Thanks

    • Elliot

    • August 13, 2013 at 12:56 pm

    Hi @cheyens
    I would first test the value of your get_field to see why the error is showing in the first place.

    
    <?php 
    
    echo '<pre>';
    	var_dump(get_field('materiale'));
    echo '</pre>';
    die;
     ?>
    

    What do you get?

    • Cheyens

    • August 13, 2013 at 6:40 pm

    Hi,

    I get this:

    string(0) ""

    • Elliot

    • August 14, 2013 at 12:06 pm

    Looks like there is no value saved for the field.

    This means your in_array function will fail due to the wrong variable type.

    Perhaps you need to also compare the value like so:

    
    <?php 
    
    if( get_field('materiale') )
    {
    
    	if( in_array( 'Materialet er tilgængeligt til download', get_field('materiale') ) )
    	{
    		echo '<a href="' . get_field('download') . '" target="_blank" class="readmore">Download PDF →</a>';
    	}
    
    }
    
    ?>
    
    • Cheyens

    • August 14, 2013 at 7:35 pm

    Hi,

    Thanks! That solved my problem 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.