How to create a function for my IF with Class Public Function?

0

Well, I have the following public function :

/**
* Mostrar notícias
*
* @param string $featured - Define se será noticia de destaque ou padrão que mostrará
*
* @param string $date - Define o tempo em que mostrará as notícias recentes (ex: notícias recentes dentro de 7 dias)
*
* @param string $limit - Define o total de notícias que será mostrada por vez dentro da função
*/
public function newsShow($featured = 'No', $date = 7, $limit) {
    $DB = new Database();
    $Translate = new Translate();
    $M = new Modulos();

    $stm = $DB->readDB('news', 'WHERE Featured = "'.$featured.'" AND Date BETWEEN CURRENT_DATE()-'.$date.' AND CURRENT_DATE() ORDER BY Date DESC LIMIT '.$limit);
    if (!$stm) {
        echo '';
    } else {
        foreach ($stm as $key) {
            // Envia as informações do banco de dados para as gets \
            $this->SetID($key["ID"]);
            $this->SetTitle($key["Title"]);
            $this->SetSubTitle($key["SubTitle"]);
            $this->SetCategory($key["Category"]);
            $this->SetMessage($key["Message"]);
            $this->SetFeatured($key["Featured"]);
            $this->SetDate($key["Date"]);

            // Define a tradução dos termos achados no banco de dados \
            $title = json_decode($this->GetTitle(), true);
            $subtitle = json_decode($this->GetSubTitle(), true);
            $title_name = $title[U_LANG].PHP_EOL;
            $subtitle_name = $subtitle[U_LANG].PHP_EOL;
            $url = $M->tirarAcentos($Translate->set('href')['news'].$this->GetID().'-'.$title_name, true);

            // Envia as informações do banco de dados para as gets \
            $this->SetURL($url);
            $this->SetTitle($title_name);
            $this->SetSubTitle($subtitle_name);
        }
    }
}

So that's fine, so I call it out of class:

<?php echo $News->newsShow('Yes', 7, '0,1'); ?>

But what I want is to do a check out of the class to see if it has a news to be shown, getting more or less like this:

<?php if (função) { echo $News->newsShow('Yes', 7, '0,1'); } else { echo null; } ?>

But that's where it is, I do not know how to do it, because I need to show a div within that IF , so look:

<?php if (funcao) { echo $News->newsShow('Yes', 7, '0,1');
            echo '<div class="ak-block-title">
                <a href="'.$News->GetURL().'" class="ak-title-link">
                    <div class="ak-layer">
                        <span class="ak-title"><?php '.$News->GetTitle().'</span>
                        <br><span class="ak-subtitle"><?php '.$News->GetSubTitle().'</span> <span class="ak-banner-more">+</span><br>
                    </div>
                </a>
            </div>'; } else { echo null; } ?>

Well this is what I want, but I do not know what to put in the IF function to do this, please help!

    
asked by anonymous 08.10.2018 / 23:25

1 answer

0

If you need to perform the same check multiple times, you can create a function that returns true if the number of rows found in the table is greater than 0, otherwise, nothing will be done.

You could create the function as follows:

  public function HasData() {
        $sql = $this->db->prepare('SELECT COUNT(id) as qt FROM suatabela');
        $sql->execute();
        $data = $sql->fetch(PDO::FETCH_ASSOC);
        if($data['qt'] != '0' ) {
            return true;
        }
        return false;    
  }

In your if you would do so:

<?php if (HasData()): echo $News->newsShow('Yes', 7, '0,1'); ?> 
   <div class="ak-block-title">
        <a href="<?=$News->GetURL()?>" class="ak-title-link">
           <div class="ak-layer">
               <span class="ak-title"><?=$News->GetTitle()?></span>
                 <br><span class="ak-subtitle"><?=$News->GetSubTitle()?></span> 
                  <span class="ak-banner-more">+</span><br>
             </div>
          </a>
     </div>
<?php endif;?>

Note: I did not post parts of conexão with the bank because it is not the case and I believe you have the knowledge to understand the operation of the code.

    
08.10.2018 / 23:55