Jump to content

return array problem


gmc1103

Recommended Posts

Hi

 

I nedd some help regarding this function

 public function newPdfPedido($atividade, $id_turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $id_professor){
        try{
            $stmt = $this->db->prepare("SELECT `turma` FROM `turmas` WHERE `id_turma`=:id_turma;");
            $stmt->execute(array(":id_turma" => $id_turma));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            $turma = $userRow['turma'];
            if($turma == ""){
                print_r($stmt->errorInfo());
            }
            else{
                $stmt = $this->db->prepare("SELECT `nome` FROM `professor` WHERE `id_prof` = :id_prof;");
                $stmt->execute(array(":id_prof" => $id_professor));
                $prof = $stmt->fetch(PDO::FETCH_ASSOC);
                $professor = $prof['nome'];
                if($professor == ""){
                    print_r($stmt->errorInfo());
                }
                else{
                    return array($atividade, $id_turma, $turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $professor);
                }
            }
        }
        catch (Exception $ex) {
            echo $ex->getMessage();
        }

    }

and then i want to get the values here in another file

list($atividade, $id_turma, $turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $professor)= $esmaior->newPdfPedido();
echo $atividade;

and i'm getting those errors

Warning: Missing argument 1 for ESMAIOR::newPdfPedido()
Warning: Missing argument 2 for ESMAIOR::newPdfPedido()
Warning: Missing argument 3 for ESMAIOR::newPdfPedido()
Warning: Missing argument 4 for ESMAIOR::newPdfPedido()
Warning: Missing argument 5 for ESMAIOR::newPdfPedido()
Warning: Missing argument 6 for ESMAIOR::newPdfPedido()
Warning: Missing argument 7 for ESMAIOR::newPdfPedido()
Warning: Missing argument 8 for ESMAIOR::newPdfPedido()
Warning: Missing argument 9 for ESMAIOR::newPdfPedido()

But i want the values from the array...

 

Link to comment
Share on other sites

Hi

 

i understand this.....but i don't want to pass parameters here

list($atividade, $id_turma, $turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $professor)= $esmaior->newPdfPedido();

i want to receive those parameters....

So...my function receives the parameters

 public function newPdfPedido($atividade, $id_turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $id_professor){
return array($atividade, $id_turma, $turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $professor);
}

And in the final of this function...i want to send parameters with return to the new file

 

So how can i get the return array values to that new file?

Link to comment
Share on other sites

The entire method makes no sense.

 

You have 9 parameters, you use 2, and you want none. Um, what? At the same time you're pulling variables out of nowhere. Where is $atividade coming from? What is $local? Are those supposed to be properties, i. e. $this->atividade and $this->local?

Edited by Jacques1
Link to comment
Share on other sites

Hi Jacques

 

Thanks for your apport.

First you don't see all the method. The following receives variables from an "insert method"

 public function newPdfPedido($atividade, $id_turma, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $id_professor){}

And after receiving those variables i must keep them in array and then call that method from the other page. The problem is this method expects variables, so the best way to do will be using getter and setters.

Link to comment
Share on other sites

The problem is that you don't really seem to understand basic programming concepts (like methods) and terminology. This makes it very difficult to figure out what you're trying to do, let alone fix the actual code.

 

I strongly recommend that you learn the basics before you try to write any complex code. The PHP manual may be a good starting point.

Link to comment
Share on other sites

Hi Jacques

 

I know methods.....the problem is not getting the values i'm expecting

For example i have this method

  public function getTurmaeProfessor(){
        global $myArr;
        $myArr[] = $this->getProfessor();
        $myArr[] = $this->getTurma();
        print_r($myArr);
        return $myArr;
    }

The print_r gives me this

Array ( [0] => Name of someone [1] => 5ºA )

When i call this method from outside function i have empty array.....normal?? I don't think so.

The variables seems to disapear...and as you can see they are there, so what's hapening?

If i put a local variable...the method works, with the getters only works inside that function

Link to comment
Share on other sites

I know methods.....

 

Sorry, but you don't.

 

I'm not saying this to attack you. It's simply the only valid advice we can give you: You need to learn the basics of variables, functions and object-oriented programming.

 

Right now, you're just writing down random code and confusing yourself. This doesn't work. You need to actually understand how PHP works.

Link to comment
Share on other sites

Ok..

Let me start again and trying to explain me better

This is my class

class ESMAIOR
{
    private $email;
    public $db;
    public $turma;
    public $professor;
    function __construct($DB_con) {
        $this->db = $DB_con;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getTurma()
    {
        return $this->turma;
    }

    public function setTurma($turma)
    {
        $this->turma = $turma;
    }
    
    public function getProfessor()
    {
        return $this->professor;
    }

    public function setProfessor($professor)
    {
        $this->professor = $professor;
    }

The user fill a form, the values of that form are registered into database with this function inside the class

 public function novaAula($atividade, $turmas, $local, $dataAula, $inicio, $fim, $fundamentacao, $observacoes, $id_professor){
        try{
            $stmt = $this->db->prepare("INSERT INTO `aulas_exterior` (`atividade`,`id_turma`,`local`,`data_aula`,`inicio`,`fim`,`fundamentacao`,`observacoes`) 
                                VALUES (:atividade,:id_turma,:local,:data_aula,:inicio,:fim,:fundamentacao,:observacoes);");
            $stmt->bindparam(":atividade", $atividade);
            $stmt->bindparam(":local", $local);
            $stmt->bindparam(":data_aula", $dataAula);
            $stmt->bindparam(":inicio", $inicio);
            $stmt->bindparam(":fim", $fim);
            $stmt->bindparam(":fundamentacao", $fundamentacao);
            $stmt->bindparam(":observacoes", $observacoes);
            $turma = explode(',', $turmas);
            foreach ($turma as $id_turma) {
                $stmt->bindParam(':id_turma', $id_turma, PDO::PARAM_INT);
                $result = $stmt->execute();
                $idAula = $this->db->lastInsertId();
                $this->insertPedidoByLastId($idAula,$id_professor, $id_turma);
                $nome_turma = $this->getTurmaById($id_turma);
                $nome_professor = $this->getNomeById($id_professor);
                $this->setTurma($nome_turma);
                $this->setProfessor(($nome_professor));
                $this->getTurmaeProfessor();
            }
            if (!$result) {
                print_r($stmt->errorInfo());
                return array('status' => 'error', 'message' => 'Problema ao gravar esta nova atividade...');
            }
            else{
                return array('status' => 'success', 'message' => 'O pedido foi criado com sucesso...');
            }
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

In this function i use the following lines

$nome_turma = $this->getTurmaById($id_turma); // call this fucntion to get the name by id
$nome_professor = $this->getNomeById($id_professor);// call this fucntion to get the name by id
$this->setTurma($nome_turma); //setting the name in variable defined outside the function
$this->setProfessor(($nome_professor));//setting the name in variable defined outside the function
$this->getTurmaeProfessor(); //Call the method i want to use the return without arguments since i have set the variables i need

Inside this funtion getTurmaeProfessor()

public function getTurmaeProfessor(){
        $myArr[] = $this->getProfessor(); //Getting the teacher name 
        $myArr[] = $this->getTurma(); // Getting the class name
        print_r($myArr);
    }
//With this print i see i'm receiving the variables - Array ( [0] => Tony Morias [1] => 5ºA )

Until now everything is ok.

 

Now i need to print a pdf (another file) with the name of teacher and the name of class

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once '../db/dbconfig.php';
$esmaior = new ESMAIOR($DB_con);
require_once __DIR__ . '/../vendor/autoload.php';
set_include_path('../src/' . PATH_SEPARATOR . get_include_path());
$dados = $esmaior->getTurmaeProfessor();
    if(!$dados){
        echo "sem dados";
    }
    print_r($dados);
//print_r($dados) gives me - Array ( [0] => [1] => )

So i don't see why i'm not receiving the values, it seems the variables setted disapears...

 

Thanks

Edited by gmc1103
Link to comment
Share on other sites

When you change the attributes of an object, you only change them for this particular object. And the entire object is destroyed once script execution ends.

 

You seem to assume that a freshly created object somehow has access to the attributes of an entirely different object within an entirely different PHP process that has already been terminated. This doesn't work. This doesn't make sense.

 

And even if I were to ignore all this, the code still doesn't make any sense. In your strange novaAula method, you call the setters within a loop, which means you keep overriding and losing the previous attribute value.

 

I'm out. All I can tell you is that you definitely need to learn the basics, or else you'll spend the next days, weeks, months fighting “strange problems” that only come from not understanding how PHP works.

Link to comment
Share on other sites

Let me tell you that you have a fantastic way of helping...Congratulations

What have you done so far is telling me 0. Why i have this problem? No one clue.

If it is the way you are here for helping those who want to learn.....let me tell you, nobody will

 

By the way...i have fixed with $_SESSION....

 

Thank you for.....nothing.

Edited by gmc1103
Link to comment
Share on other sites

With that attitude, you won't get anywhere, not here, not in other forums, not in real life.

 

We've literally dragged you through the PHP basics, explaining them over and over again, desparately trying to make sense of your weird ideas and misunderstandings. I kept going even when it was clear that you don't know anything and don't want to learn either.

 

That was clearly a mistake.

 

If you're just stressed out and want to apologize later, feel free to do that. Otherwise you can go find yourself.

Link to comment
Share on other sites

Sorry

I did mean to hurt your feeling in any way.

About dragged me to Basics.....the only thing you told me was to learn, you didn't specify any solution to my case. I must learn? Of course i do, but that was no reason to tell me this

 

The problem is that you don't really seem to understand basic programming concepts (like methods) and terminology. This makes it very difficult to figure out what you're trying to do, let alone fix the actual code.

 

I strongly recommend that you learn the basics before you try to write any complex code. The PHP manual may be a good starting point.

 

You said i don't understand basic programming, the function says the opposite, this function is to register the data in DB, i use loop, i call others functions...and you tell me i don't know the basics?? 

I'm sure i know the basics, if you understand the basics is for experienced programmer, well, then i don't.

 

Regards and sorry if i have hurt your feelings some how.

Link to comment
Share on other sites

You haven't hurt anybody's feelings. I simply do not accept a “Thank you for nothing” comment after having spent quite a lot of time on your questions in multiple threads. But since we've settled this, I have no problem with you.

 

I understand that you'd like to get a solution to every PHP problem within seconds. But we're no mind readers. Before we can jump to solutions, we need to agree on the problem. In your first post, you tried to pass zero arguments to a function with 9 parameters. How are we supposed to “solve” this? This is a conceptual issue. The only solution is to explain how parameters work and find out what you're trying to do.

 

Keep in mind: We don't know your project, we don't know your thoughts, we're not sitting in front of your PC. We only see your posts and a couple of code fragments, and when those don't really make sense, you can't expect a quick solution.

 

By the way: When I post a link to the manual, this is serious advice, not a joke or an insult. I myself look up topics in the manual all the time, simply because it's the definite language reference. This is how I learned (and still learn) PHP.

Link to comment
Share on other sites

Hi Jacques

 

The problem was not explain the way i should. Of course you don't know my project, of course you don't know my thoughts.

In the first post i wasn't expecting in quick fix. I have follow a path to do what was in my mind

  • Form for the users fill and register into database;
  • Get the name and class of the id registered into database;
  • Fill a pdf you all those data

What i did wrong?

I wanted to pass the name of teacher and the class name to the pdf after being registered into the database. But then in my html i was calling a file where i had this function to retrieve those name and this was wrong because if my loop had more than 1 only 1 pdf was shown and the array was coming empty.

What i did then?

I move all my code to the main class, then i call a function to create the pdf and send it to Head of school by email.

I still have a lot to learn, but sometimes we need a extra help.

 

Anyway sorry for my post 

 

Regards

Gil

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.