I'm implementing PHP Object Oriented and I'm developing a CRUD of students.
I have a class to count the number of students in the bank. And I wanted to get the return of the function that counts the students and send that result to index, but I'm having a hard time doing that.
class ShowAlumnus.php:
<?php
class MostraAluno
{
protected $nome, $media;
public $dns = 'mysql:host=localhost;dbname=alunos';
public $user = 'root';
public $pass = 'vertrigo';
public $pdo;
public function __construct()
{
try {
$this->pdo = new PDO($this->dns, $this->user, $this->pass);
} catch (PDOException $e) {
die('Erro de conexao com o banco de dados: ' . $e->getMessage());
}
}
public function contaAluno()
{
$count = "SELECT COUNT(*) AS total FROM usuarios ORDER BY nome ASC";
$stmt_count = $this->pdo->prepare($count);
$stmt_count->execute();
$total = $stmt_count->fetchColumn();
return $total;
}
}
And the index.php:
<?php
require_once 'core/MostraAluno.php';
$mostraAluno = new MostraAluno();
$alunos = $mostraAluno->mostraAluno();
$totAlunos = $mostraAluno->contaAluno();
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="static/css/style.css">
<meta charset="utf-8">
<title>Notas - Alunos</title>
</head>
<body>
<!--
=======================================================================
NAVBAR
=======================================================================
-->
<nav class="navbar navbar-inverse">
<div id="inicio" class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#inicio"><b>Sistema de notas</b></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="cadastrar.php"><u>Cadastrar novo aluno</u></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Perfil <span class="caret"></span></a>
<ul class="dropdown-menu">
<li role="separator" class="divider"></li>
<li><a href="core/logout.php">Sair</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!--
=======================================================================
CORPO
=======================================================================
-->
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="background-color: ">
<center><h4 class="bg-info"><b>Total de alunos: <?php echo $totAlunos; ?></b></h4></center>
</div>
</div>
When you run the index, only the text 'Total students:' appears and there is no return of the method.
In the file MostraAluno
I have removed the method mostraAluno()
, because my question is only with the contaAluno()
method.
Could someone tell me where I'm going wrong? I am now starting to implement PHP OO and am having trouble returning the method values to other files.