Calling two PHP functions in an HTML file

0

I have some information in a database in MySql . I want to show this information on a HTML page supporting me with PHP .

When I show the information of only one table there is no problem. The problem comes when I want to show another table on the same page. The code is as follows:

functions.php

<?php
require "config.php"; /*Archivo de conexión a BD*/

class eventosEscolares{
function __construct($db){
    $this->db= $db;
}

public function informacionA(){
    $query = $this->db->prepare("SELECT * FROM info_a");
    $query->execute();
    return $query->fetchAll(PDO::FETCH_ASSOC);
}
public function informacionB(){
    $query = $this->db->prepare("SELECT * FROM info_b");
    $query->execute();
    return $query->fetchAll(PDO::FETCH_ASSOC);
}

}
?>

desplegar.php

<?php
require_once "functions.php";
$db = new DatabaseConnection();
$evento = new eventosEscolares($db->pdo);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>

<body>
<nav class="main-nav">
<div class="container">
<a href="index.html">Inicio</a>
<a href="nosotros.html">Nosotros</a>
<a href="contacto.html">Contacto</a>
<a href="#">Anuncios</a>
</div>
</nav>


<div id="p_div">
<table>
<tr>
<td>Día</td><td>Descripción</td>
</tr>
<?php
$i=0;
$inf=$evento->informacionA();
foreach($inf as $evento){
    $i++
?>
<tr><td><?php echo "$evento[dia]" ?></td><td><?php echo "$evento[descripcion]" ?></td></tr>
    <?php } ?>
</table>


<table>
<tr>
<td>Día</td><td>Descripción</td>
</tr>
<?php
$i=0;
$infb=$evento->informacionB();
foreach($infb as $evento){
    $i++ ?>
<tr><td><?php echo "$evento[dia]" ?></td><td><?php echo "$evento[descripcion]" ?></td></tr>
<?php } ?>
</table>
</div>
</body>
</html>

Doing it this way I get the error:

  

Fatal error: Call to a member function information () on array in   desplegar.php on line 59

How can I correct it so that it shows me both tables correctly?

    
asked by He_slp13 04.07.2017 в 20:19
source

2 answers

0

the error is syntax:

Call to a member function informacioB()

This function does not exist, check if it is well written

ALSO:

the arrays are wrong should be: $evento['dia'] using single quotes instead of $evento[dia] and make sure they are equal to the names of the columns.

    
answered by 04.07.2017 в 20:25
0

The error is due to:

  • First you create the variable $evento doing $evento = new eventosEscolares($db->pdo); , which returns a object .
  • Later you call $inf=$evento->informacionA();
  • Here the error : When iterating the variable $inf , doing foreach($inf as $evento) , what happens is that the variable $evento is now a array and not a object ( instance of eventosEscolares ).

Solution:

You could use another name for the variable where you assign the values of the array $inf , that is, instead of $evento , you could use $data

Example:

<?php
require_once "functions.php";
$db = new DatabaseConnection();
$evento = new eventosEscolares($db->pdo);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>

<body>
<nav class="main-nav">
<div class="container">
<a href="index.html">Inicio</a>
<a href="nosotros.html">Nosotros</a>
<a href="contacto.html">Contacto</a>
<a href="#">Anuncios</a>
</div>
</nav>


<div id="p_div">
<table>
<tr>
<td>Día</td><td>Descripción</td>
</tr>
<?php
$i=0;
$inf=$evento->informacionA();
foreach($inf as $data){
    $i++
?>
<tr><td><?php echo $data['dia'] ?></td><td><?php echo $data['descripcion'] ?></td></tr>
    <?php } ?>
</table>


<table>
<tr>
<td>Día</td><td>Descripción</td>
</tr>
<?php
$i=0;
$infb=$evento->informacionB();
foreach($infb as $data){
    $i++ ?>
<tr><td><?php echo $data['dia'] ?></td><td><?php echo $data['descripcion'] ?></td></tr>
<?php } ?>
</table>
</div>
</body>
</html>
    
answered by 04.07.2017 в 21:06