Echo something only if a column has same value

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.

this is really a php question

isn’t there a variable that tells you how many rows the query returned?

1 Like

done

1 Like

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.

$titleAdded = false
while(…)
{
    If(!$titleAdded)
    {
        $titleAdded = true;
        Echo $title;
…
1 Like

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>";
}
1 Like

Or just use num_rows to see how many rows the query returned, and only output the heading if it’s more than zero.

1 Like

Perfect! It works exactly as expected! :smiley: :100:
Thank you very, very much!

ops… no, there is still a small problem:

I had simplified my code, but the real, modified, whole code is:

<?php
$queryart = "SELECT * 
FROM `bibliografie`
WHERE tipologia not like '%libro%' and keywords like '%$tema%' $altritemi and keywords not like '%noweb%' $altracondizione ORDER by autore,data";
$resultart = mysqli_query($db, $queryart);
$articleList=[];

while ($rowart = mysqli_fetch_array($resultart))
{
  $articleList[]=$rowart;
  $href = "$rowart[href]";
  $link = "$intellectualia/$href";
  $issue = "$rowart[rivista]";
  $opera = "$rowart[opera]";
}

if ($articleList){
   echo "<h3>Articoli o contributi</h3><ul>";
   foreach ($articleList as $rowart){
    echo "<li>$rowart[autore_nome] <b>$rowart[autore]</b>, “$rowart[titolo]”, in ";
    if($opera != '') {echo " <i>$rowart[opera]</i>";}
    if($issue != '') {echo " <i>$rowart[rivista]</i>";}
    echo ", $rowart[luogo] $rowart[data]";
    if($href != '') {echo " (<a href=\"$link\">vedi</a>)";}
    echo ".</li>";
   }
}
   echo "</ul>";
?>

For same reason the output cut some column: “rivista” and “luogo” and “href”, as you can see in the screenshot

The previous whole code, that is this:


$queryart = "SELECT * 
FROM `bibliografie`
WHERE tipologia not like '%libro%' and keywords like '%$tema%' $altritemi and keywords not like '%noweb%' $altracondizione ORDER by autore,data";
$resultart = mysqli_query($db, $queryart);
echo "<ul id=\"articoli\">";
while ($rowart = mysqli_fetch_array($resultart))
{
  $href = "$rowart[href]";
  $link = "$intellectualia/$href";
  $rivista = "$rowart[rivista]";
  $opera = "$rowart[opera]";

  echo "<li>$rowart[autore_nome] <b>$rowart[autore]</b>, 
  “$rowart[titolo]”, in ";
  if($rivista != '') {echo " <i>$rowart[rivista]</i>";}
  if($opera != '') {echo " <i>$rowart[opera]</i>";}
  echo ", $rowart[luogo] $rowart[data]";
  if($href != '') {echo " (<a href=\"$link\">vedi</a>)";}
  echo ".</li>";
}
echo "</ul>";

don’t gave the desised title (articoli o contributi), but gave all the columns, as expected. You can see it in the second screenshot:

Where I’m wrong?

aha! that’s what i was thinking of, but i don’t do php so i didn’t know what it was called

Indeed I can simplify the code, without any further if (there is either a “rivista” or an “opera”, never both):

echo "<li>$rowart[autore_nome] <b>$rowart[autore]</b>, “$rowart[titolo]”, in ";
    echo " <i>$rowart[opera]</i>";
    echo " <i>$rowart[rivista]</i>";

and so the code works as expected.

If you just want all rows in an array, why the need for the loop?

$resultart = mysqli_query($db, $queryart);
$articleList = mysqli_fetch_all($resultart, MYSQLI_ASSOC);

1 Like

Uhm… I am not expert: could you tell me, please, how should be my whole code?
Thank you!

Instead of this:

$articleList=[];

while ($rowart = mysqli_fetch_array($resultart))
{
  $articleList[]=$rowart;
  $href = "$rowart[href]";
  $link = "$intellectualia/$href";
  $issue = "$rowart[rivista]";
  $opera = "$rowart[opera]";
}

just write

$articleList = mysqli_fetch_all($resultart, MYSQLI_ASSOC);

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.

1 Like

Thank you, but your new code:

$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>";

I get only one row.

The only last problem I have in the code at the #13 message is that the href doesn’t work

if($href != '') {echo " (<a href=\"$link\">vedi</a>)";}

I guess as if nested within another if.

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.

1 Like

Do you mean this?

$resultart = mysqli_query($db, $queryart);
$articleList=[];

  $href = "$rowart[href]";
  $link = "$intellectualia/$href";
  $issue = "$rowart[rivista]";
  $opera = "$rowart[opera]";  

while ($rowart = mysqli_fetch_array($resultart))
{
  $articleList[]=$rowart;
}

if ($articleList){
   echo "<h3>Articoli o contributi</h3><ul>";
   foreach ($articleList as $rowart){
    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>";
   }
}

This code works, but I get exactly the same result: I lack of “href” column.

No. Where you have them now, $rowart does not even exist, why would they go there?

1 Like