fawkesb Posted April 16, 2009 Share Posted April 16, 2009 Greetings to all. As a new user of these forums let me start by saying that I am not familiar with PHP just yet although I am learning (and liking it - a LOT). I have a lot of scripting and programming experience so it's a good learning curve, but I need some help with a simple logic problem. Here is a VERY simple script I wrote to read information from 4 different files, combine them into a simple Blog structure, and echo the results: <?php $fh1 = fopen("../dsp/titles.dsp","r"); $fh2 = fopen("../dsp/dates.dsp","r"); $fh3 = fopen("../dsp/authors.dsp","r"); $fh4 = fopen("../dsp/bodies.dsp","r"); while (!feof($fh1)) { $line1 = fgets($fh1); $line2 = fgets($fh2); $line3 = fgets($fh3); $line4 = fgets($fh4); echo "<b>{$line1}</b><br />\n <i>{$line2}</i><br />\n <i>{$line3}</i><br />\n {$line4}<br />\n"; if ($line1!=""){echo "<hr>";} } fclose($fh1); fclose($fh2); fclose($fh3); fclose($fh4); ?> Obviously, this is a very impish and simple minded script as I am just learning the language and not yet familiar with all of the array options. Problem: I am not printing to screen the last record of any file. If you add a record, then you will see the 'next to last' one that was added to all files. Upon manual verification of the files, all is well. I assume this is an issue with the * feof * statement however I have tried many variations and cannot seem to get the logic to work correctly. Obviously I need to add 'one more grab' somehow in the while loop. Thank you in advance for any help from the Forums here and I would of course appreciate any shortcuts in setting up the file array, etc. if available. Take care, John Link to comment https://forums.phpfreaks.com/topic/154376-solved-logic-issue/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 16, 2009 Share Posted April 16, 2009 Your code assumes there is an equal number of lines (as found by the fgets function) in each file. It's likely you have some line endings messed up so that there are actually a different number of lines between at least the first file that you are testing the feof() of and the rest of the files. You should actually come up with a different structure. Store all the related data as one line in a single file. Use a separator character(s) that won't ever appear in the data (| or || is commonly used as a separator.) Then read the line and explode it using the separator character(s) to get the pieces of related data. Link to comment https://forums.phpfreaks.com/topic/154376-solved-logic-issue/#findComment-811637 Share on other sites More sharing options...
fawkesb Posted April 16, 2009 Author Share Posted April 16, 2009 Ok, thank you for the suggestion. I will try using explode, sep. character, and a true/false switch for nulls when someone doesn't enter anything in and just hits "submit". Take me a bit here, but will see what I come up with. Thanks again, John Link to comment https://forums.phpfreaks.com/topic/154376-solved-logic-issue/#findComment-811713 Share on other sites More sharing options...
fawkesb Posted April 16, 2009 Author Share Posted April 16, 2009 SOLVED: Thank you very much for the pointer - following is the reader code: <?php $file = fopen("../dsp/events.dsp",'r') or exit("Unable to open file!"); echo "file: $file<br><br>"; $gets = fgets($file); echo "gets: $gets<br><br>"; // debug $strarray = explode("|", $gets); // ---- setup a loop to grab a set number of records from the array at a time ---- for ($i=0; $i<count($strarray); $i++){ echo "<b>".$strarray[$i]."</b><br>"; // read first line of array echo "<i>".$strarray[$i+1]."</i><br>"; // add 1 to counter to get second line (no change to counter) echo "<i>".$strarray[$i+2]."</i><br>"; // add 2 to counter to get third line (no change to counter) echo "".$strarray[$i+3]."<br><hr>"; // add 3 to counter to get fourth line etc. etc. etc. (no change to counter) $i=$i+3; // now step the counter up by desired number - setting it up for next chunk you want } // ---- loop exits at end of array with no problems ---- fclose ($file); // don't forget to close the file ?> And for those interested or may be helped by this - the writer code: <?php $file = fopen("../dsp/events.dsp", 'a'); // open file $title=$_POST['title']; // read in data from a form $date=$_POST['date']; // . $author=$_POST['author']; // . $body=$_POST['body']; // . $strall = " | $title | $date | $author | $body"; // create your new string - be sure to start with your sep char echo $strall; // debug fwrite ($file,"$strall") or die("Couldn't write values to file!"); fclose($file); // don't forget to close the file echo "<br><br>Saved to new entry successfully!"; // setup a header redirect or whatever here ?> So anyway, thanks again and I'm sure there is a cleaner way to do this in the for loop, but it does work perfectly for what I am using it for - a simple blog type event updater for my websites. Take care, John Link to comment https://forums.phpfreaks.com/topic/154376-solved-logic-issue/#findComment-811804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.