doubt with do while not printing correct numbers

2

Good I'm doing some tables that print parimpar numbers and 5 in 5 but I already did them in for and while but when I try to do it in do while I throw another result I do not know what is due

this is with the for:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del for</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>
      <?php

for($i=1; $i<12; $i++)
{
    echo "<tr>";
echo "<td>".($i). "</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";

}
?>
    </tbody>
  </table>
</div>

</body>
</html>

THIS IS WITH THE WHILE:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del while</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>


<?php
$i=1;

while($i<12)
{
   echo "<tr>";
echo "<td>" .$i."</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";


   $i++;
}

?>

</tbody>
  </table>
</div>

</body>
</html>

AND THIS WITH THE DO WHILE DOES NOT WORSE THE RESULT

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del for</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>

<?php
$i=1;

do{

echo "<tr>";
echo "<td>".($i). "</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";



} while($i<12);


?>
 </tbody>
  </table>
</div>

</body>
</html>

I DO NOT KNOW WHICH THE OTHER RESULT IS HIRING

    
asked by wdwd 02.04.2018 в 09:41
source

1 answer

1

You need to increase the value of the variable $i in each iteration:

do{

    echo "<tr>";
    echo "<td>".($i). "</td>";
    echo "<td>".(11-$i)."</td>";
    echo "<td>".($i * 2 )."</td>";
    echo "<td>".($i * 2 -1)."</td>";
    echo "<td>".($i * 5 - 5)."</td>";
    echo "</tr>";

    /*Aquí hay que incrementar*/
    $i++;

} while($i<12);

Result:

table {
  border-collapse: collapse;
}

td, th {
  border: 1px solid #999;
  padding: 0.5rem;
  text-align: left;
}
<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
  <table class="table">
    <thead>

      <th>1 a 11</th>
      <th>11 a 1</th>
      <th> par </th>
      <th> impar </th>
      <th> 5 en 5 </th>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>10</td>
        <td>2</td>
        <td>1</td>
        <td>0</td>
      </tr>
      <tr>
        <td>2</td>
        <td>9</td>
        <td>4</td>
        <td>3</td>
        <td>5</td>
      </tr>
      <tr>
        <td>3</td>
        <td>8</td>
        <td>6</td>
        <td>5</td>
        <td>10</td>
      </tr>
      <tr>
        <td>4</td>
        <td>7</td>
        <td>8</td>
        <td>7</td>
        <td>15</td>
      </tr>
      <tr>
        <td>5</td>
        <td>6</td>
        <td>10</td>
        <td>9</td>
        <td>20</td>
      </tr>
      <tr>
        <td>6</td>
        <td>5</td>
        <td>12</td>
        <td>11</td>
        <td>25</td>
      </tr>
      <tr>
        <td>7</td>
        <td>4</td>
        <td>14</td>
        <td>13</td>
        <td>30</td>
      </tr>
      <tr>
        <td>8</td>
        <td>3</td>
        <td>16</td>
        <td>15</td>
        <td>35</td>
      </tr>
      <tr>
        <td>9</td>
        <td>2</td>
        <td>18</td>
        <td>17</td>
        <td>40</td>
      </tr>
      <tr>
        <td>10</td>
        <td>1</td>
        <td>20</td>
        <td>19</td>
        <td>45</td>
      </tr>
      <tr>
        <td>11</td>
        <td>0</td>
        <td>22</td>
        <td>21</td>
        <td>50</td>
      </tr>
    </tbody>
  </table>
    
answered by 02.04.2018 / 11:36
source