View Full Version : Echo txt files with delimiter
william james
01-08-2011, 04:58 AM
Hello Great Coders,
I read about this tutorial but having troubles implementing it...
http://www.theukwebdesigncompany.com/articles/article.php?article=165
I want this code:
<div class="pager hidepiece">
<div class="img rand" >
<a href="/video/(channel).php?t=(title)&id=(video id)"><img src="(image)" alt="(title)"></a>
<div class="desc truncate" >(title)</div>
</div>
</div>
...to be echoed as the container of my contents from a .txt file.
My contents are formatted like this:
channel|title|video|image
sample contents: archives.txt
teenamite|title1|video1|image1
teenamite|title2|video2|image2
teenamite|title3|video3|image3
teenamite|title4|video4|image4
teenamite|title5|video5|image5
etc...
Hope you can help me with this...
Thank you in advance and more power!
djr33
01-08-2011, 05:14 AM
<?php
$input = file('path/to/my/file.txt');
foreach ($input as $line) {
$line = explode('|',$line);
?>
<div class="pager hidepiece">
<div class="img rand" >
<a href="/video/<?php echo $line[0]; ?>.php?t=<?php echo urlencode($line[2]); ?>&id=<?php echo urlencode($line[1]); ?>"><img src="<?php echo $line[3]; ?>" alt="<?php echo $line[1]; ?>"></a>
<div class="desc truncate" ><?php echo $line[1]; ?></div>
</div>
</div>
<?php
}
?>
In the future it is better if you can post your existing code for us to work with.
william james
01-08-2011, 06:02 AM
<?php
$input = file('path/to/my/file.txt');
foreach ($input as $line) {
$line = explode('|',$line);
?>
<div class="pager hidepiece">
<div class="img rand" >
<a href="/video/<?php echo $line[0]; ?>.php?t=<?php echo urlencode($line[2]); ?>&id=<?php echo urlencode($line[1]); ?>"><img src="<?php echo $line[3]; ?>" alt="<?php echo $line[1]; ?>"></a>
<div class="desc truncate" ><?php echo $line[1]; ?></div>
</div>
</div>
<?php
}
?>
In the future it is better if you can post your existing code for us to work with.
Your code works perfect Sir! Thanks a million! No need to put the complete div codes per line inside a txt file!
Sad to say I dont know much about php... I am more into css.
Ive just found a .flv video sponsors and had an idea to create a site where in the main goal is on how to add videos with few clicks.
I surf around the web and collected pre coded php which I feel my project needs (copy/paste :)). I am 90% done! This would not be possible without the help of great coders here like you!
With the tools I collected, I can now add 1000 vids in just 1 min!
I have plenty of friends online who are willing to buy my script and I will be glad to donate here the moment I generate sales :)
It's an adult site :(
I just launched it yesterday though the codings are not yet fully completed.
I have more follow up questions if it is not yet too much :)
Ill just gather my thoughts....
djr33
01-08-2011, 06:46 AM
I'm glad it worked.
Due to the nature of that link, I've removed it from your post. We can still help with questions, but since this is a forum for "all audiences", it's best to not link directly to certain content.
william james
01-08-2011, 06:53 AM
No problem there Sir!
This is the final code I'm using, thanks again!
<?php
$input = file('archives/teenamite.txt');
foreach ($input as $line) {
$line = explode('|',$line);
?>
<div class="pager hidepiece">
<div class="img rand" >
<a href="/video/<?php echo $line[0]; ?>.php?t=<?php echo urlencode($line[1]); ?>&id=<?php echo urlencode($line[2]); ?>"><img src="<?php echo $line[3]; ?>" alt="<?php echo $line[1]; ?>"></a>
<div class="desc truncate" ><?php echo $line[1]; ?></div>
</div>
</div>
<?php
}
?>
Is it also possible if the echoed lines are came from a certain folder, and not directly from a txt file? I have a folder named "archives" where my .txt files located.
And how can I combine the features of the following codes provided by Nile(thanks Nile!):
this code shows the first 4 lines of each txt file from my directory "archives". The first to go on top is the latest updated txt file.
<?php
$dir = "archives/"; // the directory name
$glob = glob($dir."/*.*"); // get all the files with a 'name.extension' name
$files = array_combine($glob, array_map("file", $glob));
array_multisort(
array_map( 'filemtime', $glob ),
SORT_NUMERIC,
SORT_DESC,
$files
);
foreach($files as $key=>$value){
echo "".(implode(array_slice($value, 0, 4)))."";
}
?>
Hope I am not asking too much of your time... but you can take your time and I am willing to wait :)
Thank you in advance!
william james
01-12-2011, 09:47 PM
After a week of headaches for trying to combine all the codes to have the outcome I want , I finally able to make it work. Thanks for the contributors :)
The function of this code is to read a delimited file per line with a pagination(sample post format on the first post).
<?php
$input = file($_SERVER['DOCUMENT_ROOT'] . '/archive/18stream.txt'); # This is your text file.
$items = array();
foreach ($input as $line) {
$line = explode('|',$line);
$items[] = '<div class="pager hidepiece">
<div class="img rand" >
<a href="/video/'.$line[0].'.php?t='.urlencode($line[1]).'&id='.urlencode($line[2]).'">
<img src='.$line[3].' alt='.$line[1].'></a>
<div class="desc truncate" >'.$line[1].'</div>
</div>
</div>';
}
$thispage = $PHP_SELF ;
$num = count($items); // number of items in list
$per_page = 20; // Number of items to show per page
$showeachside = 5; // Number of items to show either side of selected page
$start = $_GET['start'];
if(empty($start))
$start=0; // Current start position
$max_pages = ceil($num / $per_page); // Number of pages
$cur = ceil($start / $per_page)+1; // Current page number
?>
<?php
for($x=$start;$x<min($num,($start+$per_page));$x++){
?>
<?php echo $items[$x];?>
<?php
}
?>
<?php include '../../wj-content/main.php'; ?>
<center>
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" bgcolor="#EAEAEA">
<?php
if(($start-$per_page) >= 0)
{
$next = $start-$per_page;
?>
<a href="<?php echo ("$thispage?start=0");?>">First</a>
|
<a href="<?php echo ("$thispage".($next>0?("?start=").$next:"?start=0"));?>">< Prev </a>
<?php
}
?>
</td>
<td align="center" valign="middle" class="selected">
<?php
$eitherside = ($showeachside * $per_page);
if($start+1 > $eitherside) echo (" ... ");
$pg=1;
if($num>$per_page){
for($y=0;$y<$num;$y+=$per_page)
{
$class=($y==$start)?"pageselected":"";
if(($y > ($start - $eitherside)) && ($y < ($start + $eitherside)))
{
?>
<a class="<?php echo ($class);?>" href="<?php echo ("$thispage".($y>0?("?start=").$y:"?start=0"));?>"><?php echo $pg;?></a>
<?php
}
$pg++;
}
}
if(($start+$eitherside)<$num) echo (" ... ");
?>
</td>
<td align="center" valign="middle" bgcolor="#EAEAEA">
<?php
if($start+$per_page<$num)
{
?>
<a href="<?php echo ("$thispage?start=".max(0,$start+$per_page));?>">Next ></a>
|
<a href="<?php echo "$thispage?start=".($num-$per_page);?>">Last</a>
</center>
<?php
}
?>
</td>
</tr>
<tr><td colspan="3" align="center" valign="middle"> </td></tr>
<tr>
<td colspan="3" align="center" valign="middle" class="selected">
Page <?php echo ($cur);?> of <?php echo ($max_pages);?><br>
( <?php echo ($num);?> videos )
</td>
</tr>
</table>
<style type="text/css">
<!--
.pageselected {
color: #FF0000;
font-weight: bold;
}
-->
</style>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.