Problem with PHP program

Asked

Viewed 28 times

1

I’m in need of help with a php exercise I’m doing. I’m getting the following error message:

Notice: Undefined offset: 1 in C: xampp htdocs calendario.php on line 5

Receives the same error message for lines 5 to 10 of the code. These lines refer to the screen printing of the values that should be in a vector. From the error message, it seems that the vector did not receive the correct values.

Follows code in full:

<?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++;
        }
    }

?>

<table border="1">
    <tr>
        <th>Dom</th>
        <th>Seg</th>
        <th>Ter</th>
        <th>Qua</th>
        <th>Qui</th>
        <th>Sex</th>
        <th>Sáb</th>
    </tr>
    <?php calendario();?>

</table>
  • What do you want to do ?

  • This program will print a calendar using an html table. My problem is actually the error message. I couldn’t figure out why the mistake was made. Solving the error message problem I believe I can finish the program quietly.

1 answer

1


Your mistake is just a misspelled semicolon.

<?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){ // REMOVI O PONTO E VIRGULA DAQUI
                linha($semana);
                $semana=array();
            }
            $dia++;
        }
    }

?>

<table border="1">
    <tr>
        <th>Dom</th>
        <th>Seg</th>
        <th>Ter</th>
        <th>Qua</th>
        <th>Qui</th>
        <th>Sex</th>
        <th>Sáb</th>
    </tr>
    <?php calendario();?>

</table>
  • Thank you very much Allan. That’s right.

Browser other questions tagged

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