Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to rewrite this repeated if statement?

I'm thinking there is an easier way to write this code, but not sure on what approach to take. Basically, I want to check if each variable exists, and if so - add the appropriate markup to the page.

Any suggestions would be great.

<?php
    $words = get_field('words');
    $photography = get_field('photography');
    $architect = get_field('architect');
?>

<div class="panel">
    <?php if( $words ): ?>
        <div>
            <p><span>Words</span><span><?php echo $words;?></span></p>
        </div>
    <?php endif ;?>

    <?php if( $photography ): ?>
        <div>
            <p><span>Photography</span><span><?php echo $photography;?></span></p>
        </div>
    <?php endif; ?>

    <?php if( $architect ): ?>
        <div>
            <p><span>Architect</span><span><?php echo $architect;?></span></p>
       </div>
    <?php endif; ?>
</div>
like image 550
deleteddeleted Avatar asked Jun 16 '15 06:06

deleteddeleted


People also ask

How many if else statement is too many?

You can nest up to 7 IF functions to create a complex IF THEN ELSE statement. TIP: If you have Excel 2016, try the new IFS function instead of nesting multiple IF functions.

What is the other statement that can avoid multiple nested IF condition?

Alternatives to nested IF in Excel To test multiple conditions and return different values based on the results of those tests, you can use the CHOOSE function instead of nested IFs.


4 Answers

You can use array & loop -

<?php
    $fields = array();
    $fields['words'] = get_field('words');
    $fields['photography'] = get_field('photography');
    $fields['architect'] = get_field('architect');
?>

<div class="panel">
    <?php foreach($fields as $key => $value): 
         if($value)
    ?>
        <div>
            <p><span><?php echo ucwords($key);?></span><span><?php echo $value;?></span></p>
        </div>
    <?php 
         endif;
    endforeach;?>
</div>
like image 131
Sougata Bose Avatar answered Nov 06 '22 03:11

Sougata Bose


Create a field array with their appropriate label if you want to have your own labels.

<div class="panel">
    <?php
    $fields = array (
            'words' => 'Words',
            'photography' => 'Photography',
            'architect' => 'Architect' 
    );
    foreach ( $fields as $field => $label ) {
        $value = get_field ( $field );
        if (!empty($value)) {
            echo '<div>
                <p><span>' . $label . '</span><span>' . $value . '</span></p>
            </div>';
        }
    }
    ?>
</div>
like image 30
Manish Jangir Avatar answered Nov 06 '22 04:11

Manish Jangir


<?php
   $required_fields = array("words","photography","architect");
   $data = array();
   foreach($required_fields as $field)
   {
        $data[$field] = get_field($field);
   }
?>


<div class="panel">
    <?php foreach($data as $data_field=>$data_value): 
         if($data_value)
    ?>
        <div>
            <p><span><?=$data_field?></span><span><?=$data_value?></span></p>
        </div>
    <?php 
         endif;
    endforeach ;?>
</div>
like image 32
abhinsit Avatar answered Nov 06 '22 04:11

abhinsit


You can create a function like this and then iterate the data that it returns to output your content

<?php
    //define a function to fetch data in specific fields
    function fetch_data($keys=array()){
        $data = array();
        foreach($keys as $key){
            $val = get_field($key);
            if(!empty($val)){
                $data[$key] = $val;
            }
        }
        return $data;
    }

    //now you can easily add or change your fields
    $data = fetch_data(array('words','photography','architect'));
?>

<div class="panel">

<?php 
    foreach($data as $field){
        if(isset($data[$field])){
            $val = $data[$field];
            ?>
            <div>
                <p><span><?php echo ucfirst($field); ?></span><span><?php echo $val; ?></span></p>
            </div>
            <?php 
        }
    }
?>
like image 1
Eason.Luo Avatar answered Nov 06 '22 02:11

Eason.Luo