Lines numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Lines numbers

Post by HiddenS3crets »

I'm trying to print out source code with a line number next to each line.

This function kind of works, but first off the code goes past the numbers.

This is my source code:

Code: Select all

$lines = explode("\n", $code);
    $numbers = "";
    $final = "";

    for($x = 1; $x <= 100; $x++) {
        $number = $x . "<br \>\n";
        $numbers .= $number;
        $line = $lines[$x - 1] . "<br \>\n";
        $final .= $line;
    }
    
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td width=\"1%\" align=\"right\" valign=\"top\">
$numbers
    </td>
  
    <td width=\"1%\" valign=\"top\">
    </td>

    <td width=\"98%\" align=\"left\" valign=\"top\">
$final
    </td>
  </tr>
</table>";
}
$code being the source code to display.

A live example of this can be viewed here
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

because your loop only runs 100 times.


use a foreach or a while loop instead
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post by HiddenS3crets »

I have it fixed. Thanks
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

because of the way you're outputting it into two columns of a table, put each line in it's own row

Code: Select all

<tr>
<td>line#</td>
<td>line of code</td>

</tr>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

jshpro2 wrote:because of the way you're outputting it into two columns of a table, put each line in it's own row

Code: Select all

<tr>
<td>line#</td>
<td>line of code</td>

</tr>
That would render copy and pasting pretty much useful, I believe.
Line breaks are key.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Jcart wrote:That would render copy and pasting pretty much useful, I believe.
Line breaks are key.
Just fixing his code, not advocating it. Good point however
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

From console

Code: Select all

cat source_file_name.php  | php -R 'echo $argi.":".$argn."\n";' | php -s >  target_file_name.html
Post Reply