pea Posted January 3, 2007 Share Posted January 3, 2007 Hello, i've got a text file that's cut up into pieces using array_slice and then each slice is echoed. I need to echo each line number. I've looked at the manual:[code]$lines = file('http://www.example.com/');foreach ($lines as $line_num => $line) { echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";}[/code]but for me $lines isn't a file array. My script is below. Thanks, Peter[code]<?php$page=$_GET['page'];if(!isset($HTTP_GET_VARS['page'])){$page = 1;}$array = file("FlatFort/messagebox.txt");$s=sizeof($array);$show_per_page = 20;$start = ($page * $show_per_page) - $show_per_page;$slice = array_slice($array, $start, $show_per_page);$end=$show_per_page*$page;if ($start != '0') {$new_page=$page-1;$previously="<a href='messagebox.php?page=$new_page'><b>Newer</a></b></font>";}else {$previously="";}if ($end < $s) {$new_page1=$page+1;$next="<a href='messagebox.php?page=$new_page1'><b>Older</a></b></font>";}else {$next="";}echo "<table width=100% align='center'><tr><td width=50%>".$previously."</td><td align=right width=50%><b>".$next."</b></font></td></tr></table><br>";$break="|";foreach ($slice as $thisline) {list($name,$c,$date,$url)=explode($break,$thisline);require("./accounts/{$name}_user.php");echo "<table width=\"95%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td width=\"160\" height=\"16\" valign=\"top\"><img src=\"skins/Default_img/top.gif\" width=\"160\" height=\"17\" /></td> <td width=\"100%\" height=\"17\" background=\"skins/Default_img/repeat.gif\"> </td> <td width=\"160\" height=\"16\" valign=\"top\"><img src=\"skins/Default_img/topright.gif\" width=\"160\" height=\"17\" /></td> </tr> <tr> <td colspan=\"3\" class=\"block\"> <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td width=\"10%\" valign=\"top\" style=\"padding-top:5; padding-left:5\"><span class=\"name\">$name</span></td> <td height=\"20\"><span class=\"date\" style=\"padding-top:5\">$date</span></td> </tr> <tr> <td width=\"10%\" valign=\"top\" style=\"padding:10\"><div align=\"center\"><img src=$avatar alt=\"\"></div></td> <td height=\"100%\" style=\"padding:5; vertical-align:top\"><span class=\"message\">$c</span></td> </tr> </table> </td> </tr> <tr> <td height=\"22\" colspan=\"3\" background=\"skins/Default_img/bottom repeat.gif\"></td> </tr></table>";}echo "<table width=100% align='center'><tr><td width=50%>".$previously."</td><td align=right width=50%><b>".$next."</b></font></td></tr></table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/ Share on other sites More sharing options...
ober Posted January 3, 2007 Share Posted January 3, 2007 Is each slice one line?If so, it's as simple as an array counter:[code]<?phpfor($i=1;$<count($myarray);$i++) echo "Line #<b>$i</b> : " . htmlspecialchars($myarray[$i-1]) . "<br />\n";?>[/code]Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-152244 Share on other sites More sharing options...
pea Posted January 3, 2007 Author Share Posted January 3, 2007 No it's 20. Like 20 lines per page Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-152260 Share on other sites More sharing options...
ober Posted January 3, 2007 Share Posted January 3, 2007 ... ok... that doesn't really help me.Do you want line numbers for each one of those lines within the slice portions... you need to describe the problem better. Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-152264 Share on other sites More sharing options...
pea Posted January 3, 2007 Author Share Posted January 3, 2007 I need line numbers for each line of the whole file, not each slice. The objective is to be able to unlink a line from links on each echoed line, so i need line numbers. I can do the unlinking, just can't figure out how to get the line numbers. Don't know how to explain it more..file{slice oneline 1line 2line 3slice twoline 4line 5line 6} Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-152271 Share on other sites More sharing options...
pea Posted January 10, 2007 Author Share Posted January 10, 2007 Maybe i need to give each line it's number and the slice it. Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-157754 Share on other sites More sharing options...
Barand Posted January 11, 2007 Share Posted January 11, 2007 [code]<?php$a = range (1,20);$perpage = 5;for ($page=0; $page<4; $page++) { $b = array_slice ($a, $page*$perpage, $perpage); echo "<p>Page $page</p>"; foreach ($b as $k => $val) { $line = $page * $perpage + $k + 1; // <--- calculate line number echo "Line $line : $val<br>"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-157838 Share on other sites More sharing options...
pea Posted January 11, 2007 Author Share Posted January 11, 2007 Ok, but how do i show only the first slice? and how to i add an explode thing to assign variables. I have this normally 'list($name,$c,$date,$url)=explode($break,$thisline);'Sorry, arrays just really confuse me Quote Link to comment https://forums.phpfreaks.com/topic/32683-echoing-line-numbers-after-array_slice/#findComment-158332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.