par
telnes » 13 juin 2014, 14:09
1 est ce que tu as compris le code ?
2 est ce que tu as regardé sur
http://www.php.net/ les fonctions qui te paraissent pas claires ?
3 est ce que tu as exécuté ton code ?
ps : la lecture de http://www.php.net/manual/fr/language.v ... basics.php est fortement conseillé
html.tpl
<html>
<head>
<title>{productname} Home Page</title>
<style></style>
</head>
<body>
<img style="width: 100%; height: 100%" src='data:image/png;base64,{productlogo}'/>
<img style="width: 100%; height: 100%" src='data:image/png;base64,{productimage}'/>
<strong>{productname}</strong>
<del>
<font color=#FF0000>
{productoldprice}
</font>
</del>
<strong>{productprice}</strong>
<input type=hidden name=model id=model value='{model}'>
<p>{productdescription} </p>
</body>
</html>
data.csv
productname , productlogo , productimage , productoldprice , productprice , model , productdescription
page 1 , bbbbbbbbbbb1 , cccccccccccc1 , ddddddddddddddd1 , eeeeeeeeeeee1 , fffff1 , gggggggggggggggggg1
page 2 , bbbbbbbbbbb2 , cccccccccccc2 , ddddddddddddddd2 , eeeeeeeeeeee2 , fffff2 , gggggggggggggggggg2
page 3 , bbbbbbbbbbb3 , cccccccccccc3 , ddddddddddddddd3 , eeeeeeeeeeee3 , fffff3 , gggggggggggggggggg3
code.php
<?php
$file = 'data.csv'; //chemin vers le fichier csv
$tpl = 'html.tpl'; //chemin vers le fichier template
$row = 1; //compteur de ligne
$tabData = array(); //tableau des données CSV
$tabTitle = array(); //titre les colonnes
$numPage = (isset($_GET['p']) and $_GET['p']>0)?(int)$_GET['p']:'1'; //numéro de la page récupéré en URL ?p=3 ou 1 sinon
//ouvre le fichier
if (($handle = fopen($file, "r")) !== FALSE) {
// on parcours le fichier csv tant qu'il n'y a plus de ligne
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data); //nombre de champs sur une ligne
//récupère les champs sur une ligne
for ($c=0; $c < $num; $c++) {
if($row == 1){ // titre des colonnes csv
$tabData[trim($data[$c])] = array(); //on créer un tableau avec le titre de la colonne
$tabTitle[$c] = trim($data[$c]);
}
else{
$tabData[$tabTitle[$c]][] = trim($data[$c]); // on met les données dans le tableau
}
}
$row++; // on incrémente la ligne
}
//si $numPage > $row == impossible
if($numPage >= ($row-1)) $numPage = 1;
//récupère le fichier template
$tplFile = file_get_contents($tpl);//récupère le fichier template
$datas = getLine($tabData,$numPage);//récupère les champs de la ligne numPage
//boucle sur les champs
foreach($datas as $title=>$value){
//remplace dans le tpl
$tplFile = str_replace('{'.$title.'}',$value,$tplFile);
}
//écris les autres pages
foreach($tabData['productname'] as $p=>$value){
echo '<a href="?p='.($p+1).'">'.$value.'</a> - '; // lien pas tres catholique !! ??? :/ :)
}
echo $tplFile; //affiche le tpl
}
else{
die("no file to open");
}
//function sinon
function getLine($array,$num){
$ligne = $num-1;
$tmp = array(); //tableau
foreach($array as $title=>$tab){ //pour chaque titre=>valeur du tableau
$tmp[$title]=$tab[$ligne];
}
return $tmp;
}
?>
1 est ce que tu as compris le code ?
2 est ce que tu as regardé sur http://www.php.net/ les fonctions qui te paraissent pas claires ?
3 est ce que tu as exécuté ton code ?
[b]ps : la lecture de http://www.php.net/manual/fr/language.variables.basics.php est fortement conseillé[/b]
html.tpl
[html]
<html>
<head>
<title>{productname} Home Page</title>
<style></style>
</head>
<body>
<img style="width: 100%; height: 100%" src='data:image/png;base64,{productlogo}'/>
<img style="width: 100%; height: 100%" src='data:image/png;base64,{productimage}'/>
<strong>{productname}</strong>
<del>
<font color=#FF0000>
{productoldprice}
</font>
</del>
<strong>{productprice}</strong>
<input type=hidden name=model id=model value='{model}'>
<p>{productdescription} </p>
</body>
</html>
[/html]
data.csv
[html]
productname , productlogo , productimage , productoldprice , productprice , model , productdescription
page 1 , bbbbbbbbbbb1 , cccccccccccc1 , ddddddddddddddd1 , eeeeeeeeeeee1 , fffff1 , gggggggggggggggggg1
page 2 , bbbbbbbbbbb2 , cccccccccccc2 , ddddddddddddddd2 , eeeeeeeeeeee2 , fffff2 , gggggggggggggggggg2
page 3 , bbbbbbbbbbb3 , cccccccccccc3 , ddddddddddddddd3 , eeeeeeeeeeee3 , fffff3 , gggggggggggggggggg3
[/html]
code.php
[php]
<?php
$file = 'data.csv'; //chemin vers le fichier csv
$tpl = 'html.tpl'; //chemin vers le fichier template
$row = 1; //compteur de ligne
$tabData = array(); //tableau des données CSV
$tabTitle = array(); //titre les colonnes
$numPage = (isset($_GET['p']) and $_GET['p']>0)?(int)$_GET['p']:'1'; //numéro de la page récupéré en URL ?p=3 ou 1 sinon
//ouvre le fichier
if (($handle = fopen($file, "r")) !== FALSE) {
// on parcours le fichier csv tant qu'il n'y a plus de ligne
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data); //nombre de champs sur une ligne
//récupère les champs sur une ligne
for ($c=0; $c < $num; $c++) {
if($row == 1){ // titre des colonnes csv
$tabData[trim($data[$c])] = array(); //on créer un tableau avec le titre de la colonne
$tabTitle[$c] = trim($data[$c]);
}
else{
$tabData[$tabTitle[$c]][] = trim($data[$c]); // on met les données dans le tableau
}
}
$row++; // on incrémente la ligne
}
//si $numPage > $row == impossible
if($numPage >= ($row-1)) $numPage = 1;
//récupère le fichier template
$tplFile = file_get_contents($tpl);//récupère le fichier template
$datas = getLine($tabData,$numPage);//récupère les champs de la ligne numPage
//boucle sur les champs
foreach($datas as $title=>$value){
//remplace dans le tpl
$tplFile = str_replace('{'.$title.'}',$value,$tplFile);
}
//écris les autres pages
foreach($tabData['productname'] as $p=>$value){
echo '<a href="?p='.($p+1).'">'.$value.'</a> - '; // lien pas tres catholique !! ??? :/ :)
}
echo $tplFile; //affiche le tpl
}
else{
die("no file to open");
}
//function sinon
function getLine($array,$num){
$ligne = $num-1;
$tmp = array(); //tableau
foreach($array as $title=>$tab){ //pour chaque titre=>valeur du tableau
$tmp[$title]=$tab[$ligne];
}
return $tmp;
}
?>
[/php]