Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2011, 05:55 PM   PM User | #1
LegoTrip
New Coder

 
Join Date: Apr 2011
Posts: 25
Thanks: 4
Thanked 0 Times in 0 Posts
LegoTrip is an unknown quantity at this point
Question Foreach Echo Text Document, Target Styles By Line

Hey guys! I need a little help with something... but please keep in mind I am a PHPN00B so pls don't hit me in the face

Basically, I have a text document named headline.txt which contains the following lines:

Lilly Creightmore

Homemaker, photographer, blogger.

Crystal Palace, London, United Kingdom, January 2011


Then I have pulled that out using the following code:

Code:
<?php 		
   $text_file1 = glob('headline.txt');
?>
<?php foreach ($text_file1 as $key => $file1) : ?>		
   <?php echo nl2br(file_get_contents($file1)); ?>
   <br />
   <br />
<?php endforeach; ?>
This is all fine and well, works perfectly, as you can see in the div with the class "luckypeopletop" over here: http://we-are-lucky.com/stage/666/Th...Ben/index2.php

However, is it possible to target each line with a separate style class? For example, one style for Lilly Creightmore, a different one for Homemaker, photographer, blogger. etc.

Ideally, it would eventually look like this: http://we-are-lucky.com/stage/666/The-Lucky/Ben/

Does this even make sense? Regardless, thanks for reading
LegoTrip is offline   Reply With Quote
Old 10-03-2011, 07:20 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Can your text file look like this, where you insert HTML/CSS into the actual text file?

The first line looks at your CSS style sheet for the class called "topLine".
The next line uses "inline CSS" to define it right there in the line.

Code:
<div class="topLine">Lilly Creightmore</div><br />
<br />
<span style="color:#a00000";>Homemaker, photographer, blogger.</span><br />
<br />
Crystal Palace, London, United Kingdom, January 2011<br />
Wherever you want that text file to appear, you include it ...

<html>
<style>
.topLine{
font-family:arial;
color:#00a000;
}
</style>
<body>
Hi there, this is my site <br /><br />

<?php include("headline.txt");?>

</body>
</html>


.

Last edited by mlseim; 10-03-2011 at 07:25 PM..
mlseim is offline   Reply With Quote
Old 10-03-2011, 07:36 PM   PM User | #3
LegoTrip
New Coder

 
Join Date: Apr 2011
Posts: 25
Thanks: 4
Thanked 0 Times in 0 Posts
LegoTrip is an unknown quantity at this point
That is a very good suggestion, but the main reason I am approaching things this way is so that my client can add information just by writing a text file. I am worried adding styles into the text document would just confuse them :/

If that is the only way, so be it, but I was hoping someone would have some fancy idea I could implement.

Thanks for the reply tho mlseim!
LegoTrip is offline   Reply With Quote
Old 10-03-2011, 08:14 PM   PM User | #4
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
There may be a fancier way, but here's one solution:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<style type="text/css">
.s1 {color:#F00}
.s2 {color:#0F0}
.s3 {color:#00F}
</style>
</head>

<body>

<p>    
<?php
    $fp 
fopen ('headline.txt','r');
    echo 
"<span class='s1'>".fgets ($fp)."<br></span>";
    echo 
"<span class='s2'>".fgets ($fp)."<br></span>";
    echo 
"<span class='s3'>".fgets ($fp)."<br></span>";
?>
</p>
</body>
</html>
tracknut is offline   Reply With Quote
Users who have thanked tracknut for this post:
LegoTrip (10-04-2011)
Old 10-03-2011, 09:46 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Is the information structured? Or will it be sort of free-form?
mlseim is offline   Reply With Quote
Old 10-03-2011, 10:08 PM   PM User | #6
JV-
New Coder

 
Join Date: Oct 2011
Posts: 19
Thanks: 0
Thanked 3 Times in 3 Posts
JV- has a little shameless behaviour in the past
If you could have the client edit a CSV file or XML file instead, you could easily manipulate the data with PHP and apply styles to different areas.

You could also do the same with a fixed length text file, but that may be too confusing for the client.

Answering mlseim's question will help to determine the best solution.
JV- is offline   Reply With Quote
Old 10-03-2011, 11:07 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I'm steering toward the idea that the OP doesn't use MySQL. So, they can also
avoid using .txt files as well. They can use Google Docs (spreadsheet). Allow certain
Google accounts to access the spreadsheet ... and it's all free. A PHP script can
easily read the data and process it, just like a database.
mlseim is offline   Reply With Quote
Old 10-04-2011, 04:31 AM   PM User | #8
coin
New Coder

 
Join Date: Sep 2011
Posts: 24
Thanks: 1
Thanked 3 Times in 3 Posts
coin is an unknown quantity at this point
Maybe something like this?
PHP Code:

$classes 
= array('class1''class2''class3');
$file file_get_contents($filename);
$current 0;

foreach(
$file as $line) {
     echo 
'<span class="'.$classes[$current].'">'.$line.'</div>';
     
$current++;

coin is offline   Reply With Quote
Old 10-04-2011, 08:59 AM   PM User | #9
LegoTrip
New Coder

 
Join Date: Apr 2011
Posts: 25
Thanks: 4
Thanked 0 Times in 0 Posts
LegoTrip is an unknown quantity at this point
Wow, you guys are great, thanks so much for all your help.

I went with tracknut's suggestion, and it worked perfectly. That's some sexy php ninja skills right there buddy, thanks a million! Exactly what I was asking.

coin, your suggestion seems really cool too, I will definitely look into using arrays in the future too.

All of you are awesome, seriously, thanks so much!!!!
LegoTrip is offline   Reply With Quote
Reply

Bookmarks

Tags
document, file_get_contents, foreach, php, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:27 PM.


Advertisement
Log in to turn off these ads.