Create a table regarding a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredo

    Create a table regarding a text file

    Hello,
    I have this .txt file :

    Roger|tow25$ran k259
    Isabelle|tow36$ rank24
    Pascal|tow12$ra nk29
    Sergeï|tow45$ra nk5
    Michel|tow1245$ rank45478
    Frédéric|tow1$r ank125425

    And this programm php3
    <?php
    $fichier = "classeur.t xt";
    if($fp = fopen($fichier, "r")){
    $ligne=1;
    echo "<table border=1 bordercolor=\"# 00CCFF\" width=500>\n";
    echo "<tr align=center><t d 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".$to w."</td><td>Obj".$ob j."</td[color=blue]
    >";[/color]
    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


  • Pedro

    #2
    Re: Create a table regarding a text file

    Fredo wrote:[color=blue]
    > list( $tow, $obj ) = explode ( "$rank", $tampon );[/color]

    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.

    Comment

    • Fredo

      #3
      Re: Create a table regarding a text file

      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" <hexkid@hotpop. com> a écrit dans le message de
      news:bo8jot$1bf fdj$1@ID-203069.news.uni-berlin.de...[color=blue]
      > Fredo wrote:[color=green]
      > > list( $tow, $obj ) = explode ( "$rank", $tampon );[/color]
      >
      > 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.[/color]


      Comment

      • Pedro

        #4
        Re: Create a table regarding a text file

        "Fredo" <(oter-ceci)@fre.fr> wrote:[color=blue]
        >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
        >[/color]

        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="#0 0CCFF" width="500">';
        echo '<tr align="center"> <td colspan="3">TIT RE</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.

        Comment

        • Fredo

          #5
          Re: Create a table regarding a text file

          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 ..


          Comment

          • Pedro

            #6
            Re: Create a table regarding a text file

            Fredo wrote:[color=blue]
            > Now, will it be possible to sort the obtained table ? (i would like to sort
            > it using the TOW key)[/color]

            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.

            Comment

            • Fredo

              #7
              Re: Create a table regarding a text file

              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&G ameAndMod=CSpor ts.net rank in Medal Of
              Honor: Tug of War&Rank=26&Nam eID=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="#0 0CCFF" width="500">';
              echo '<tr align="center"> <td colspan="3">TIT RE</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


              Comment

              • Pedro

                #8
                Re: Create a table regarding a text file

                Fredo wrote:[color=blue]
                > 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&G ameAndMod=CSpor ts.net rank in Medal Of
                > Honor: Tug of War&Rank=26&Nam eID=342676489
                >
                > And i have adapted your prog to this,
                ><?php[/color]

                [previous, but corrected, version snipped]
                [color=blue]
                > 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.[/color]

                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.
                [color=blue]
                > I have tried to apply your aray tip, but didn't manage... lol[/color]

                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.

                Comment

                Working...