/    Sign up×
Community /Pin to ProfileBookmark

automatically include all lines from a text file within HTML

i have this script:

[code=php]
<?php

$lines = file(‘[[URL]]’);
$l_count = count($lines);
for($x = 0; $x< $l_count; $x++)
{
}
echo ‘<b><a href=”‘.$lines[1].'”>’.$lines[2].'</a></b><br />’;
echo ‘<a href=”‘.$lines[3].'”>’.$lines[4].'</a><br />’;
echo ‘<a href=”‘.$lines[5].'”>’.$lines[6].'</a><br />’;
echo ‘<a href=”‘.$lines[7].'”>’.$lines[8].'</a><br />’;
?>
[/code]

I use it to automate 10 headlines, i also use it on an archive page, which requires all the data.

How would i construct a code that detects how many lines there are and replicated the script for each item (as long as the text file is).

(the text file is plain text on different lines, a line is then included)

Thanks for any help.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@MellottNMar 01.2008 — This code is a little bit sloppy, but it gets the job done ?

[code=php]
<?php

$lines = file('youtextfile.txt');
$l_count = count($lines);
$num = 0;
foreach ($lines as $bleh) {
if($num > $l_count){
die();
}
echo "<b><a href="".$lines[$num]."">";
$num++;
echo $lines[$num]."</a></b><br />";
$num++;
}

?>
[/code]


I never messed with the 'foreach' construct until now, so I just messed around with it until it worked. :3

The 'if($num > $l_count)' part just cuts off the loop after it's maxed out of lines because for some reason it would echo about double the amount of lines it really needed.

?
Copy linkTweet thisAlerts:
@danpoultonauthorMar 01.2008 — thanks for your reply.

the code works in concept, but i need to not show the first line, then show line 2 as the href and line 3 as the link text.

how would i go about changing that?
Copy linkTweet thisAlerts:
@danpoultonauthorMar 26.2008 — how would i go about adding an explode to separate text with ||?
Copy linkTweet thisAlerts:
@ZnupiMar 26.2008 — I don't think exploding is a good idea. Here's my go at it:
[code=php]
$lines = file('[[url]]');
for ($i=0; $i < count($lines); $i++) {
if (!$i%2) echo '<a href="' . htmlentities($lines[$i]) . '">';
else echo htmlentities($lines[$i]) . '</a><br />';
}
[/code]

Try this, tell us how it goes ?.
Copy linkTweet thisAlerts:
@danpoultonauthorMar 26.2008 — thanks for you code, not sure how it has gone yet, am a bit confused.

My text file is structured like this:

Title (will go inside <a></a>)

Description (for RSS file (this part i have worked out already))

Page ID (goes in href="" with the start of the link)

So how would i structure the PHP code to have all the titles and links, in normal A tags with a line break.

I think its the numbers part i'm stuck on.

Thanks for your help.
Copy linkTweet thisAlerts:
@ZnupiMar 27.2008 — Does this file contain multiple such entries? If yes:
[code=php]
$lines = file('[[url]]');
for ($i=0; $i < count($lines)-2; $i+=3) {
echo '<a href="' . htmlentities($lines[$i+2]) . '">' . htmlentities($lines[$i]) . '</a><br />' . htmlentities($lines[$i+1]) . '<br />';
}
[/code]

Try this one (untested).
Copy linkTweet thisAlerts:
@danpoultonauthorMar 27.2008 — thanks that code works completely as it should ? .

Can i limit it to show only the top 10 lines of the file, i need this for my homepage, the code works well on my index page.
Copy linkTweet thisAlerts:
@ZnupiMar 27.2008 — thanks that code works completely as it should ? .

Can i limit it to show only the top 10 lines of the file, i need this for my homepage, the code works well on my index page.[/QUOTE]

You mean 10 [b]entries[/b] (1 entry = 3 lines)?
[code=php]
function displayRSS($filename, $limit=false) {
$lines = file($filename);
$limit = (!is_int($limit)) ? count($lines)-2 : $limit*3-2;
for ($i=0; $i < $limit; $i++) {
echo '<a href="' . htmlentities($lines[$i+2]) . '">' . htmlentities($lines[$i]) . '</a><br />' . htmlentities($lines[$i+1]) . '<br />';
}
}
// to display all elements:
displayRSS('[[url]]');
// to display the first $n elements:
displayRSS('[[url]]', $n);
[/code]

Untested. Tell us if it works ?
Copy linkTweet thisAlerts:
@danpoultonauthorMar 27.2008 — http://www.gameslayer.co.uk/blog/

you can see the code there, it doesn't work currently.
Copy linkTweet thisAlerts:
@ZnupiMar 27.2008 — Sorry about that, error in my code. This one [i]should[/i] work:
[code=php]
function displayRSS($filename, $limit=false) {
$lines = file($filename);
$limit = (!is_int($limit)) ? count($lines)-2 : $limit*3-2;
for ($i=0; $i < $limit; $i+=3) {
echo '<a href="' . htmlentities($lines[$i+2]) . '">' . htmlentities($lines[$i]) . '</a><br />' . htmlentities($lines[$i+1]) . '<br />';
}
}
// to display all elements:
displayRSS('[[url]]');
// to display the first $n elements:
displayRSS('[[url]]', $n);
}
[/code]

Tell us how it goes ?
Copy linkTweet thisAlerts:
@danpoultonauthorMar 27.2008 — thanks, script works perfectly ? .
×

Success!

Help @danpoulton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...