• 0

Need some php-scripting help...


Question

Ok I have been working on the code for some hours now, and i have to say "IT'S BOTHERING ME A LOT" and I need some help from you php masters out there....

<?
$fil = "something.txt";

print "<p><SELECT NAME=\"1\" SIZE=\"21\" MULTIPLE>
<OPTION>        </OPTION>";

$fp = @fopen($fil, "r") or die("Error");
$indhold = @fread($fp, filesize($fil)); 
$split = @split( "\n", $indhold); 

    for ($x = count($split)-1; $x > 0; $x--) {

        while($str = fgets($fp, 1024)) {
              $linje = @split( "\|", $str);

$linje[1] = ereg_replace( "\n", "", $linje[1]);

    print "<OPTION>$x, $linje[0], $linje[1], $linje[2], $linje[3]</OPTION>\n"; 
} }

   print "</SELECT></p>";
?>

Paste this in the "something.txt"

this space for TOPIC|this space for TEXT|this space for TIME|and this space for File
this space for TOPIC|this space for TEXT|this space for TIME|and this space for File
this space for TOPIC|this space for TEXT|this space for TIME|and this space for File
this space for TOPIC|this space for TEXT|this space for TIME|and this space for File
this space for TOPIC|this space for TEXT|this space for TIME|and this space for File

Ok the code works, but i keep getting the same number on each line.

I don't know whats wrong with the code, so some help from you php-guru would help.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You are getting the same number on each line because the value of $x does not decrease in the while loop, only in the for loop. It is only running the for loop once but it is running the while loop for each line in the something.txt file. If you want it to run the for loop each time, you need to fseek($fp, 0) after each while to reset the pointer to the beginning each time. Also note that i needed to add a fseek($fp, 0) after the $split = @split("\n", $indhold); just to get your code to run correctly.

hope this helps.

Link to comment
Share on other sites

  • 0

Try this instead, it's cleaner, and probably faster:

<?
$name = 'something.txt';

if( $file = file ( $name );  )
{

  print '<p><SELECT NAME="1" SIZE="21" MULTIPLE>
           <OPTION>        </OPTION>';

  $x = count( $file );
  foreach( $file as $line )
  {
    $items = explode( '|', $line );
    print "<OPTION>$x, $items[0], $items[1], $items[2], $items[3]</OPTION>\n";
    $x--;
  }

  print "</SELECT></p>";

} else { die( 'Error!' ); }
?>

BTW, you can't have newlines in the 2nd spot ($linje[1] or $item[1]), cause you already split them out in the $split = @split( "\n", $indhold); line....

Oh yeah, no gaurantee this works :p

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.