Ok so basically I am pulling a bunch of urls from a file, each on their own new line, now I have come to something I want to do. <?php $newestdisplay = 25; //count the entrys while ($counter < $newestdisplay) { $counter=$counter+1; //echos out the 25 recently added urls / listings echo "<a href='goto.php?id=...?...' target='_blank'>".$file[$counter] . "</a><br/>"; } ?> PHP: Basically this echos out the 25 recent entries to the file.. But I want to get the line number of them and add it to the ?id=...?... if anyone can help I would love it. thanks, Brett
Does it start reading at the top of the file by any chance? If so that would be simple. Just use your $counter for line number and id. If not this will help http://php.about.com/od/advancedphp/ss/php_read_file_5.htm
Omg I feel like a retard... wow... o wow..stupid me i feel stupid, that was very easy to much time looking at script.. gotta start making some coffee.
Issue resolved and all that but just here to teach you something new (maybe): you can do $counter++; instead of $counter=$counter+1; Shorter and does the same thing. Also.. since you didn't define $counter at first, it's obviously starting at 0, and you're adding 1 to it before it gets to its first line, which means you're skipping the very first element of the array (since arrays start with element 0 and not 1). If you actually have something in the element 0 of the array, you should do the $counter increase after the echo.
Sorry if this is called bumping but I do have another problem, so maybe a new post would suit, so I will call this a new post. so what if i wanted to select a specific entry, say line number 5, how would I go about this, I have been looking at this code for to long already I know it $counter= 0; foreach($file as $row) { $line = explode("|", $row); echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>'; $counter++; } PHP:
Try: <?php $want = 5; $counter= 0; foreach($file as $row) { if ($counter == $want){ $line = explode("|", $row); echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>'; break; } $counter++; } ?> PHP: Edit: There's a better way if you want it. I thought it would be better this way so you can see how your code could be modified. Jay
Exactly what I was looking for, I was almost there, already had the if statement going just couldn't piece the rest together, thanks Jay
Very welcome The other method is a lot shorter, and will execute quicker (by a few microseconds, anyway): <?php $want = 5; $line = explode("|", $row[$want]); echo '<div><a href="'.$counter[$e].'">'.$line[0].'</a></div>'; ?> PHP: That should give you the general idea. Jay