Can you put a do while loop with alternative syntax?
I have to show data of some articles, but first I want to show the category of those articles
If I do the following
<?php while($art = $articulos->fetch_object()): ?>
<p><?=$art->nombre?></p>
<p><?=$art->descripcion?></p>
<?php endwhile; ?>
I will be displaying the name and description of the items. But before the while I want to show the category of the articles ($art->category). However, until I do the fetch_object() I will not have the category property available.
I thought of putting a do while loop
<?php $art = $articulos->fetch->object(); ?>
<p>Categoría: <?=$art->categoria?></p>
<?php do: ?>
<p><?=$art->nombre?></p>
<p><?=$art->descripcion?></p>
<?php while($art = $articulos->fetch->object()); ?>
But it gives me an error and I don't know where to put the endwhile
Is it possible to do an alternative syntax with do while?
And if not, how could I show the category first? Taking advantage of the fact that in the sql query I already have the name of the category, to avoid having to make two queries, one to show the category and another to show the other data
Thank you!
The structure of do...while in php is very simple
in your example
The block repeats as long as the condition in while() is true. At least it repeats once, so this loop can work for you.
From the php documentation : PHP offers an alternative syntax for some of its control structures, namely: if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
So the answer to your question would be no, there is no alternative syntax for do-while loops.
Another way to implement it can be: