par
rimie » 07 févr. 2012, 06:22
voila le dernier code que j'ai pu mettre:
Code: php
<?php
$connexion = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_passwd
, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$db_charset.'')); //SET NAMES utf8
$connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$type = 2011;
$cat = 'classe1';
$sql = 'SELECT * FROM jours WHERE annee = "'.$type.'" AND id_c = (SELECT idc FROM classes WHERE classe = "'.$cat.'") GROUP BY jour ORDER BY idj ASC';
//echo $sql;
$req = $connexion->query($sql);
$result = $req->fetchAll();
if(empty($result))
{
echo 'Aucune resultat';
}
else
{
echo '<table width="500" border="1">';
echo '<tr>';
echo '<td>Jour</td>';
echo '<td>Etudiants</td>';
echo '</tr>';
foreach($result as $k=>$v)
{
$jour = $v['jour'];
echo '<tr>';
echo '<td>'.$jour.'</td>';
echo '<td>';
//Lister les etudiants
// je mets * temporairement
$sqls = 'SELECT *
FROM joinTables
INNER JOIN etudiants ON joinTables.idetudiant = etudiants.ide
INNER JOIN jours ON joinTables.idjour = jours.jour
AND joinTables.idclasse = jours.id_c
AND joinTables.annee = jours.annee';
//echo $sql;
$reqs = $connexion->query($sqls);
$results = $reqs->fetchAll();
if(empty($results))
{
echo 'Aucun etudiant n a passe l exam';
}
else
{
foreach($results as $kk=>$vv)
{
$nom = $vv['nom'];
echo $etudiant;
}
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
?>
Les tables:
--
-- Table structure for table `classes`
--
CREATE TABLE IF NOT EXISTS `classes` (
`idc` int(2) NOT NULL AUTO_INCREMENT,
`classe` varchar(20) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idc`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `classes`
--
INSERT INTO `classes` (`idc`, `classe`) VALUES
(1, 'classe1'),
(2, 'classe2');
-- --------------------------------------------------------
--
-- Table structure for table `etudiants`
--
CREATE TABLE IF NOT EXISTS `etudiants` (
`ide` int(2) NOT NULL AUTO_INCREMENT,
`nom` varchar(30) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ide`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
--
-- Dumping data for table `etudiants`
--
INSERT INTO `etudiants` (`ide`, `nom`) VALUES
(1, 'etudiant1'),
(2, 'etudiant2'),
(3, 'etudiant3'),
(4, 'etudiant4'),
(5, 'etudiant5'),
(6, 'etudiant6'),
(7, 'etudiant7'),
(8, 'etudiant8'),
(9, 'etudiant9'),
(10, 'etudiant10'),
(11, 'etudiant11'),
(12, 'etudiant12'),
(13, 'etudiant13'),
(14, 'etudiant14'),
(15, 'etudiant15');
-- --------------------------------------------------------
--
-- Table structure for table `jointables`
--
CREATE TABLE IF NOT EXISTS `jointables` (
`idjoin` int(11) NOT NULL AUTO_INCREMENT,
`idetudiant` int(11) NOT NULL,
`idjour` int(11) NOT NULL,
`idclasse` int(11) NOT NULL,
`annee` varchar(4) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idjoin`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=15 ;
--
-- Dumping data for table `jointables`
--
INSERT INTO `jointables` (`idjoin`, `idetudiant`, `idjour`, `idclasse`, `annee`) VALUES
(1, 1, 1, 1, '2011'),
(2, 2, 1, 1, '2011'),
(3, 3, 2, 1, '2011'),
(4, 4, 2, 1, '2011'),
(5, 1, 3, 1, '2011'),
(6, 5, 3, 1, '2011'),
(7, 6, 3, 1, '2011'),
(8, 10, 1, 2, '2011'),
(9, 11, 1, 2, '2011'),
(10, 12, 2, 2, '2011'),
(11, 14, 2, 2, '2011'),
(12, 10, 3, 2, '2011'),
(13, 11, 3, 2, '2011'),
(14, 12, 3, 2, '2011');
-- --------------------------------------------------------
--
-- Table structure for table `jours`
--
CREATE TABLE IF NOT EXISTS `jours` (
`idj` int(3) NOT NULL AUTO_INCREMENT,
`jour` varchar(20) collate utf8_unicode_ci NOT NULL,
`id_c` int(2) NOT NULL,
`annee` varchar(4) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idj`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
--
-- Dumping data for table `jours`
--
INSERT INTO `jours` (`idj`, `jour`, `id_c`, `annee`) VALUES
(1, '1', 1, '2011'),
(2, '2', 1, '2011'),
(3, '3', 1, '2011'),
(4, '4', 1, '2011'),
(5, '5', 1, '2011'),
(6, '1', 2, '2011'),
(7, '2', 2, '2011');
resultat: etudiant1etudiant2etudiant3etudiant4etudiant1etudiant5etudiant6etudiant10etudiant11etudiant12etudiant14
voila le dernier code que j'ai pu mettre:
Code: php
[php]
<?php
$connexion = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_passwd
, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.$db_charset.'')); //SET NAMES utf8
$connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$type = 2011;
$cat = 'classe1';
$sql = 'SELECT * FROM jours WHERE annee = "'.$type.'" AND id_c = (SELECT idc FROM classes WHERE classe = "'.$cat.'") GROUP BY jour ORDER BY idj ASC';
//echo $sql;
$req = $connexion->query($sql);
$result = $req->fetchAll();
if(empty($result))
{
echo 'Aucune resultat';
}
else
{
echo '<table width="500" border="1">';
echo '<tr>';
echo '<td>Jour</td>';
echo '<td>Etudiants</td>';
echo '</tr>';
foreach($result as $k=>$v)
{
$jour = $v['jour'];
echo '<tr>';
echo '<td>'.$jour.'</td>';
echo '<td>';
//Lister les etudiants
// je mets * temporairement
$sqls = 'SELECT *
FROM joinTables
INNER JOIN etudiants ON joinTables.idetudiant = etudiants.ide
INNER JOIN jours ON joinTables.idjour = jours.jour
AND joinTables.idclasse = jours.id_c
AND joinTables.annee = jours.annee';
//echo $sql;
$reqs = $connexion->query($sqls);
$results = $reqs->fetchAll();
if(empty($results))
{
echo 'Aucun etudiant n a passe l exam';
}
else
{
foreach($results as $kk=>$vv)
{
$nom = $vv['nom'];
echo $etudiant;
}
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
?>
[/php]Les tables:
[sql]
--
-- Table structure for table `classes`
--
CREATE TABLE IF NOT EXISTS `classes` (
`idc` int(2) NOT NULL AUTO_INCREMENT,
`classe` varchar(20) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idc`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `classes`
--
INSERT INTO `classes` (`idc`, `classe`) VALUES
(1, 'classe1'),
(2, 'classe2');
-- --------------------------------------------------------
--
-- Table structure for table `etudiants`
--
CREATE TABLE IF NOT EXISTS `etudiants` (
`ide` int(2) NOT NULL AUTO_INCREMENT,
`nom` varchar(30) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ide`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
--
-- Dumping data for table `etudiants`
--
INSERT INTO `etudiants` (`ide`, `nom`) VALUES
(1, 'etudiant1'),
(2, 'etudiant2'),
(3, 'etudiant3'),
(4, 'etudiant4'),
(5, 'etudiant5'),
(6, 'etudiant6'),
(7, 'etudiant7'),
(8, 'etudiant8'),
(9, 'etudiant9'),
(10, 'etudiant10'),
(11, 'etudiant11'),
(12, 'etudiant12'),
(13, 'etudiant13'),
(14, 'etudiant14'),
(15, 'etudiant15');
-- --------------------------------------------------------
--
-- Table structure for table `jointables`
--
CREATE TABLE IF NOT EXISTS `jointables` (
`idjoin` int(11) NOT NULL AUTO_INCREMENT,
`idetudiant` int(11) NOT NULL,
`idjour` int(11) NOT NULL,
`idclasse` int(11) NOT NULL,
`annee` varchar(4) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idjoin`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=15 ;
--
-- Dumping data for table `jointables`
--
INSERT INTO `jointables` (`idjoin`, `idetudiant`, `idjour`, `idclasse`, `annee`) VALUES
(1, 1, 1, 1, '2011'),
(2, 2, 1, 1, '2011'),
(3, 3, 2, 1, '2011'),
(4, 4, 2, 1, '2011'),
(5, 1, 3, 1, '2011'),
(6, 5, 3, 1, '2011'),
(7, 6, 3, 1, '2011'),
(8, 10, 1, 2, '2011'),
(9, 11, 1, 2, '2011'),
(10, 12, 2, 2, '2011'),
(11, 14, 2, 2, '2011'),
(12, 10, 3, 2, '2011'),
(13, 11, 3, 2, '2011'),
(14, 12, 3, 2, '2011');
-- --------------------------------------------------------
--
-- Table structure for table `jours`
--
CREATE TABLE IF NOT EXISTS `jours` (
`idj` int(3) NOT NULL AUTO_INCREMENT,
`jour` varchar(20) collate utf8_unicode_ci NOT NULL,
`id_c` int(2) NOT NULL,
`annee` varchar(4) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`idj`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8 ;
--
-- Dumping data for table `jours`
--
INSERT INTO `jours` (`idj`, `jour`, `id_c`, `annee`) VALUES
(1, '1', 1, '2011'),
(2, '2', 1, '2011'),
(3, '3', 1, '2011'),
(4, '4', 1, '2011'),
(5, '5', 1, '2011'),
(6, '1', 2, '2011'),
(7, '2', 2, '2011');
[/sql]
resultat: etudiant1etudiant2etudiant3etudiant4etudiant1etudiant5etudiant6etudiant10etudiant11etudiant12etudiant14