IOAF Posted May 6, 2008 Share Posted May 6, 2008 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; } } } } Quote Link to comment Share on other sites More sharing options...
atravotum Posted May 6, 2008 Share Posted May 6, 2008 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! Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 6, 2008 Author Share Posted May 6, 2008 cheers for that! ill do my best to integrate it but im not really good with php, any pointer on what i should be replacing? Quote Link to comment Share on other sites More sharing options...
atravotum Posted May 6, 2008 Share Posted May 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 6, 2008 Author Share Posted May 6, 2008 i just cant figure it out. i get the idea of what your saying i just dont know what to actually do. not brilliant with php Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 6, 2008 Author Share Posted May 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 6, 2008 Share Posted May 6, 2008 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(). Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 6, 2008 Share Posted May 6, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 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; } } } } Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 7, 2008 Share Posted May 7, 2008 At line 5 replace: if($line[0] == "albumdesc"){ with: if($line == "albumdesc"){ Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 server has gone down now! i love coding websites will try that when it comes back up. Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 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; } } } } Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 7, 2008 Share Posted May 7, 2008 You are returning the array from file() to $lines, not $content. So it should be: foreach($lines as $line){ Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 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... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 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 {" Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 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; } } } } ?> Quote Link to comment Share on other sites More sharing options...
IOAF Posted May 7, 2008 Author Share Posted May 7, 2008 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.