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 thediv
– Jorge.M
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?
– user126995
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– Jorge.M
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?– user126995
If you want I can make an example of what I think is best, but I will use
PDO
to illustrate the consultation.– Jorge.M
Opa, of course, can do in PDO even, I can then move to mine quietly. It will be of great help!
– user126995