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;

}

}

}

}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

i think i should be able to replace that bit of code with a new one that will open the txt file, and echo all the text in that file.

 

can anyone help me wright that bit of code? im slowly getting there with php but im still struggling lol

 

thanks

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

I just tried a shorter approach for the file() technique, which removes the need to run a foreach loop.

 

<?php
$content = file('file.txt');
$content = implode('<br />', $content);
echo $content;
?>

Link to comment
Share on other sites

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;
			}
		}
	}
}

Link to comment
Share on other sites

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;
			}
		}
	}
}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Tried what you just posted water and got this error ( i cleaned up the brakes the forum put into the code)

 

Parse error: syntax error, unexpected T_ELSE in /home/william/public_html/gallery/index.php on line 288

 

doesnt seem to like  "else {"

Link to comment
Share on other sites

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;
               }
         }
      }
   }
?>

 

Link to comment
Share on other sites

hahaha

 

this time it outputs this:

 

<div class="description">edghd</div>ArrayArrayArrayArrayArrayArrayArrayArray

 

were getting there... i think?

 

edit: i notice its not finished ecoing the whole thing (the 3rd echo)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.