1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
<?php
// Table SQL
/*
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`price` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=79 ;
INSERT INTO `products` (`id`, `name`, `price`) VALUES
(1, 'Test name', 280),
(2, 'Motorola Google Nexus 6, Midnight Blue 32GB', 400),
(3, 'Samsung Galaxy S4 i9500 16GB', 600),
(6, 'Bench Men''s Bench Spokes Slim T-Shirt', 14),
(7, 'HP ZBook 17 Mobile Business Workstation', 514),
(8, 'Samsung Galaxy Tab 4', 210),
(9, 'Spalding Men', 49),
(10, 'Sony Smart Watch 3', 194),
(11, 'Huawei SnapTo', 179),
(12, 'Abercrombie Men''s Lake Arnold Blazer', 25);
ALTER TABLE `products`
ADD PRIMARY KEY (`id`);
ALTER TABLE `products`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=13;
*/
$connexion = new PDO('mysql:host=localhost;dbname=tests', 'root', 'root', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$sql_select = $connexion->prepare("SELECT * FROM products ORDER BY id ASC");
$sql_select->setFetchMode(PDO::FETCH_OBJ);
$sql_select->execute();
$tableau_valeurs = array('btn_ajout' => true, 'Nom' => 'name', 'Prix' => 'price');
$nombre_de_champs_tableau = count($tableau_valeurs) -1; // On enlève le bouton ajout
$output = '';
// Affichage de l'entete du tableau
$affichage_entete = '';
$affichage_body = '';
$i = 0;
foreach($tableau_valeurs as $cle => $valeur) {
if ($cle == "btn_ajout" and $valeur == true) { $output .= '<h2>OBTENU</h2><p><a href="ajouter.php" class="btn btn-primary btn-primary"><span class="glyphicon glyphicon-plus"></span> Ajouter</a></p>'; } // Si bouton ajout
else {
$affichage_entete .= '<td>'.$cle.'</td>';
// Affichage des données du tableau
if ( ($i%$nombre_de_champs_tableau) == 1) {$affichage_body .= '<tr>'; }
$affichage_body .= '<td>'.$valeur.'</td>';
if ( ($i%$nombre_de_champs_tableau) == 0) {$affichage_body .= '</tr>'; }
}
$i++;
}
$output .= '
<table cellspacing="0" cellpadding="0" class="tableaux_centrer tableaux_bordure" border="1">
<tr class="entete">'.$affichage_entete.'</tr>
<tbody>'.$affichage_body.'</tbody>
</table>
';
echo $output;
?>
<h2>ATTENDU</h2>
<p><a href="ajouter.php" class="btn btn-primary btn-primary"><span class="glyphicon glyphicon-plus"></span> Ajouter</a></p>
<table cellspacing="0" cellpadding="0" class="tableaux_centrer tableaux_bordure" border="1">
<tr class="entete">
<td>Nom</td><td>Email</td>
</tr>
<tbody>
<?php
while($row_select = $sql_select->fetch()) {
if ( ($i%$sql_select->rowCount()) != 1 ) { echo '<tr>'; }
?>
<td><?php echo $row_select->name; ?></td><td><?php echo $row_select->price; ?></td></tr>
<?php
if ( ($i%$sql_select->rowCount()) == 1 ) { echo '</tr>'; }
}
?>
</tbody>
</table> |
Partager