+ Reply to Thread
Results 1 to 4 of 4

Thread: Php Include file within a While loop

  1. #1
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    Php Include file within a While loop

    Hi, i was wondering if it is possible to use a php include within a while loop. like this. i tried it but the results are not showing. i only get the first result from the query.
    PHP Code:
    <?php while($data mysql_fetch_array($result))  {?>    

    <div id="resultswrapper">
           <?php $path $_SERVER['DOCUMENT_ROOT']; $path .= "/resultscontainer.php"; include_once($path);?>                    
         </div>
        
    <?php  }?>
    the resultscontainer.php file contains a table to display the query. but i seem to be only getting the first result. but if i were to just paste the table there, the while loop works fine. is there something im missing out?

    Thanks.
    Last edited by tillabong; 06-02-2010 at 04:53 AM.

  2. #2
    kadai's Avatar
    kadai is offline x10Hosting Member kadai is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    51

    Re: Php Include file within a While loop

    Well, since the file loaded ($path) does not change, my guess is that PHP interpretates that there is no need to load the given file because it was already loaded.

    Since the include_once is a function that includes once, and only once a file... it will be skipped each new time it is loaded with the very same file it was loaded.

    A way to test different results is to simply use include($path); instead. May be risky but you'll surely get a result.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Php Include file within a While loop

    A better solution would be to define a class or function in "resultscontainer.php" and call the function/a method of the class to display each row. Abstracting further, define another class or function to display all the results; give it the result of a query and a class or function that displays each item, and the result view loops over the results displaying each item.

    Since $path is invariant over the loop, it should be set outside the loop. Otherwise, you're wasting cycles re-evaluating the expression.
    Last edited by misson; 06-02-2010 at 01:53 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    Re: Php Include file within a While loop

    ok thank you so much for your replies. i will study them carefully. thanks!

+ Reply to Thread

Similar Threads

  1. how to include a file
    By piorivas in forum Programming Help
    Replies: 2
    Last Post: 12-29-2009, 10:36 AM
  2. Include file
    By triviata in forum Free Hosting
    Replies: 9
    Last Post: 10-19-2009, 10:25 AM
  3. PHP Include, file help!
    By stillDOLL in forum Programming Help
    Replies: 8
    Last Post: 10-07-2008, 04:26 AM
  4. include html from another file?
    By galaxyAbstractor in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 11-17-2007, 01:24 PM
  5. Replies: 0
    Last Post: 10-10-2007, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers