I have added an ordering of news by its categories, where each new news that is in the table one_more_news
is added id
to the tablecategory
Which displays all the titles of the new news with their respective category without problem.
PREMIER LEAGUE
Lorem ipsum dolor sit amet, consectetur adipiscing elit 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit 2
CUP AMERICA
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Lorem ipsum dolor sit amet, consectetur adipiscing elit
Those results are executed by the following query:
$stmtcategory = $con->prepare("SELECT id_category,title_category FROM category WHERE active=?");
$stmtcategory->bind_param("i",$active);
$active = "1";
$stmtcategory->execute();
$stmtcategory->store_result();
if ($stmtcategory->num_rows>0) {
$stmtcategory->bind_result($id_category, $title_category);
while ($stmtcategory->fetch()) {
echo '<div class="date-text">
<h4>'.$title_category.'</h4>
<ul>';
$stmt = $con->prepare("SELECT id_one_more_news,cover_page,title,description,detail,url,date_post FROM one_more_news WHERE id_category=? order by id_one_more_news ASC LIMIT 2");
$stmt->bind_param("i",$id_category);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) {
$stmt->bind_result($id_one_more_news, $cover_page, $title, $description, $detail, $url, $date_post);
while ($stmt->fetch()) {
echo '<li>'.$title.'</li>';
}
echo '</ul>';
echo '</div>';
} else {
//echo "<span>No existe noticias nuevas</span>";
echo '</ul>';
echo '</div>';
}
}
} else {
echo "No existe noticias nuevas";
}
My little problem in the query code is that when there is no content for a category, the title of that category is displayed, resulting in the following:
PREMIER LEAGUE
Lorem ipsum dolor sit amet, consectetur adipiscing elit 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit 2
CUP AMERICA
Can you explain to me, how can I make the title of the category not appear, when there is no news linked to the same category?
Additional Information
Tablecategory
id_category title_category active
1 PREMIER LEAGUE 1
2 CUP AMERICA 1
Tableone_more_news
id_one_more_news ..... title ......... id_category active
1 .... 1 1
2 ... 1 1
Eduardo promised is debt.
Here I leave you a code proposal based on what has been said in comments. The executed query takes advantage of the concept of relational databases, obtaining data from several tables through indexes that serve to relate the data contained in them. That will save us from having to send two queries to the database. And it will avoid the original problem you have of repeated data.
As before, the code puts each piece of information inside its respective
<li>
.I have implemented a control for when the data comes empty.
Although I do not see that you use all the columns ofSELECT
the I have put them all (so you can see more widely how it worksGROUP_CONCAT
). If you are not going to use them, remove them and that's it. In the end I have removed the columns that were not used for simplicity reasons, because I wanted to create a proof of concept inphpfiddle
. If there were more columns, they would come out the same astitle
, with each value separated by|
.Here is the updated code, with the controls also on the connection and on the preparation of the statement. If it fails tell me and if you don't understand something, ask in comments. I hope it helps. I have tested it in the new fiddle (updated link at the end) and it works perfectly for me.
proof of concept
Here you can see a test based on real data: VER DEMO EN PHPFIDDLE(click
Run - F9
to see the result).The query now brings the data like this:
And the reading we do inside
foreach
allows us to obtain a result like this: