开发者

Combine three "complex" PHP conditions in one perfect php snippet

开发者 https://www.devze.com 2023-03-20 18:57 出处:网络
I\'m stuck in Drupal Panels / PHP Access plugins. At least, now I found the three conditions to create my final snippet. the purpose of it is to return TRUE; if \"condition1 is TRUE\" OR \"condition2

I'm stuck in Drupal Panels / PHP Access plugins.

At least, now I found the three conditions to create my final snippet. the purpose of it is to return TRUE; if "condition1 is TRUE" OR "condition2 is TRUE" OR "condition3 is TRUE". I found a lot of similar questions, but the last condition force me to post here to find the right way to do this.

Condition 1:

// At least  $view1->result has result.
$view1 = views_get_view('sp_onglet_videos');
$v开发者_如何学Goiew1->set_display('views-tab-embed_1');
$output1 = $view1->preview();
if ($view1->result) {
  return TRUE;
}

Condition 2 (same thing):

// At least  $view2->result has result.
$view2 = views_get_view('sp_onglet_audio');
$view2->set_display('views-tab-default');
$output2 = $view2->preview();
if ($view2->result) {
  return TRUE;
}

Condition 3 is more complex:

// Checks for content in the field field_txt_videos.
if (isset($contexts['argument_nid_1']->data-> field_txt_videos)) {
  $field = $contexts['argument_nid_1']->data-> field_txt_videos;
  if (is_null($field)) {
    return FALSE;
  }
  if (is_array($field)) {
    foreach ($field as $key => $val) {
      if (is_array($val)) {
        $field[$key] = array_filter($val);
      }
    }
    $field = array_filter($field);
    return count($field);
  }
  if (is_string($field) && trim($field) == '') {
    return FALSE;
  }
  if ($field) {
    return TRUE;
  }
  return FALSE;
}

I would like to have something clean (and functional) like this:

if ($view1->result && $view2->result && $field) {
return TRUE;
      }

But it's to tricky for my php knowledge. Need a little help !


You want to save the result of the 3rd condition (into a variable) and use this result to run your final condition/query. But you can query the 3rd condition if it is a function.


It is better to properly space your code and use plenty of newlines.

However, PHP does have some pretty cool tricks to do assignment inside conditional statements.

if(($view1 = views_get_view('sp_onglet_videos')) AND $view1->set_display('views-tab-embed_1') AND ($output1 = $view1->preview()) AND $view1->result) return TRUE;

However, as you can see this code is a mess - don't do it unless your assignment is really small. Take this simple security check at the top of a PHP file:

<?php defined('BASE_PATH') OR die('Not Allowed');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号