D
D
Dmitry Filandor2018-07-24 10:04:28
PHP
Dmitry Filandor, 2018-07-24 10:04:28

How to assign a value to a variable in a loop?

Hello!
There is a need to separate the tape by dates! I have a loop for outputting tape entries in my template:

<?php

global $DateControl; /*ПРОБУЮ   ГЛОБАЛЬНУЮ ПЕРЕМЕННУЮ*/



class td_module_8 extends td_module {
    function render() {
        ob_start();
        $title_length = $this->get_shortcode_att('m8_tl');
    
    date_default_timezone_set('Europe/Kiev');
    $DateNow =   date("d.m.Y");
    //$DateControl = "1"; ЕСЛИ ТУТ ДЕЛАТЬ ПРИСВОЕНИЕ - ПРИСВАИВАЕТ
    $DateNews = $this->get_date();	
        ?>

        <div class="<?php echo $this->get_module_classes();?>">
            <div class="item-details">
      <?php //dsy даты
      if($this->get_date() != $DateControl ){  //ЭТО РАБОЧАЯ ЛОГИКА, ЕСЛИ ДАТА ТЕКУЩЕГО ПОСТА ОТЛИЧАЕТСЯ ОТ КОНТРОЛЬНОЙ ДАТЫ (DateControl - ПРИСВАИВАЕМ В КОНЦЕ ЦИКЛА, И ВОТ ПРИСВОЕНИЕ НИЖЕ, ОНО НЕ РАБОТАЕТ), ТО СТАВИТ ДАТУ НОВОГО ДНЯ
          echo   $this->get_date(); 			   
      }
      ?>

      <?php //DSY ВАЖНОСТЬ			 
      $i = get_field( "important-news", $this->post->ID ); 
      //$icon = '<img src= ' + get_field( "img-icon", $this->post->ID ) + '>';
      $icon =   get_field( "img-icon", $this->post->ID )  ;

      switch ($i) {
        case null:
           echo $this->get_title($title_length, $icon);
          break;				 

        case "important":
           echo $this->get_titleImportant($title_length, $icon); 
          break;

        case "importanthard":
            echo $this->get_title_very_Important($title_length, $icon); 
           break;
      }
      ?> 

         <?php /* echo $this->get_image('td_324x235'); */ ?>

                <div class="td-module-meta-info">
                    <?php if (td_util::get_option('tds_category_module_8') == 'yes') { echo $this->get_category(); }?>
                    <?php echo $this->get_author();?>
                    <?php echo $this->get_date();?>  
                    <?php /*echo get_the_time('', $this->post->ID ); */?>				
                    <?php echo $this->get_comments();?>					 
                </div>
            </div>

            <?php echo $this->get_quotes_on_blocks();?>
      <?php  
      $DateControl = $this->get_date(); // ВОТ ТУТ ЗАГВОЗДКА, ПРИСВОЕНИЕ НИГДЕ НЕ РАБОТАЕТ, ТОЛЬКО В ВЕРХНЕЙ ЧАСТИ КОДА, А МНЕ ПРИСВАИВАТЬ ДАТУ НАДО В КОНЦЕ КАЖДОЙ ИТЕРАЦИИ ЦИКЛА
      ?>
        </div>
     
        <?php return ob_get_clean();
    }
}

TOTALLY it should turn out :
_
_
_ 00 news vava v vyvvyv 18/07/2018 12:00 vava news at 12:40 vav news vavu 18:00 vava v vvyvyv news UPD 1: I seem to understand. $DateControl always has a primary value, which is set when it is declared, or null if it is not set, because it is re-created every time the record is displayed (every cycle). The value that I assign at the end is simply not saved. UPD 2: In order for the variable to retain its value, added static.
But now for some reason the conditional operator does not work:
$DateNews = $this->get_date();
static $DateControl ; 

 if( $DateControl !=  $DateNews ){
          echo   'DateControl !=  DateNews </br></br>';
          echo   $DateControl; 				   
           
      }
      
          echo   'Обе переменные вне</br>';  
          echo   $DateControl;  
          echo   $this->get_date(); 

           //echo   $DateNews;

Added output of variables to see their content, picture: DateControl ! = DateNews -
v53qqPk.png
immediately the dates are equal!, and the block of the conditional operator is triggered () took not just a date, but:
<time class=»entry-date updated td-module-date» datetime=»2018-07-23T23:22:53+03:00″>23.07.2018</time>

visually in the browser I see a purely date, but in the source it is completely different. Now everything compares and everything is beautiful - 37SnHRV.png
Thank you all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-07-24
@LifeAct

The global keyword must be inside the function - it's not for declaring variables as global, but for importing them into the current scope.
But since you are using a class, it makes much more sense to use a static property of that class instead of a global variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question