Today I tried unsuccessfully to add an element (<h3>articles</h3>) in a webpage with mysql, only if there are articles of that author.
This code works, but I get only the articles, if there are, but I would add also the title <h3>articles</h3>only once (this is the point), at the top of the list of articles.
$queryart = "SELECT *
FROM `bibliografie`
WHERE tipologia not like '%libro%' and keywords like '%$tema%' ORDER by autore,data";
$resultart = mysqli_query($db, $queryart);
echo "<ul>";
while ($rowart = mysqli_fetch_array($resultart))
{
echo "<li>$rowart[autore_nome] <b>$rowart[autore]</b>,
“$rowart[titolo]”, <i>$rowart[rivista]</i>, $rowart[data]";
echo ".</li>";
}
echo "</ul>";
How can add <h3>articles</h3> at the top of a list of articles, only if there are articles?
I hope my question is clear… Otherwise tell me to explain better.
The problem is that if I put <h3>articles</h3>before the while (condition: see my code above) I get only one <h3>articles</h3> (as expected) but I get this title also even there aren’t articles.
If, instead, I put <h3>articles</h3>after the while I get that title only if there are articles (as expected), but in this case there are as many <h3>articles</h3> as the rows (= each row has the title “articles”).
How could I get both a) the title articles only if there are articles, and b) only once.
Thank you, but I’m afraid I don’t understand.
I try to simplify (and to translate), as possible, my code:
$query = "SELECT *
FROM `bibliography`
WHERE tipologia like '%article%' and keywords like '%$theme%' ORDER by author,date";
$result = mysqli_query($db, $query);
echo "<ul>";
while ($row = mysqli_fetch_array($result))
{
echo "<li>$row[author],
“$row[title]”, <i>$row[issue]</i>, $row[date]";
echo ".</li>";
}
echo "</ul>";
In each page with bibliography I have this variable:
<?php
$theme = "[the name of the author]";
include "my-path/the-file-with-the-common-code[as above]";
?>
Where should I put you new code?
EDIT
I wish a title like “articles” to separate them from “books”, listed above them (when there are articles about an author: always there are books, sometimes also articles).
Gather the query results into an array rather than echo them.
$query = "SELECT *
FROM `bibliography`
WHERE tipologia like '%article%' and keywords like '%$theme%' ORDER by author,date";
$result = mysqli_query($db, $query);
$articleList=[];
while ($row = mysqli_fetch_array($result))
{
$articleList[]=$row;
}
Then, you can test if there are elements in the array and output the appropriate HTML.
if ($articleList){
echo "<h3>Articles</h3><ul>";
foreach ($articleList as $row){
echo "<li>$row[author], “$row[title]”, <i>$row[issue]</i>, $row[date]";
echo ".</li>";
}
echo "</ul>";
}
If you particularly need those four scalar variables ($href, $link, $issue, $opera) then you’ll still need to do the loop, but I’m not sure why you’d want to be setting those as they’ll write over each other on each iteration of the loop. If you really need them to contain the last values in the array, you could do that after you’ve retrieved the array by using count or some other way of accessing the last array elements.
I’m not that familiar with mysqli so you should check that what’s in $articleList is the same as it is when you use the loop.
$query2 = "SELECT *
FROM `bibliografie`
WHERE tipologia not like '%libro%' and keywords like '%$tema%' $altritemi and keywords not like '%noweb%' $altracondizione ORDER by autore,data";
$result2 = mysqli_query($db, $query2);
$articleList = mysqli_fetch_all($result2, MYSQLI_ASSOC);
echo "<h3>Articoli o contributi</h3><ul>";
echo "<li>$rowart[autore_nome] <b>$rowart[autore]</b>, “$rowart[titolo]”, in ";
echo " <i>$rowart[opera]</i>";
echo " <i>$rowart[rivista]</i>";
echo ", $rowart[luogo] $rowart[data]";
if($href != '') {echo " (<a href=\"$link\">vedi</a>)";}
echo ".</li>";
echo "</ul>";
You should not be setting your four variables ($href, $link, $rivista, and $opera) in your while loop. Think about it for a minute. Those variables can only hold one value, and you replace that value every loop iteration. At the end of the while loop, they will contain values relevant to only the last row that was fetched. When you try to use them later in the foreach loop then, they will not have the values for the current row like they did in your original code, they will have the values of the last row.
You moved all the other code over to the foreach loop correctly, so why did you not move the assignment of those variables over to the foreach as well? That is where they need to be for them to have the proper value of each row.