Jump to content

Help to get echo to print all lines not just $line[1]


IOAF

Recommended Posts

Hi, this is probably something really easy or impossible knowing me.

 

The script im using shows a description on a page, the description is inputed via a WYSIWYG editor that when you input your info into, saves to a file in multiple lines (as you would normally code the html that the WYSIWYG outputs)

 

But my problem is getting the information back on the web page, the script uses the line echo trim($line[1]); which only reads from the first line, i know i could change 1 to 3 of 4 but i need it to output all the lines in the file. Ive looked on Google to try and find the different attributes to $line but i cant seem to figure out the problem.

 

Is it possible?

 

any help is much appreciated!

 

if($IG_CONFIG['usedatafiles'] AND $IG_CONFIG['albumdescriptions']){

if(@file_exists($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt')){

$lines = file($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt');

foreach ($lines as $line) {

$line = explode("|",$line);

if($line[0] == "albumdesc"){

echo '<br/><div class="description">';

echo trim($line[1]);

echo '</div><br/>';

break;

}

}

}

}

I think this is what your aiming for

 

//Process to pull the data from a file, one line at a time. Then checks if the line has data, if it does send it to be processed and stored.
$file=fopen($file_temp,"r")or exit("Unable to open file!");
$i=0;
while(!feof($file))
{
   $strings[$i]=fgets($file);
   	if((trim($strings[$i]) != "") && $strings[$i] != NULL)
{
	echo $strings[$i];
}
$i++;
}
fclose($file);

 

Thats a piece of code i used to pull data from a csv file line by line store it to an array throw it into a function to handle the commas and return the separated value. Hope it helps!

Hmm I'm not quite sure on some of the code you have, its not code I have used however... If I had to take an educated guess you should be able to put replace your for each loop, with the while loop i gave you. Since they both aim to accomplish the same task.

This topic seems confusing to me lol, so im just throwing code to open a file and echo its content:

 

Using fopen() and fread()

<?php
$handle = fopen('file.txt', 'r');
$content = fread($handle, filesize('file.txt'));
echo nl2br($content);
?>

 

Using file()

<?php
$content = file('file.txt');
foreach($content as $line){
        echo $line . '<br />';
}
?>

 

Using file_get_contents()

<?php
$content = file_get_contents('file.txt');
echo nl2br($content);
?>

 

As u can see the simplest and shortest way is using file_get_contents().

Cheers for the help gear, i think im getting closer

 

ive modified my code a little using what you put for using file()

 

this makes sence to me how it should work... but on the page when you view it it doesnt show anything at all, no error but no text (and looking at the pages source its not outputting anything at all) argh!

 

if($IG_CONFIG['usedatafiles'] AND $IG_CONFIG['albumdescriptions']){
	if(@file_exists($IG_CONFIG['imagedir'].$dir.'/idutgallerydata.txt')){
		$content = file($IG_CONFIG['imagedir'].$dir.'/idutgallerydata.txt');
		foreach($content as $line){
			if($line[0] == "albumdesc"){
				echo '<br/><div class="description">';
				echo $line . '<br />';
				echo '</div><br/>';
				break;
			}
		}
	}
}

Doesnt seem to work.

 

Warning: Invalid argument supplied for foreach() in /home/william/public_html/gallery/index.php on line 282

 

but the way i have this coded now (ill put at the bottom of this post), would every line in the text file need to have 'albumdesc' at the beginning of it? its only there on the first line (what i assume php calls line[0])

 

 

	if($IG_CONFIG['usedatafiles'] AND $IG_CONFIG['albumdescriptions']){
	if(@file_exists($IG_CONFIG['imagedir'].$dir.'/idutgallerydata.txt')){
		$lines = file($IG_CONFIG['imagedir'].$dir.'/idutgallerydata.txt');
		foreach($content as $line){
			if($line == "albumdesc"){
				echo '<br/><div class="description">';
				echo $line . '<br />';
				echo '</div><br/>';
				break;
			}
		}
	}
}

 

No no no!  You completely misunderstood what line[1] meant from the beginning!  In your original script, you call explode() on the line, separating it at the |.  $line[1] is the part on the line after the |.  What was the problem with the script?

ok, i tried the "foreach($lines as $line){" and the page dident display anything again.

 

 

from the top:

 

this is whats in the text file (example):

 

albumdesc|quadbike

<ul>

<li>1234cc</li>

<li>brakes/li>

<li>etc...</li>

</ul>

<p>some more info</p>

 

with the original code, the page will display only the top line, which on the site will read "quadbike"

 

i need it to echo all the lines not just the top, and i cant figure out how to do that. so when i say lines i mean

 

this is a line

and this is another

 

argh!

 

 

edit:

 

so the site should display:

 

quadbike

 

.1234cc

.brakes

.etc...

 

You want EVERYTHING to display?  And do you only want the albumdesc thing to display in the div tags?

 

if($IG_CONFIG['usedatafiles'] AND $IG_CONFIG['albumdescriptions']){
      if(@file_exists($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt')){
         $lines = file($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt');
         foreach ($lines as $line) {
            $line = explode("|",$line);
            if($line[0] == "albumdesc"){
               echo '
<div class="description">';
               echo trim($line[1]);
               echo '</div>
';
               else {
               echo $line;
               }
            }
         }
      }
   }

 

Copy and paste that in.  It should work as you wanted it.

My bad.  I counted the brackets incorrectly.  Here (I checked with my syntax highlighter):

 

<?php
if($IG_CONFIG['usedatafiles'] AND $IG_CONFIG['albumdescriptions']){
      if(@file_exists($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt')){
         $lines = file($IG_CONFIG['imagedir'].$dir.'/xxxxx.txt');
         foreach ($lines as $line) {
            $line = explode("|",$line);
            if($line[0] == "albumdesc"){
               echo '
<div class="description">';
               echo trim($line[1]);
               echo '</div>
';
            }
       else {
               echo $line;
               }
         }
      }
   }
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.