2014-07-25 4 views
-1

Comment imprimer la valeur d'une fonction dans une table? J'ai du mal à comprendre ça, je l'ai cherché dans Google, j'ai regardé des tutoriels Youtube sur Youtube et je n'ai toujours rien trouvé, et il y a aussi une erreur qui me dit que $ cols, $ rows, $ rNumber est indéfini . La sortie devrait ressembler à une demi-pyramide.Imprimer Valeurs de fonction dans un tableau?

<?php 
do { 
    $rNumber = mt_rand(3, 11); 
} while ($rNumber % 2 == 0); 

echo '<strong>Looping Statements Exercises</strong>'; 
echo '<br>'; 
echo '<br>'; 
echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.'; 
echo '<br>'; 
echo '<br>'; 


echo '<table border = "1">'; 
echo '<tr>'; 
echo '<td colspan = "4"><center>Size:<strong>', $rNumber, ' x ', $rNumber, '</strong>'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure A'; 
echo '<td> Figure B'; 
echo '<td> Figure C'; 
echo '<td> Figure D'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure A here'; 
echo '<td>', figure_b($cols, $rows), '</td>'; 
echo '<td> Figure C here'; 
echo '<td> Figure D here'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure E'; 
echo '<td> Figure F'; 
echo '<td> Figure G'; 
echo '<td> Figure H'; 
echo '</tr>'; 
echo '<tr>'; 
echo '<td> Figure E here'; 
echo '<td> Figure F here'; 
echo '<td> Figure G here'; 
echo '<td> Figure H here'; 
echo '</tr>'; 


function figure_b($rows, $cols) 
{ 
    if ($rNumber == 5) { 
     for ($rows = 0; $rows <= 5; $rows++) { 
      for ($cols = 0; $cols <= 5; $cols++) { 

       return $cols; 
      } 
      return $rows; 
     } 
    } 
} 

echo '</table>';     
+0

figure_b (Col. $, $ lignes) où est que les variables? –

+0

Il ne cause pas l'erreur, mais peut-être que vous devriez fermer tags ... – lpg

Répondre

-1
<?php 

do{ 
             $rNumber = mt_rand(3,11); 
             }while ($rNumber % 2 == 0); 

             echo '<strong>Looping Statements Exercises</strong>'; 
             echo '<br>'; 
             echo '<br>'; 
             echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.'; 
             echo '<br>'; 
             echo '<br>'; 


             echo '<table border = "1">'; 
             echo '<tr>'; 
             echo '<td colspan = "4"><center>Size:<strong>',$rNumber,' x ',$rNumber,'</strong>'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure A'; 
             echo '<td> Figure B'; 
             echo '<td> Figure C'; 
             echo '<td> Figure D'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure A here'; 
             echo '<td>', figure_b(5, 5,$rNumber),'</td>'; 
             echo '<td> Figure C here'; 
             echo '<td> Figure D here'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure E'; 
             echo '<td> Figure F'; 
             echo '<td> Figure G'; 
             echo '<td> Figure H'; 
             echo '</tr>'; 
             echo '<tr>'; 
             echo '<td> Figure E here'; 
             echo '<td> Figure F here'; 
             echo '<td> Figure G here'; 
             echo '<td> Figure H here'; 
             echo '</tr>'; 


             function figure_b($rows, $cols,$rNumber) 
             { 
              if($rNumber == 5) 
              { 
               for($rows = 0; $rows<=5; $rows++) 
               { 
                for ($cols = 0; $cols<=5; $cols++) 
                { 

                return $cols; 
                } 
               return $rows; 
               } 
              } 
             } 
             echo '</table>'; 

?> 
0

Votre fonction:

function figure_b($rows, $cols) 
{ 
if($rNumber == 5) 
{ 
for($rows = 0; $rows<=5; $rows++) 
{ 
for ($cols = 0; $cols<=5; $cols++) 
{ 

return $cols; 
} 
return $rows; 
} 
} 
} 

est complètement faux.

Il devrait être plutôt:

function figure_b($rNumber) 
{ 
    if ($rNumber == 5) { 
     for ($rows = 0; $rows <= 5; $rows++) { 
      for ($cols = 0; $cols <= 5; $cols++) { 
       echo $cols.' '; 
      } 
      echo $rows.'<br />'; 
     } 
    } 
} 

Si vous voulez afficher quoi que ce soit dans votre table et utiliser la variable rNumber (il semble qu'il n'y a pas besoin de passer $rows et $cols les variables ici)

Cependant votre code est vraiment mauvais, vous ne devriez pas mélanger le code PHP et le code HTML de cette façon. Si vous avez besoin de vous devez déplacer une partie de votre code vers un autre fichier et ne pas utiliser autant de echo car cela affecte également vos performances.

0

vous pouvez commencer par essayer de Changhe ce

$rNumber = mt_rand(3,11); 
}while ($rNumber % 2 == 0); 

à ceci:

$rNumber = mt_rand(3,11);        
    while ($rNumber % 2 == 0); 

et votre fonction:

function figure_b($rNumber) 
    { 
       if($rNumber == 5) 
       { 
        for($rows = 0; $rows<=5; $rows++) 
         { 
          for ($cols = 0; $cols<=5; $cols++) 
           { 

            echo $cols; 
           } 
            echo $rows; 
          } 
        } 
} 
Questions connexes