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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
<?php
class Meteo
{// fonction pour lire le fichier .csv et récupération des données dans un tableau
public function read_csv($filename)
{
if(($handle=fopen($filename,'r')) !== FALSE)
{
while (($line=fgetcsv($handle,1024,';')) !== FALSE)
{
if (!(is_null($line)))
{
$data[]=$line;
}
}
}
return $data;
}
}
class Breve
{
private $_id;
private $_date;
private $_city;
private $_period;
private $_summary;
private $_id_summary;
private $_temperature_min;
private $_temperature_max;
private $_comments;
//Constructeur
public function __construct(array $data)
{
$this->setId($data['id']);
$this->setDate($data['date']);
$this->setCity($data['city']);
$this->setPeriod($data['period']);
$this->setSummary($data['summary']);
$this->setId_summary($data['id_summary']);
$this->setTemperature_min($data['temperature_min']);
$this->setTemperature_max($data['temperature_max']);
$this->setComments($data['comments']);
}
//création des setters
public function setId($id)
{
$this->_id = $id;
}
public function setDate($date)
{
return $this->_date = $date;
}
public function setCity($city)
{
$this->_city = $city;
}
public function setPeriod($period)
{
$this->_period = $period;
}
public function setSummary($summary)
{
$this->_summary = $summary;
}
public function setId_summary($id_summary)
{
$this->_id_summary = $id_summary;
}
public function setTemperature_min($temperature_min)
{
$this->_temperature_min = $temperature_min;
}
public function setTemperature_max($temperature_max)
{
$this->_temperature_max = $temperature_max;
}
public function setComments($comments)
{
$this->_comments = $comments;
}
//création des getters
public function getId()
{
return $this->_id;
}
public function getDate()
{
return $this->_date;
}
public function getCity()
{
return $this->_city;
}
public function getPeriod()
{
return $this->_period;
}
public function getSummary()
{
return $this->_summary;
}
public function getId_summary()
{
return $this->_id_summary;
}
public function getTemperature_min()
{
return $this->_temperature_min;
}
public function getTemperature_max()
{
return $this->_temperature_max;
}
public function getComments()
{
return $this->_comments;
}
}
/* Classe pour insérer dans une base de données */
class breveManager
{
private $_db;
public function __construct($db)
{
$this->setDb($db);
}
public function setDb(PDO $dbh)
{
$this->_db = $dbh;
}
public function addBreve(Breve $breve)
{
$sql = 'SELECT COUNT(*) AS nb FROM meteo
WHERE date = "'.$breve->getDate().'" AND ville = "'.$breve->getCity().'" AND period = "'.$breve->getPeriod().'"';
$compt = $this->_db->query($sql);
$donnees = $compt->fetch();
if($donnees['nb'] == 0)
{
$insert = 'INSERT INTO meteo (date,ville,period,summary,id_summary,T_min,T_max,comments)
VALUES (:date, :ville, :period, :summary, :id_summary, :temperature_min, :temperature_max, :comments)';
$stmnt = $this->_db->prepare($insert);
$stmnt->bindParam(':date', $breve->getDate());
$stmnt->bindParam(':ville', $breve->getCity());
$stmnt->bindParam(':period', $breve->getPeriod());
$stmnt->bindParam(':summary', $breve->getSummary());
$stmnt->bindParam(':id_summary', $breve->getId_summary());
$stmnt->bindParam(':temperature_min', $breve->getTemperature_min());
$stmnt->bindParam(':temperature_max', $breve->getTemperature_max());
$stmnt->bindParam(':comments', $breve->getComments());
$stmnt->execute();
$stmnt->closeCursor();
$stmnt = NULL;
}
else
{
$update = 'UPDATE meteo SET date = :date, ville = :ville, period = :period, summary = :summary, id_summary = :id_summary, T_min =
:temperature_min, T_max = :temperature_max, comments = :comments
WHERE date = :date AND ville = :ville AND period = :period';
$stmnt = $this->_db->prepare($update);
$stmnt->bindParam(':date', $breve->getDate());
$stmnt->bindParam(':ville', $breve->getCity());
$stmnt->bindParam(':period', $breve->getPeriod());
$stmnt->bindParam(':summary', $breve->getSummary());
$stmnt->bindParam(':id_summary', $breve->getId_summary());
$stmnt->bindParam(':temperature_min', $breve->getTemperature_min());
$stmnt->bindParam(':temperature_max', $breve->getTemperature_max());
$stmnt->bindParam(':comments', $breve->getComments());
$stmnt->execute();
$stmnt->closeCursor();
$stmnt = NULL;
}
}
public function getBreve($city,$today,$after_tomorrow)
{
if(is_string($city))
{
$sql = 'SELECT * FROM meteo WHERE ville= :city AND date > :today AND date <= :after_tomorrow';
$stmnt = $this->_db->prepare($sql);
$stmnt->bindParam(':city',$city);
$stmnt->bindParam(':today',$today);
$stmnt->bindParam(':after_tomorrow',$after_tomorrow);
$stmnt->execute();
while($row = $stmnt->fetch(PDO::FETCH_ASSOC))
{
$result[] = $row;
}
}
return $result;
}
}
/* Instanciation des classes créées */
$meteo = new Meteo();
$filename = 'http://localhost:8888/devoir_2_php/meteo.csv';
$data = $meteo->read_csv($filename); //data contient les résultats
//$meteo = new Meteo('http://localhost:8888/devoir_2_php/meteo.csv');
echo '<pre>';
print_r($data);
echo '<pre>';
for($i=0;$i < count($data);$i++)
{
$new_content = array('date' => $data[$i][0], 'city' => $data[$i][1], 'period' => $data[$i][2], 'summary' => $data[$i][3], 'id_summary'=> $data[$i][4], 'temperature_min' => $data[$i][5], 'temperature_max' => $data[$i][6], 'comments' => $data[$i][7]);
$breve = new Breve($new_content);
$db = new PDO('mysql:host=localhost;dbname=table','root','root');
$manager = new breveManager($db);
$manager->addBreve($breve);
}
$today = $data[0][0];
$after_tomorrow = $data[6][0];
$ville = $manager->getBreve('Paris',$today,$after_tomorrow);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>PREMIERS PAS AVEC BRACKETS</title>
<meta name="description" content="An interactive getting started guide for Brackets.">
<link rel="stylesheet" href="meteo.css">
</head>
<body>
<div id="gabarit">
<h1> Météo d'une ville </h1>
<?php foreach($ville as $result): ?>
<div class="vignette">
<h2> <?php echo $result['ville'] ;?></h2>
<?php echo date("d-m-Y",strtotime($result['date'])) ;?>, <?php echo $result['period'] ;?>
<p class="summary"><?php echo $result['summary'] ;?></p>
<p> Minimales : <?php echo $result['T_min']. '°C' ;?><br />
Maximales : <?php echo $result['T_max']. '°C' ;?></p>
<p> Détails : <?php echo $result['comments'] ;?></p>
</div>
<?php endforeach ?>
</div>
</body>
</html> |
Partager