handling files

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
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

handling files

Post by maciek4 »

I want to write the content of 2 fields to a file and then display it on another page in seperate lines

Code: Select all

$data = trim($name."\n".$desc."\n");
fwrite($fp, $dane);
and the result is that its in 1 line. How to make it display in 2 lines?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Why do you add the last \n if you trim the string? (and thus remove that last \n).

Code: Select all

$line = trim($name) . "\n" . trim($desc) . "\n";
Offcourse, when you're on windows you'll need \r\n instead. (And why're going to display this in html, you'll need nl2br if you want the same "layout")
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

timvw wrote:Why do you add the last \n if you trim the string? (and thus remove that last \n).

Code: Select all

$line = trim($name) . "\n" . trim($desc) . "\n";
Offcourse, when you're on windows you'll need \r\n instead. (And why're going to display this in html, you'll need nl2br if you want the same "layout")
Something wrong with this :#

Code: Select all

$line = trim($name) . "\n" . trim($desc) . "\n";
Can you write it correctly?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I wrote it correctly... But the syntax highlighter messed it up...

I hope it's clear that the two broken parts are:

Code: Select all

"\n"
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

ok it worked. How do I increment by 2

i+2?
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

Ok it worked.


I want afret every 2 lines to add link

so 2 lines of text then link below

another 2 lines of text the same link below etc
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

Code: Select all

for ($i=1;$i<count($lines);$i+=2)  
{
   echo nl2br($lines[$i]) . "<br /><br />\n";
   for ($i=2;$i<count($lines);$i+=2)
   {
   echo nl2br($lines[$i]) . "<br /><br />\n";
   echo '<a href="'.$_SERVER['REQUEST_URI'].'">Delete</a>' ;
   echo '<a href="commit.php?edit" name="edit">edit</a>'. "<br /><br />\n";
 }
 }


almost works.

I want it to look like this:

Product A
Description A
links

Product B
Description B
links

Product C
Descritpion C
links

etc

It gives me

Product A
Description A

links

Description B

links

Description C

links

etc

so I'm missig Product names from 3 line on.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

maciek4 wrote:

Code: Select all

echo nl2br($lines[$i]) . "<br /><br />\n";
would output:

the product<br/>
description<br/>
<br/>
<br/>

See what you need to change?
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

timvw wrote:
maciek4 wrote:

Code: Select all

echo nl2br($lines[$i]) . "<br /><br />\n";
would output:

the product<br/>
description<br/>
<br/>
<br/>

See what you need to change?
Nope. I still cant get Product names.
maciek4
Forum Commoner
Posts: 29
Joined: Tue Apr 04, 2006 8:17 am

Post by maciek4 »

There is a problem with the loop not <br/>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Where is $lines coming from?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Your two loops both modify $i.

Code: Select all

for($i = 0; $i < count($lines); $i += 2) {
  //output $lines[$i] -> product
  // output $lines[$i+1] -> description
  // output link
}
Post Reply