Affichage code html dans du php

Résolu
abdinfo6 Messages postés 36 Date d'inscription mercredi 17 novembre 2010 Statut Membre Dernière intervention 18 avril 2011 - 29 nov. 2010 à 11:52
abdinfo6 Messages postés 36 Date d'inscription mercredi 17 novembre 2010 Statut Membre Dernière intervention 18 avril 2011 - 29 nov. 2010 à 16:21
Bonjour,

je galère sur mon code php. J'essaye d'afficher un fichier texte qui se compose ainsi livre|auteur|prix||. J'ai donc par
exemple :

livre A|auteur C|30||
livre B|auteur D|100||
livre D|auteur B|20||
livre B|auteur A|150||
livre G|auteur I|15||
livre A|auteur G|40||
livre C|auteur C|30||

J'aimerais affiche par auteur le nom du livre ainsi que son prix :
auteur C
livre A 30
livre C 30

auteur D
livre B 100 ...

J'ai mon code suivant qui me génere du grand n'importe quoi sur ma page php :


<html>
<head>
<meta http-equiv= "content-type" content="text/html; charset=utf-8">
</head>




 Activités facturables 


Juillet 2010 &#8211; Septembre 2010



<col width =350> <col width= 100> <col width=100>

----

 
Activité  |

Coûts h/j |

Total h/j |

<?php		$recap  = $txt.\"/\".'recap.txt';
$tab_recap = file(\"$recap\");
$nb=count($tab_recap);

$i = 0;
$j = 0;
$z = 0;
$tps_cli = 0;
$tps_act = 0;
$tps_total = 0;

while ($i < $nb)
{

$tab_i = explode ('|', $tab_recap[$i]);
if ($tab_i[3] != 'x')
{
$a = $tab_i[1];
?>

----

<?php echo $a ?>

<?php
while ($j < $nb)
{
$tab_j = explode ('|', $tab_recap[$j]);
if ($activite == $tab_j[1] && $tab_j[3] != 'x')
{
if ($a == $tab_j[1])
{
$c = $tab_j[0];
$tps_cli = $tab_j[2];
$z = $j+1;

while ($z < $nb)
{

$tab_z = explode ('|', $tab_recap[$z]);
if ($tab[4] != 'x')
{if ($a $tab_z[1] && $c $tab_z[0])
{
$tps_cli = $tps_cli + $tab_z[2];
}
}
$z++;
}
?>
<ol type=A>
<li><?php echo $c ?>

</ol>,
<ol type=A>
<li><?php echo $tps_cli ?>

</ol>,
<?php
$tps_act = $tps_act + $tps_cli;
}
}
$j++;
}

?>
<?php $tps_act ?>,

<?php
$tps_act = 0;
}
$i++;
$j = $i;
}
?>

----

Total,
32,8





</html>



Merci de m'aider
abdinfo6

7 réponses

cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
29 nov. 2010 à 13:51
voila j'ai du rajouté deux variables
pourque ça tourne


<html>
<head>
<meta http-equiv= "content-type" content="text/html; charset=utf-8">
</head>




 Activités facturables 


Juillet 2010 &#8211; Septembre 2010



<col width =350> <col width= 100> <col width=100>

----

 
Activité  |

Coûts h/j |

Total h/j |

<?php		$recap  = 'recap.txt';
// $recap = $txt.\"/\".'recap.txt';

echo  $tab_recap = file(\"$recap\");

foreach($tab_recap as $line) {
echo $line.'
';
}

$nb=count($tab_recap);

$i = 0;
$j = 0;
$z = 0;
$tps_cli = 0;
$tps_act = 0;
$tps_total = 0;
$activite=0;
$tab=0;

while ($i < $nb)
{

$tab_i = explode ('|', $tab_recap[$i]);
if ($tab_i[3] != 'x')
{
$a = $tab_i[1];
?>

----

<?php echo $a ?>

<?php

while ($j < $nb)
{
$tab_j = explode ('|', $tab_recap[$j]);
if ($activite == $tab_j[1] && $tab_j[3] != 'x')
{
if ($a == $tab_j[1])
{
$c = $tab_j[0];
$tps_cli = $tab_j[2];
$z = $j+1;

while ($z < $nb)
{

$tab_z = explode ('|', $tab_recap[$z]);
if ($tab[4] != 'x')
{if ($a $tab_z[1] && $c $tab_z[0])
{
$tps_cli = $tps_cli + $tab_z[2];
}
}
$z++;
}
?>
<ol type=A>
<li><?php echo $c ?>

</ol>,
<ol type=A>
<li><?php echo $tps_cli ?>

</ol>,
<?php
$tps_act = $tps_act + $tps_cli;
}
}
$j++;
}

?>
<?php $tps_act ?>,

<?php
$tps_act = 0;
}
$i++;
$j = $i;
}
?>

----

Total,
32,8





</html>

3
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
29 nov. 2010 à 12:42
bonjour

pourquoi pas utiliser mysql ou sqlite
http://www.php.net/manual/en/function.sqlite-array-query.php
je comprend pas l'utilisation d'un fichier texte.

a++
0
abdinfo6 Messages postés 36 Date d'inscription mercredi 17 novembre 2010 Statut Membre Dernière intervention 18 avril 2011
29 nov. 2010 à 13:10
Tout simplement car je n'ai pas beaucoup de lignes à chaque fois !! Donc inutile d'utiliser une BDD Mysql.


abdinfo6
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
29 nov. 2010 à 13:25
re

a mon avis le format csv est plus pratique
mais aussi basique que le txt
au niveau manipulation open() write() close()

puis tu as des fonctions dédies au csv
http://php.net/manual/en/function.fgetcsv.php
http://www.php.net/manual/en/function.fputcsv.php
la fonction sort();

http://www.developpez.net/forums/d624485/php/langage/fichiers/csv-tri-donnees-fichier-csv/

pourquoi reinventer la roue

a++
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
abdinfo6 Messages postés 36 Date d'inscription mercredi 17 novembre 2010 Statut Membre Dernière intervention 18 avril 2011
29 nov. 2010 à 13:27
Que j'utilise un fichier texte ou un fichier texte ne change rien dans l'exécution du code php ...
j'ai un probleme lorsqu'un auteur a écrit plusieurs livres, il m'affiche les livres en dehors du tableau alors que j'ai
une boucle while pour les afficher dans le tableau ...


abdinfo6
0
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
29 nov. 2010 à 13:44
Notice: Undefined variable: activite in C:\wamp\www\php6\abinfo6.php on line 70

j'ai une variable qui est pas définie
0
abdinfo6 Messages postés 36 Date d'inscription mercredi 17 novembre 2010 Statut Membre Dernière intervention 18 avril 2011
29 nov. 2010 à 16:21
Merci, j'ai modifié quelques éléments dans ton code et cela fait a peu près ce que je veux.

Le seul problème est que j'ai une page : form.php
Une seconde : traitement.php (page qui alimente un fichier texte)
Une troisieme : affichage.php (page qui affiche le contenu du fichier texte)

Je ne sais pas comment faire pour dire que lorsque je clique sur un bouton de ma page form.php, il me fasse le traitement de donnée (traitement.php) et qu'il m'affiche le résultat (affichage.php) en même temps ...

Merci


abdinfo6
0
Rejoignez-nous