proggR Posted July 18, 2008 Share Posted July 18, 2008 I've been boggled by this for a few days now and I've just been putting it off but I need it fixed. I'm reading in a text file thats path is saved as $course. This will get checked to see if its a valid file and if it is its supposed to open it up and read the contents into an array. It loops through a while loop as long as it hasn't reached the end of the file. For redundancy I have an if statement within that loop to also check if its the end of the file and if its not, it read the line, and if it is, it closes the file. If none of this has been true it returns a page called missing.html to be displayed. The problem is that it appears that somehow the array gets one more index then it should be getting because where I'm supposed to have 3 posts, I have 3 posts and an error saying it couldn't include '' (blank). The exact error is: Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in E:\Program Files\Apache Group\Apache2\htdocs\class.php on line 104 which refers to this section: <?php //prints all the posts on page foreach ($post[$page] as $value) { include($value); echo "<a href =\"".$value."\">".$value."</a><br>"; } ?> The reason it has $page is because I also have this section that determines what page you're viewing and splits the first array ($item) into a 2d array with 5 articles per page <?php //determimes page if (isset($_GET["page"])){ $page = $_GET["page"]; } else{ $page=0; } //end determining page $post= array_chunk($item,5);// splits the posts into pages with 5 posts per page ?> This is the main section that I believe is the problem but I'm not sure. If anyone sees anything I've missed thats causing this extra index value please let me know. I have two sections of my site that need this to work. Thanks in advance <?php //assigns each post to a one dimensional array which will be split into a 2d array later $counter = 0; if(is_file($course)){ $fh = fopen($course,'rt'); While(!feof($fh)){ if(!feof($fh)) { $buffer = fgets($fh); $item[$counter] = trim($buffer); $counter = $counter+1; } else{ fclose($fh); } } } else { $item[0]="missing.html"; } //end the reading of the file ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 18, 2008 Share Posted July 18, 2008 you could add a check like this <?php $buffer = trim(fgets($fh)); if($buffer != ''){ $item[$counter] = trim($buffer); $counter = $counter+1; } ?> Scott. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 18, 2008 Author Share Posted July 18, 2008 I had something similar to that too. Changing the code I have with your example gave me an infinite loop though for some reason. I'll keep trying that check and see if I just did something wrong though. Any other ideas? *edit* ok no more infinite loop, but still the extra index value. Do you think maybe its happening with the array_chunk() or my foreach() sections? The logic of that first section (which I've posted last) seems right to me and has redundant checks to make sure it is. I'm at a loss. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted July 18, 2008 Share Posted July 18, 2008 if you include variables into a function there scope remains inside that function unoless tehy are all globals Quote Link to comment Share on other sites More sharing options...
proggR Posted July 18, 2008 Author Share Posted July 18, 2008 But how would that change any of this? None of these are declared within a function. I'm not calling any functions that aren't from the PHP library. I haven't got around to organizing my code into my own functions so they're all declared outside of variables. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted July 18, 2008 Share Posted July 18, 2008 But how would that change any of this? None of these are declared within a function. I'm not calling any functions that aren't from the PHP library. I haven't got around to organizing my code into my own functions so they're all declared outside of variables. if you include pear into a function you can only use it inside that function, at a lower level this is teh arangment of the memory and code that physicaly restricts it when u declare a function it has a memory space outside of wich nothis is recognised, instead from outside you can pip info into its signiture paramiters but u cant talk to it otherwise, so if u include pear into a function you can only use it inside that functuion. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 18, 2008 Author Share Posted July 18, 2008 I don't know what pear is. The C:.../pear path it has as the include_path has nothing to do with any variables I've created. C: is my Vista partition, I'm working on E:. All I'm including is the variables from the array that hold the value of the path to files I'm using. The three that actually exist work fine, its just got this 4th that it shouldn't have. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 18, 2008 Author Share Posted July 18, 2008 bump (sorry to be a pain) Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 18, 2008 Share Posted July 18, 2008 can you post the whole file or more code because that looks like it should be fine Scott. Quote Link to comment Share on other sites More sharing options...
proggR Posted July 19, 2008 Author Share Posted July 19, 2008 Those are the only scripts. The rest is just HTML code. I'll post the whole file when I'm at home and have it again. I ended up putting a check in my foreach() that checked before it included and if it was just "" as a value it would include my standard "Oops, this file isn't there" file. I was excited that it worked except when I tested it, I had 5 posts and this was the 6th so it was on its own page, but then I deleted the posts and only had 2 and it would come up with that message on the same page as the other posts. So now I have to think again. Its the last thing I have left to do except security so I hope I can resolve it soon. I'll post the whole code tonight and see if anyone can spot something I'm missing. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted July 19, 2008 Share Posted July 19, 2008 sorry i thoght it was in a unction i was in a rush Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 20, 2008 Share Posted July 20, 2008 you have posted 3 bits of code can you post the three bits in order or something that doesn't look right. Scott. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted July 21, 2008 Share Posted July 21, 2008 have nu got pear installed and is it in teh place it says ? 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.