line counter

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

TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

gurjit wrote:does no one have an answer to this problem. i've been at it all day and cant figure it out. please someone HELP.
I have to admit, I'm with patrikG on this one. You've got all the PHP tools in front of you. You can't dive into a project like this without a reasonable knowledge of HTML and PHP.

Rather than trying to tackle the whole deal at once, try to get something working. Do you have a letter that you know fits on one page? Then get that one working. Then see what happens when you run a longer letter into it. As you play with it, the answers will come to you. And more importantly, the questions will come.

"How do I handle [this specific problem]?" is more likely to get an answer than "How do I code my whole project?"
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

come on guys i have tried all day. i've been on this 9 hours and rite now i'm totally brain dead. i tried wordwrap function but when displaying on clientside it dont necessarily mean that the character int will fit the same as the table width.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

The trouble with your question is that the number of lines is totally dependant on the browser. If for instance you have not defined a point size then the number of characters fitting in width 670 can vary, also if you do not use a monospace font (Courier) then the number of characters per line can also change (a line of W's fill up a line far quicker than a line of i's.

If your requirement is even possible, you need to get a piece of code that calculates things using the browser settings which is outside the scope of PHP and is more client side based (as mentioned by patrikG). As such you need to probably go outside this php based forum into a more expansive client side based forum (e.g Javascript) to get the answer you want. Your requirement is far more complex than the normal javascript requirements discussed within this forum and I would not even know where to begin.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

i would set the font size in the table through a style sheet. so the table size and text would be the same.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

gurjit wrote:come on guys i have tried all day. i've been on this 9 hours and rite now i'm totally brain dead. i tried wordwrap function but when displaying on clientside it dont necessarily mean that the character int will fit the same as the table width.
I guess the hangup is that you're not really asking a question. "C'mon guys, my project is coming due and I don't know what to do! Help me!" isn't anything we can helpfully respond to.

Your task, if I understand it, is to output an HTML page that has a header with your school's logo, the contents of a letter, and a footer that has some school-related text in it.

So do that. Don't worry about word wrap and what-if-it-spills-onto-another-page and all that. Just get that part working.

Then tweak it. I think you're hung up on HOW you're going to do it and it's keeping you from actually DOING anything.

Tables are simple, use tables:

Code: Select all

<html>
<!-- an 8 1/2 x 11 page is about 900 pixels high, tweak to taste-->
<!-- header -->
<table style="height:150px"><tr><td>
  <img src="schoolLogo.jpg">
</td></tr></table>

<!-- letter area -->
<table style="height:600px"><tr><td valign="top">
Hello new student,<br />
You're about to embark on an exciting educational adventure! Congratulations! (Now pay up.)
</td></tr></table> 

<!-- footer -->
<table style="height:150px"><tr><td>
The Bentinel School Of Satire<br />
Open 24 hrs, 8-5 Mondays-Thursday
</td></tr></table>
</html>
You can do it with CSS or who knows what else, but tables are easy and well supported and pretty straightforward, so I'd go that route. Your PHP code will of course need to populate the letter area.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

A somewhat rough solution could be: have a fixed font-size, e.g. 12px, (as you suggested above) then estimate how many characters there are per line in your table with fixed width.
Once you've come up with a number, use wordwrap and use $lines=explode("\n",$myStringFromTheTableWithTheFixedWidth). Do count($lines) and Bob's your uncle.

Edit: must... post... faster... ;)
Last edited by patrikG on Thu Mar 18, 2004 11:07 am, edited 1 time in total.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

sorry for all the trouble.

lets say the letter area went onto page 2 then how can i make the header part print again?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

General suggestion (very much like TheBentinel.com's advise at the top of the page): structure your problem. Break it into little sub-problems. Then start solving them one by one.
Jumping from one problem to the next will only confuse you more.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

gurjit wrote:sorry for all the trouble.

lets say the letter area went onto page 2 then how can i make the header part print again?
Dude, I'm sorry too. You're taking a pasting here and you're just trying to get your job done. I've been right where you are.

Without some SERIOUS CSS or javascript code, you aren't going to just pour the text into the letter and have the client figure out when to break to another page, complete with headers and footers. CSS (Cascading Style Sheets) can probably do it, but I don't know how and what experience I have with it says that it's very hard to get and keep working.

Look, you need to get something working. You'd be amazed how actually seeing the text screwed up on the page will help you see how to fix it. Go ahead and make it work right when the letter is only one page.

And the others are absolutely right, make sure you use a style that keeps your font's at a precise size, I recommend in pixels:
<font style="font-size:12px">here's the letter text</font>

BUT, please, go write something. Don't get hung up on knowing how you'll handle every problem that can come up. Get some code up and running, then you'll see what you need to do.

Answers are like dead baseball players: If you build it, they will come.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

thanks TheBentinel.com and all that replied.

i will continue to try.
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post by sebnewyork »

I agree with Patrick, this should be a client side (i.e dhtml and JavaScript) issue:
Counting the number of lines or number of characters that will fit within say 350 px wide by 400 px height is just impossible if you don't predefine the FONT, FONT SIZE, LINE HEIGHT, TEXT ALIGNMENT, PADDING, MARGIN, BORDER, and other html/css layout variables.
Post Reply