Jump to content

Echoing line numbers after array_slice


pea

Recommended Posts

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\">&nbsp;</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]
Link to comment
Share on other sites

Is each slice one line?

If so, it's as simple as an array counter:
[code]<?php
for($i=1;$<count($myarray);$i++)
  echo "Line #<b>$i</b> : " . htmlspecialchars($myarray[$i-1]) . "<br />\n";
?>[/code]

Hope that helps.
Link to comment
Share on other sites

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 one
line 1
line 2
line 3
slice two
line 4
line 5
line 6
}
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.