dimanche 3 janvier 2016

Several if conditions, check if no one is true

I'm not sure how to put together this code with a few if/else statements.

I check a field if it is not empty - in that case I also check if the value match a certain text.

This procedure is repeated two more times with two more fields.

The last thing is to add a new field if there where no matches in the if conditions. I need to rewrite this somehow, I didn't figure it out. I thought I could set a variable to "true" if there was a match. But I'm breaking some scope rules I guess. So the idea with my "$alreadyexists" variable is totally wrong.

How should I carry out this thing?

$textcontent = "Unique text in a unique field";

$subfieldvalue1 = get_post_meta($page_id,$subfield1 );
$subfieldvalue2 = get_post_meta($page_id,$subfield2 );
$subfieldvalue8 = get_post_meta($page_id,$subfield8 );

$alreadyexists = false;

if(!empty($subfieldvalue1)){
    if($subfieldvalue1[0] === $textcontent){
            $alreadyexists = true;
    }
}
if(!empty($subfieldvalue2)){
    if($subfieldvalue2[0] === $textcontent){
        $alreadyexists = true;
    }
}
if(!empty($subfieldvalue8)){
    if($subfieldvalue8[0] === $textcontent){
        $alreadyexists = true;
    }
}
if($alreadyexists = false){
    //add new field with text content!
}

Aucun commentaire:

Enregistrer un commentaire