Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
CREATE TABLE clients( Id INT AUTO_INCREMENT, Client VARCHAR(300) NOT NULL, Categorie VARCHAR(300) NULL, Credit VARCHAR(300) NULL, PRIMARY KEY(Id) ); INSERT INTO clients ( Client,Categorie,Credit ) VALUES ('Ray Mathieu','Pratiquant',100); INSERT INTO clients ( Client,Categorie,Credit ) VALUES ('Collette Antoine','Staff',0); CREATE TABLE rotations ( Id INT AUTO_INCREMENT, Client VARCHAR(300) NOT NULL, Saut VARCHAR(300) NULL, Cout VARCHAR(300) NULL, PRIMARY KEY(Id) ); INSERT INTO rotations ( Client,Saut,Cout ) VALUES ('Ray Mathieu','Standard 4000m',1); INSERT INTO rotations ( Client,Saut,Cout ) VALUES ('Collette Antoine','Init B4',2); INSERT INTO rotations ( Client,Saut,Cout ) VALUES ('Ray Mathieu','Standard 1500m',11); INSERT INTO rotations ( Client,Saut,Cout ) VALUES ('Collette Antoine','Standard 4000m',22);

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php $Rep_Clients = $pdo->query("SELECT * FROM clients"); $Clients = $Rep_Clients->fetchAll(PDO::FETCH_ASSOC); //print_r($Clients); echo ' <table> <tr> <th>Client</th> <th>Categorie</th> <th>Credit</th> <th>Debit</th> <th>Solde</th> </tr>'; $i = 0; foreach ($Clients as $Client) { $Rep_Debits = $pdo->query("SELECT sum(Cout) as Debit FROM rotations WHERE Client = '".$Client['Client']."' "); $Debits = $Rep_Debits->fetch(); //print_r($Debits); $Clients[$i]['Debit'] = $Debits['Debit']; $Clients[$i]['Solde'] = ($Client['Credit']-$Debits['Debit']); $i++; } $Tri = 'Client'; //$Tri = 'Categorie'; //$Tri = 'Credit'; //$Tri = 'Debit'; //$Tri = 'Solde'; $Ordre = 'ASC'; //$Ordre = 'DESC'; if ($Ordre == 'ASC') { echo array_multisort(array_column($Clients, $Tri), SORT_ASC, $Clients); } if ($Ordre == 'DESC') { echo array_multisort(array_column($Clients, $Tri), SORT_DESC, $Clients); } foreach ($Clients as $Client) { echo ' <tr> <td>'.$Client['Client'].'</td> <td>'.$Client['Categorie'].'</td> <td>'.$Client['Credit'].'</td> <td>'.$Client['Debit'].'</td> <td>'.$Client['Solde'].'</td> </tr> '; } echo '</table>'; print_r($Clients); ?>
Copy Clear