0
What is the way to use a foreach
for this situation ?
Would be selecting an element from tabela dois
- event - and for each event, pick up from tabela um
data that also have in its structure the same event.
By the way, when asking the question I saw in the related on INNER JOIN
, that would be one way ?
Update - Now it worked! :)
First was some details regarding the HTML table.
Was using <thread>
but it is <thead>
and the <th>
of the title outside a <tr>
does not work, I started to use <h>
.
Now the changes that really matter to logic.
I changed the structure of the fetch() loop to fetchAll(). The difference is that before I was creating a simple array and I wasn’t going through all the elements when filling the tables. With Fetchall I passed all the information from the Tickets table in one variable - Just like in the @Paulo example.
And the condition If
now it works well, taking the events with the same name and making the loop for each one.
MySQL
CREATE DATABASE Exercicio;
USE Exercicio;
CREATE TABLE IF NOT EXISTS eventos (
ID SMALLINT AUTO_INCREMENT PRIMARY KEY,
evento VARCHAR(50) NOT NULL
);
INSERT INTO eventos (evento) VALUES ('um');
INSERT INTO eventos (evento) VALUES ('dois');
INSERT INTO eventos (evento) VALUES ('tres');
CREATE TABLE IF NOT EXISTS ingressos (
ID SMALLINT AUTO_INCREMENT PRIMARY KEY,
nome VARCHAR(50) NOT NULL,
mesa VARCHAR(50) NOT NULL,
evento VARCHAR(50) NOT NULL
);
INSERT INTO ingressos (nome,mesa,evento) VALUES ('fulano', '30', 'um');
INSERT INTO ingressos (nome,mesa,evento) VALUES ('fulana', '35', 'um');
INSERT INTO ingressos (nome,mesa,evento) VALUES ('ciclano', '10', 'dois');
INSERT INTO ingressos (nome,mesa,evento) VALUES ('ciclana', '31', 'dois');
INSERT INTO ingressos (nome,mesa,evento) VALUES ('beltrano', '60', 'tres');
INSERT INTO ingressos (nome,mesa,evento) VALUES ('beltrana', '35', 'tres');
HTML
and PHP
<?php
include 'conexao.php';
$ingressos = $con->prepare("SELECT ingressos.nome, ingressos.mesa, ingressos.evento, eventos.evento FROM ingressos INNER JOIN eventos ON ingressos.evento = eventos.evento");
$ingressos ->execute();
$ingressos->setFetchMode(PDO::FETCH_ASSOC);
$dados = $ingressos->fetchAll();
$eventos = $con->prepare("SELECT evento FROM eventos");
$eventos ->execute();
?>
<!doctype html>
<html>
<body>
<?php
if($ingressos){
foreach($eventos as $evt){
echo " <div class='box[]'>
<table border='1px'>
<thead>
<h3> ".$evt['evento']." </h3>
<tr>
<th>mesa</th>
<th>nome</th>
</tr>
</thead> ";
foreach($dados as $ingr){
if($ingr['evento'] == $evt['evento']){
echo " <tbody> ";
echo " <tr> ";
echo "<td>" .$ingr['mesa']. "</td>";
echo "<td>" .$ingr['nome']. "</td>";
}}}}
echo " </tr> ";
echo " </tbody> ";
echo " </table> ";
echo "</div>";
?>
</body>
</html>
I’ve been running in the database adding events in the Events table and more tickets in the Tickets table and ready, all going your way!
you commented on the other uses of foreach, would it by any chance be as if I wanted to group mysql lines according to the common event ? I repeated his example and the foreach ends up generating a table for each row found, always having Event A or Event B repeating for example, but the data are organized correctly, the only thing is that they are not grouped in the same table. Returning to your example, would continue printing in Event A the rest of your INSERT, in the case of event A would have tbm the table 34 client for example. Thank you very much!
– Bender
I added a way I tried to do, it’s not like yours, the structure of Mysql is different, then take a look if you can.
– Bender
@Yemoja I have seen yes, already put as requested, it is a matter of logic only!
– Paulo Ricardo
@Yemoja Ready each table with their respective data.
– Paulo Ricardo
Thanks again @Paulo, but I still need to review some things... it’s still not what I imagined, I must be wanting to work wrong with the database. The idea was to take this data from the ticket table and organize it on a page and separate it into their respective events. I put the Event column in both to make this the common element. However, the events are logged dynamically, this is the need of foreach - I think - pro php see in Mysql how many html tables will have to create.
– Bender
Good morning @Yemoja - I found a way and I think now you can solve your problem!
– Paulo Ricardo
Let’s go continue this discussion in chat.
– Bender