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

Asked

Viewed 133 times

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 far so good, and I also call her out of class:

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

But what I want is to do a check outside 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 don’t know how to do it, because I need to show a div inside that IF, will look like this:

<?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 that’s what I wish, but I don’t know what to put in the function of IF to make this journey, please help me!

  • You can make a select COUNT to check if the number of news is greater than 0, if it is, you display the div

  • Well, good idea, but then what, how am I gonna do this COUNT? Why, I want to use this same function in several areas of my site, so I did it in class, so I don’t have to keep creating the same query call every time I need to use, but I have a project that I called?

  • Why doesn’t create a function that returns the number of records from the table? In the check you would do so: if(retornoLinhas() > 0) then you put your code

  • Oh yes, I get it, you say kind of create a get inside the function itself, getting: if($News->GetResult() > 0) { echo código }? Then within the function I would create something like: $this->SetResult(), but there you are, thinking this way, I will still be doubtful when creating Setresult, unless in it I do the same thing from the beginning, getting like this: $this->SetResult(!$stm); will it work?

  • If you want I can make an example of what I think is best, but I will use PDO to illustrate the consultation.

  • Opa, of course, can do in PDO even, I can then move to mine quietly. It will be of great help!

Show 1 more comment

1 answer

0


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

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 his 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 didn’t post parts of conexão with the bank because it does not come to the case and I believe that you have knowledge to understand the functioning of the code.

  • You don’t need the conexão That I know how to do, well, I looked at his method, is he’s what I need, thank you :D

  • @Akatsuki how nice to be able to help you! D

  • Well it looks like this now rs: public Function hasData() { $DB = new Database(); $stm = $DB->readDB('news', null, 'COUNT(ID) as QT'); if (!$stm) { Return false; } Else { foreach ($stm as $key) { if ($key['QT'] != 0) { Return true; } Else { Return false; } } } }

  • Ball show! It worked?

  • Well, I made some adjustments :D, now it’s much better r

  • look at the new script: https://pastebin.com/v2fNkq26

  • in the check itself to see if it has database record, already put soon the definition of prominence, between dates and the limit, thus in the own if already define and only shows the news below rs :D. Thanks for everything!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.