The value being passed to while
is not a number n
as you are thinking, so it is not possible to do the +1
that you wanted.
while($row = mysqli_fetch_array($result))
The variable $row
of the expression is receiving a array of function mysqli_fetch_array($result)
, and since that array is not empty, PHP will interpret as true TRUE
and when the data ends the expression will return NULL
which will be interpreted as FALSE
and then the while will be interrupted.
Note the following code:
while(TRUE) {
// Trecho de código
}
The above code represents a loop (loop) infinite, or "never" will be stopped unless a break
within the loop.
But there is a way to do it the way you wanted, just use a counter instead of the expression $row = ...
, example:
$nrows = mysqli_num_rows($result);
while($nrows){
$nrows--;
$row = mysqli_fetch_array($result);
// Restante do código aqui
}
But the problem is that when you finish counting, where you have $row['img']
an index error will be launched, because the variable $row
will have the value NULL
, since you finished the data. To get around this you would have to add a few if’s and/or ternary operations, things totally unnecessary if put the standard image after the loop as in the example:
<?php
$con = mysqli_connect("localhost","root","","gibellino");
mysqli_set_charset($con,"utf-8");
$result = mysqli_query($con,"select * from index_img");
while($row = mysqli_fetch_array($result)){
$img = $row['img'];
echo "<td><img src='../imagem/bd/index/$img' width='170px' height='300px'><a href='#'><img src='../imagem/fancy_closebox.png' id='fechar'></a></td>";
}
// Aqui fora do loop vai a imagem padrão que será sempre adicionada após todas as demais.
echo "<td><img src='../imagem/bd/index/aqui-vai-a-imagem-padrao.jpg' width='170px' height='300px'><a href='#'><img src='../imagem/fancy_closebox.png' id='fechar'></a></td>";
}
?>
Explain better what you want to do. What is the expected result.
– Daniel Omine
@Danielomine, the number "n" that while has comes from the database, right? I wanted the number that while had was "n+1". for example I have 5 data in the database and I wanted while to fetch this 5 data and add a value like this was 6
– Bruno Gibellino
this is obvious.. what I ask is, why do you want 1 more ? is to be able to generate 1 more
<td><img src='...
? (off topic: please try to write Portuguese correctly)– Daniel Omine
yes it is to be able to generate more 1 <img> but this image does not come from the same table.
– Bruno Gibellino
then put this additional image out of the repeat loop.
– Daniel Omine
The problem is that I have a table with images (I think this I could see) but I don’t know how many images I have there because I can add how to remove images from the table. And I wanted to add, when the number of images in the database came to an end, a different image that is not in the database
– Bruno Gibellino
Honestly I still don’t understand the goal. Edit your question and clarify better.
– user28595