"Undefined index" and "Can not access empty property"

0

Hello everyone! Although there are many posts here in the Stack regarding this problem, none of them was enough so that I could resolve mine.

I'm creating an admin dashboard for a website whose homepage is basically a calendar, on whose dates that contain events you can click and open pages with lesson content.

In admin.php, I created a form to register new dates while automatically creating a page with some resources for the teacher to put content in it. The form has 3 fields: Title, Date and Meeting.

So I was editing the file cadastrando.php and I decided to make the same name written by the teacher in the title field, automatically added to the database in the last field of the form (called "meeting"), ".php" (which would become the file name of the newly created page). Then I'd delete the "meeting" field, so nobody could write anything inside. But I did not do this, since the error described in the title of this post occurred and the whole page of the admin was unconfigured, disappearing even the calendar.

It occurs that although I was moving the file cadastrando.php, the error describes a problem in calendario.php, that was working normally until then and was not stirred.

Anyway, if anyone can give a light, thank you very much.

Here is the error:

*calendar.phpline12:

$encontro=$info['encontro'];

*calendar.phpline20:

'encontro'=>$row->{$encontro}

Herearethecodes:

inadmin.php(intherelevantpart):

<!--cadastrarevento--><divclass="item-main-admin">

        <p2><center>
        <form name="cadastrarevento" method="post" action="cadastrando.php">
        Título: <input type="text" name="titulo" value="encontro-"/> </br>
        Data: <input type="text" name="data" placeholder="aaaa/mm/dd" /> </br>
        Encontro: <input type="text" name="encontro" /> </br>
        <input type="submit" value="Cadastrar evento" />
        </form>
        </p2></center>
    </div>




<!--calendario-->
<div class="item-main-admin">
    <?php 
    $eventos = montaEventos($info);
    montaCalendario($eventos); 
    ?>
</div>

<script type="text/javascript" src="/administrador/calendario/jquery.js"></script>
    <script type="text/javascript" src="/administrador/calendario/functions.js"></script>

No cadastrando.php:

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
$host = "localhost";
$user = "root";
$pass = "";
$banco = "calendario";
$conexao = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($banco) or die(mysql_error());
?>


<?php
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
?>


<?php

$titulo=$_POST['titulo'];
$data=$_POST['data'];
$encontro=$_POST['encontro'];


$myFile = "$titulo.php";


$result = mysql_query("SELECT titulo FROM eventos WHERE titulo = '$titulo'");

if (mysql_num_rows($result)>0) {
        echo "O título $titulo já existe. Crie um diferente.";
        } else {

    $sql = mysql_query("INSERT INTO eventos(titulo, data, encontro) 
    VALUES('$titulo', '$data', '$encontro')");


$fh = fopen($myFile, 'w');

$stringData = "<html><?php include 'template.php';?></html>";   


fwrite($fh, $stringData);
//header('Location: ' . $myFile);
fclose($fh);


?>



<div class="cadastro_ok">
<div id="item-cadastro_ok">
    <img src="imagens/ok.png" />
</div>
<div id="item-cadastro_ok">
    <?php echo "Cadastro efetuado com sucesso!"; ?>
</div>
</div>

<?php } ?>



<?php
echo '<meta http-equiv="refresh" content="3;URL=admin.php" />';
?>

</div>

</body>

</html>

**** NOTE: I had put it like this ($ myFile instead of $ encounter), and then I had the error, although it does not seem to come from there:

$sql = mysql_query("INSERT INTO eventos(titulo, data, encontro) 
    VALUES('$titulo', '$data', '$myFile')");

And in the calendar.php:

<?php

function num($num){
    return ($num < 10) ? '0'.$num : $num;
}
function montaEventos($info){
    global $pdo;
    //tabela, data, titulo, encontro
    $tabela = $info['tabela'];
    $data = $info['data'];
    $titulo = $info['titulo'];
    $encontro = $info['encontro'];
    $eventos = $pdo->prepare("SELECT * FROM '".$tabela."' ");
    $eventos->execute();
    $retorno = array();
    while($row = $eventos->fetchObject()){
        $dataArr = date('Y-m-d', strtotime($row->{$data}));
        $retorno[$dataArr] = array(
            'titulo' => $row->{$titulo},
            'encontro' => $row->{$encontro}
        );
    }
    return $retorno;
}
function diasMeses(){
    $retorno = array();
    for($i = 1; $i<=12;$i++){
        $retorno[$i] = cal_days_in_month(CAL_GREGORIAN, $i, date('Y'));
    }
    return $retorno;
}
function montaCalendario($eventos = array()){
    $daysWeek = array(
        'Sun',
        'Mon',
        'Tue',
        'Wed',
        'Thu',
        'Fri',
        'Sat'
    );
    $diasSemana = array(
        'Dom',
        'Seg',
        'Ter',
        'Qua',
        'Qui',
        'Sex',
        'Sab'
    );
    $arrayMes = array(
        1 => 'Janeiro',
        2 => 'Fevereiro',
        3 => 'Março',
        4 => 'Abril',
        5 => 'Maio',
        6 => 'Junho',
        7 => 'Julho',
        8 => 'Agosto',
        9 => 'Setembro',
        10 => 'Outubro',
        11 => 'Novembro',
        12 => 'Dezembro'
    );
    $diasMeses = diasMeses();
    $arrayRetorno = array();
    for($i =1; $i <= 12; $i++){
        $arrayRetorno[$i] = array();
        for($n=1; $n<= $diasMeses[$i]; $n++){
            $dayMonth = gregoriantojd($i, $n, date('Y'));
            $weekMonth = substr(jddayofweek($dayMonth, 1),0,3);
            if($weekMonth == 'Mun') $weekMonth = 'Mon';
            $arrayRetorno[$i][$n] = $weekMonth;
        }
    }
    echo '<a href="#" id="volta">&laquo;</a><a href="#" id="vai">&raquo;</a>';
    echo '<table border="0" width="100%">';
    foreach($arrayMes as $num => $mes){
        echo '<tbody id="mes_'.$num.'" class="mes">';
        echo '<tr class="mes_title"><td colspan="7">'.$mes.'</td></tr>';
        echo '<tr class="dias_title">';
        foreach($diasSemana as $i => $day){
            echo '<td>'.$day.'</td>';
        }
        echo '</tr><tr>';
        $y = 0;
        foreach($arrayRetorno[$num] as $numero => $dia){
            $y++;
            if($numero == 1){
                $qtd = array_search($dia, $daysWeek);
                for($i=1; $i<=$qtd; $i++){
                    echo '<td></td>';
                    $y+=1;
                }
            }
            if(count($eventos) > 0){
                $month = num($num);
                $dayNow = num($numero);
                $date = date('Y').'-'.$month.'-'.$dayNow;
                if(in_array($date, array_keys($eventos))){
                    $evento = $eventos[$date];
                    echo '<td class="evento"><a href="'.$evento['encontro'].'" title="'.$evento['titulo'].'">'.$numero.'</a></td>';
                }else{
                    echo '<td class="dia_'.$numero.'">'.$numero.'</td>';
                }
            }else{
                echo '<td class="dia_'.$numero.'">'.$numero.'</td>';
            }
            if($y == 7){
                $y=0;
                echo '</tr><tr>';
            }
        }
        echo '</tr></tbody>';
    }
    echo '</table>';
}
?>
    
asked by anonymous 11.01.2018 / 05:49

1 answer

0

The error is because the index encontra in $info['encontro'] . The $info variable in the montaEventos method does not have a value.

To fix this, you should use a ternary operator >.

It works as follows:

(expr1) ? value1 : value2;

It will compare to expr1 , if it is true, it returns value1 , otherwise it returns value2

This ternary is nothing more than a if..else is a single line.

To correct this error you must create a condition to check if content $info['encontro'] exists, if it does not exist, you can add a new value or return null .

Ex ( PHP 7 + ): $encontro = $info['encontro'] ?? null;

Ex

11.01.2018 / 12:27