← Desenvolvimento

Dúvida em PHP

Lida 1270 vezes

Offline

hrpttr7 
Membro
Mensagens 3 Gostos 0
Troféus totais: 20
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 First Post Karma

Código: [Seleccione]
<html>
<body>
<table>
<tr>
<?php
// define directory path
$dir ".";

// look for JPEGs
if &#40;is_dir&#40;$dir&#41;&#41; &#123;
    
if &#40;$dh = opendir&#40;$dir&#41;&#41; &#123;
        
while &#40;&#40;$file = readdir&#40;$dh&#41;&#41; !== false&#41; &#123;
            
if &#40;preg_match&#40;"/.jpg/", $file&#41;&#41; &#123; 

            
echo "<td> $file etc etc </td>"

           
&#125;
        
&#125;
       
closedir&#40;$dh&#41;;
   
&#125;
&#125;
?>

</tr>
</table>
</body>
</html>


Boas.

Com este código, o PHP vai escrever X vezes <td> "nome do ficheiro" etc etc </td>, em que X é o número de ficheiros no directório.

A minha dúvida é esta: como é que posso fazer com que entre cada 3 <td> "nome do ficheiro" etc etc </td> ele imprima </tr><tr>?

Espero que me tenha feito entender. Desde já obrigado! :wink:
Offline

Pure_Water 
Membro
Mensagens 515 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 500 Posts

eu n percebo mt de php, mas pensando em outras linguagens de programçao, n podes criar um contador e meter um if para que a cada 3 ele mete o <tr>?
Offline

pedrolopes 
Membro
Mensagens 91 Gostos 0
Troféus totais: 23
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 50 Posts 10 Posts

antes do echo metes:
Código: [Seleccione]
contador++;
depois do echo metes:
Código: [Seleccione]
if contador == 3 {
echo"<tr></tr>";
contador = 0;
}

deve funcionar, basta ajustares a sintaxe!
Offline

hrpttr7 
Membro
Mensagens 3 Gostos 0
Troféus totais: 20
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 First Post Karma

Código: [Seleccione]
<html>
<body>
<table>
<tr>
<?php
// define directory path
$dir ".";

// look for JPEGs
if &#40;is_dir&#40;$dir&#41;&#41; &#123;
    
if &#40;$dh = opendir&#40;$dir&#41;&#41; &#123;
        
while &#40;&#40;$file = readdir&#40;$dh&#41;&#41; !== false&#41; &#123;
            
if &#40;preg_match&#40;"/.jpg/", $file&#41;&#41; &#123; 

            
$contador++;

            echo 
"<td> $file etc etc </td>"

            
if &#40;$contador == 3&#41; &#123;
            
echo"</tr><tr>";
            
$contador 0;
            &
#125;

           
&#125;
        
&#125;
       
closedir&#40;$dh&#41;;
   
&#125;
&#125;
?>

</tr>
</table>
</body>
</html>


Pus assim e já deu. Muito obrigado pela ajuda! :wink: