By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,826 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,826 IT Pros & Developers. It's quick & easy.

Create a table regarding a text file

P: n/a
Hello,
I have this .txt file :

Roger|tow25$rank259
Isabelle|tow36$rank24
Pascal|tow12$rank29
Sergeï|tow45$rank5
Michel|tow1245$rank45478
Frédéric|tow1$rank125425

And this programm php3
<?php
$fichier = "classeur.txt";
if($fp = fopen($fichier,"r")){
$ligne=1;
echo "<table border=1 bordercolor=\"#00CCFF\" width=500>\n";
echo "<tr align=center><td colspan=3>TITRE</td>";
while (!feof($fp)) {
list( $name, $tampon ) = explode( "|tow", $fp );
list( $tow, $obj ) = explode ( "$rank", $tampon );
echo "\t<tr>";
echo "<td
align=center><b>Nom".$name."</b></td><td>Tow".$tow."</td><td>Obj".$obj."</td
";

echo "</tr>\n";
$ligne++;
echo "</table>\n";
echo "$cell";
fclose($fp);
}else{
echo "Error : open impossible ".$fichier;
exit();
}
?>

I would like to past each value of the lines in the tex file, in 3 variables
$nom; $tow; $obj
and create a table with 3 column ( column 1 the name, in 2 the tow, and in 3
the obj)

But htis not work
There must be an error, but i don't know where.

Thanks by advance
--
**********************
SOCARA S.A.
Strasbourg
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Fredo wrote:
list( $tow, $obj ) = explode ( "$rank", $tampon );


if $rank is undefined "$rank" is parsed by php to ''.
if $rank is "defined" "$rank" is parsed by php to 'defined'.

Either use single-quotes
explode ( '$rank', $tampon );

or escape the dollar sign
explode ( "\$rank", $tampon );
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2

P: n/a
It's not really where the mess is.
In fact my table is created but i have those message :
NomResource id #1 Tow Obj
NomResource id #1 Tow Obj
As i should have
roger Tow25 Obj259
isabelle Tow36 Obj24
My table is created but is empty.
And the prog didn't manage to find the end of the text file as php create a
non-ending table
.......
"Pedro" <he****@hotpop.com> a écrit dans le message de
news:bo*************@ID-203069.news.uni-berlin.de...
Fredo wrote:
list( $tow, $obj ) = explode ( "$rank", $tampon );


if $rank is undefined "$rank" is parsed by php to ''.
if $rank is "defined" "$rank" is parsed by php to 'defined'.

Either use single-quotes
explode ( '$rank', $tampon );

or escape the dollar sign
explode ( "\$rank", $tampon );
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.

Jul 17 '05 #3

P: n/a
"Fredo" <(oter-ceci)@fre.fr> wrote:
It's not really where the mess is.
In fact my table is created but i have those message :
NomResource id #1 Tow Obj
NomResource id #1 Tow Obj


Indent your code!
I only noticed you lacked a } after *I* indented your code.
<?php
$fichier = 'classeur.txt';
if($fp = fopen($fichier, 'r')) {
$ligne = 1;
echo '<table border="1" bordercolor="#00CCFF" width="500">';
echo '<tr align="center"><td colspan="3">TITRE</td>';
while (!feof($fp)) {

### YOU NEED THIS!!
$data = fgets($fp);

### change the explode parameter
list ($name, $tampon) = explode('|tow', $data);

list ($tow, $obj) = explode('$rank', $tampon);
echo '<tr>';
echo '<td align="center"><b>Nom: ', $name, '</b></td>';
echo '<td>Tow ', $tow, '</td>';
echo '<td>Obj ', $obj, '</td>';
echo '</tr>';
$ligne++;

###
### YOU NEED THIS!!
###
}

echo '</table>';

### what's this???
echo "$cell";

fclose($fp);
} else {
exit('Error : open impossible ' . $fichier);
}
?>
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #4

P: n/a
Thank you a lot, you have helped me to make a biggggg step.

Now, will it be possible to sort the obtained table ? (i would like to sort
it using the TOW key)

Again thank you ..
Jul 17 '05 #5

P: n/a
Fredo wrote:
Now, will it be possible to sort the obtained table ? (i would like to sort
it using the TOW key)


For that I'd _first_ get the file contents into an array,
then sort the array, and only after all this output it

<?php
function cmp($a, $b) {
if ($a[1] == $b[1]) return 0;
return ($a[1] < $b[1]) ? -1 : 1;
}

$fichier = 'classeur.txt';
preg_match_all('/^(.+)\|tow(.+)\$rank(.+)$/im', implode('', file($fichier)), $data);
foreach ($data[0] as $k=>$v) {
// reorganize $data into $arr
$arr[] = array($data[1][$k], $data[2][$k], $data[3][$k]);
}
unset($data); // not needed anymore
usort($arr, 'cmp'); // sort by TOW

// output
echo "<table>\n";
foreach ($arr as $x) {
echo "<tr><td>";
echo implode('</td><td>', $x);
echo "</td></tr>\n";
}
echo "</table>\n";
unset($arr);
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #6

P: n/a
Again it works fine..
Now, all we have done was about an example.
The real file i have to wirk with contains those lines

PlayerName=-={P.A.G}=-Barbichette's&GameAndMod=CSports.net rank in Medal Of
Honor: Tug of War&Rank=26&NameID=342676489

And i have adapted your prog to this,
<?php
$fichier = 'rang.txt';
if($fp = fopen($fichier, 'r')) {
$ligne = 1;
echo '<table border="1" bordercolor="#00CCFF" width="500">';
echo '<tr align="center"><td colspan="3">TITRE</td>';
while (!feof($fp)) {
$data = fgets($fp);
list ($name, $tamp) = explode('&Game', $data);
list ($bid, $tamp2) = explode('&Rank=', $tamp);
list ($tow, $tamp3) = explode('&Name', $tamp2);
echo '<tr>';
echo '<td align="center"><b>'. $name. '</b></td>';
echo '<td>'. $tow. '</td>';
echo '</tr>';
$ligne++;
}
echo '</table>';
fclose($fp);
} else {
exit('Error : open impossible ' . $fichier);
}
?>

You can see that in my table i only use $name and $tow
now how should i modified my prog to make a sort by tow number.
I have tried to apply your aray tip, but didn't manage... lol
Jul 17 '05 #7

P: n/a
Fredo wrote:
Again it works fine..
Now, all we have done was about an example.
The real file i have to wirk with contains those lines

PlayerName=-={P.A.G}=-Barbichette's&GameAndMod=CSports.net rank in Medal Of
Honor: Tug of War&Rank=26&NameID=342676489

And i have adapted your prog to this,
<?php
[previous, but corrected, version snipped]
You can see that in my table i only use $name and $tow
now how should i modified my prog to make a sort by tow number.
To sort by tow you have to have all data available.
So you can't echo it line by line as you read the file.
I have tried to apply your aray tip, but didn't manage... lol


What did you try?
What errors did it generate?
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.