Your architecture is a mess!
Let’s go in parts, before, understanding of HTTP:
- the client makes a request
- the server receives this request
- the server processes this request
- ... still processing...
- the server responds with data
- the client downloads the request
- if it is a browser that is receiving HTML, the client renders
So your processing is free. And how do you do the processing? You said you want to "move from one PHP file to another PHP file", which doesn’t exactly make sense... PHP files weren’t meant to talk to each other the way you indicated. It even seems that you wanted a file to be executed, returned to the client and that then made a request right after to rescue the data obtained by the previous script. At least that’s how I understood your line.
Usually what is done is to create a collection of functions in a file more focused on the work of backend and, in the file more frontend, call these functions and display them correctly. To do this, you do not need to separate beautifully across all layers of MVC.
For example, this separation of data retrieval from the database and then display was made in that other question:
- to original reply from @Sveen used the principle of separation of interface and bank
- to my bitch (which is a variation of the @Sveen response, as I pointed out in the text) only gives an easier maintenance air; I turned a file inclusion usage into a function call
Because the only difference between the two answers was that I used a function, I’m going to focus on the original repsosta, @Sveen’s. The idea here was:
- isolate in a component the display of a bank line
- in the frontend, first make the database data independent basic statement
- then into the core of frontend, obtain the various lines and, for each one, call the line display
Adapting to your case, it could be something like this:
back php:
<?php
function obter_linhas($nome) {
$con = new mysqli("host", "user", "pass", "db");
$res = $con->query("SELECT * FROM tabela WHERE coluna = '$nome'");
return $res->fetch_all();
}
?>
front php.:
<?php include_onde 'back.php' ?>
<html>
<body>
<h1>Hello, world!</h1>
<?php
foreach (obter_linhas($_GET['nome'] as $linha) {
echo "<li>Olha a primeira posição do array: ".$linha[0]."</li>";
}
?>
</body>
</html>
Ready. We did the visualization of the data independently of the obtainment. In a way, we separated the "front" and the "back".
About the mess... the use apparent resembles very much something that would be an adaptive page. So, in that case, you wouldn’t need to submit the page for a full reload. An asynchronous reload of the minimally necessary part would be enough. For example, you could separate a <div>
for your results and, when you redeem the values, write in this <div>
(including discarding the previous values).
Using this type of thinking, you can have interfaces that display new information without needing a full page load:
To do this, you would need to send the data (the data itself, not the display of it; it is usually transferred in json
or xml
such data) from a endpoint PHP. On the client side, Javascript would take care of receiving this data, interpreting and then displaying it in the most appropriate way possible.
You will find more about this partial data request by searching about AJAX. By the way, this acronym means (or meant in the origins):
Tosynchronous JavaScript tond XML
In free translation:
Asynchronous Javascript and XML
Here, asynchronous means that the script will call a endpoint on the server and it will respond when possible, having no guarantee of when this occurs. Javascript is the programming language that makes the call and then treats the obtained response. And XML today is understood only as a data transfer, it doesn’t need to be the XML data transfer format.
Another use of AJAX you can see here, in Gitlab:
Here, when asking to display a tab, Gitlab starts by doing the asynchronous request, then putting the placeholder of the balls spinning so I think he’s working. When the request is finally completed, a Javascript intercepts the data and tries to display it to me in the best way possible.
In your comments you refute all the answers. This is a sign that your question is not clear. How do these controllers work? When you say "controller," everyone thinks about MVC and the function they have in that pattern. You don’t tell the function of controllers in your application, and why they need to communicate with each other - which is quite unusual in "traditional" controllers, by the way. Also explain about views and how they are loaded.
– bfavaretto
I can tell you that using sessions for everything you seem to be doing is the worst possible solution.
– bfavaretto
@bfavaretto so I asked that question
– Costamilam
@bfavaretto rephrased the question I hope it became clearer
– Costamilam
I’m reversing your issue because the question has become wider, and invalidated the previous answers.
– bfavaretto
I have no idea what "pass data between files" is. What kind of data, what kind of files? It would be nice to describe a real scenario. " Passing data between PHP files" is not usually done. What is done is HTML to PHP and vice versa. From PHP to PHP you do include, usually, or save somewhere (and it’s atypical anyway). The question is definitely not clear. I would suggest practical examples, so we have how to help.
– Bacco
@Bacco really. I deleted my answer because I may have misinterpreted it. The question is wide.
– Andrei Coelho
Another thing. I think that asking for the BEST form becomes a question of opinion many times, because in programming, often, it has several ways of doing. It would be interesting to ask for suggestions like @Bacco said. The best way, who will identify, is you. Because you know the project.
– Andrei Coelho
Do you have any code you have made? It may be clearer.
– Andrei Coelho
@bfavaretto updated the question, I believe my doubt is clearer now
– Costamilam
Since much of the question is crossed out, it was removing that which you yourself considered important. If you disagree, just undo ;-) Of all sorts, you gain visibility
– Jefferson Quesado