Error in php’s Count function. Warning: Count(): Parameter must be an array or an Object that Implements Countable in line 41

Asked

Viewed 1,485 times

0

<!DOCTYPE html>
<html>
<head>
    <title>Calendario</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>Dom</th>
            <th>Seg</th>
            <th>Ter</th>
            <th>Qua</th>
            <th>Qui</th>
            <th>Sex</th>
            <th>Sab</th>
        </tr>
        <?php calendario(); ?>

    </table>
    <?php
        function linha($semana)
        {
            echo"
                <tr>
                    <td>{$semana[0]}</td>
                    <td>{$semana[1]}</td>
                    <td>{$semana[2]}</td>
                    <td>{$semana[3]}</td>
                    <td>{$semana[4]}</td>
                    <td>{$semana[5]}</td>
                    <td>{$semana[6]}</td>
                </tr>
            ";
        }
        function calendario()
        {
            $dia =1;
            $semana = array();
            while($dia<=31){
                array_push($semana, $dia);
                if(count($semana == 7)){
                    linha($semana);
                    $semana = array();
                }
                $dia++;
            }
        }

        ?>
</body>
</html>

2 answers

1


This is wrong:

count($semana == 7)

When does $semana == 7 you take a boolean (true or false) and not the array itself, it should probably be:

count($semana) == 7
  • 1

    Fighting guy’s working agr!

  • @Billyjean has resolved, be sure to mark the response with . I recommend reading the page [tour] site.

1

Just commenting on an alternative, you can use the functions range and array_chunk to generate the weeks you want:

$semanas = array_chunk(range(1, 31), 7);

This will generate the array:

(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
        )

    [1] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 10
            [3] => 11
            [4] => 12
            [5] => 13
            [6] => 14
        )

    [2] => Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
            [3] => 18
            [4] => 19
            [5] => 20
            [6] => 21
        )

    [3] => Array
        (
            [0] => 22
            [1] => 23
            [2] => 24
            [3] => 25
            [4] => 26
            [5] => 27
            [6] => 28
        )

    [4] => Array
        (
            [0] => 29
            [1] => 30
            [2] => 31
        )

)

Then you could do something like:

foreach ($semanas as $semana) {
  echo "<tr>";
  foreach ($semana as $dia) {
    echo "<td>{$dia}</td>";
  }
  echo "</tr>";
}

Exit:

<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
  </tr>
  <tr>
    <td>8</td>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>14</td>
  </tr>
  <tr>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td>21</td>
  </tr>
  <tr>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>25</td>
    <td>26</td>
    <td>27</td>
    <td>28</td>
  </tr>
  <tr>
    <td>29</td>
    <td>30</td>
    <td>31</td>
  </tr>
</table>

Obviously this code will not consider cases that the month does not start on Sunday.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.