#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14
    Is there a problem with using an include in a loop? The example below returns a parse error on the } else { line below the include("spec.inc"); line.

    The spec.inc is printed fine once, but then it tries to print it twice and dies. See the link for the current example in action:
    http://www.georgiaoffroad.com/bike2.php3?model=88kx500


    <?php

    // CHECK FOR EXISTENCE OF SPECIFICATION DATA
    // IF 'Y' OR 'y', INCLUDE SPEC TABLE, OTHERWISE CLOSE THE TABLE AND CONTINUE

    while ($myrow = mysql_fetch_array($result)) {

    if($myrow['specs'] == "Y" &#0124; &#0124;$myrow['specs'] == "y"){
    include("spec.inc");
    } else {
    echo "<tr>n <td colspan=5 height=20></td></tr></table>nn";
    }
    }
    mysql_data_seek($result,0);
    ?>
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    I don't see anything wrong with the code as far as executing. Did you cut and paste the code into the message or type it in?

    One caveat: DON'T use include() in a loop. include() pulls the file each time it is enountered making it good to use inside conditionals (won't be accessed if the condition isn't met) but terrible to use inside a loop. In a loop use require() instead.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Niteroi, RJ, Brazil
    Posts
    30
    Rep Power
    14
    ...but PHP 101 in DevShed said to use include inside loops because require is unconditional or something...

    Try it, but I think it won't work... You could make it work?

    ------------------
    Eduardo Teixeira Cardoso
    PHP Programmer from Brazil
    idnotfound@vircio.com.br
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Ontario, Canada
    Posts
    498
    Rep Power
    18
    Require
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>
    The require statement replaces itself with the specified file, much like the C preprocessor's #include works.
    This means that you can't put a require statement inside of a loop structure and expect it to include the contents of a different file on each iteration. To do that, use an include statement.
    [/quote]

    include
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>
    The include statement includes and evaluates the specified file.
    This happens each time the include statement is encountered, so you can use an include statement within a looping structure to include a number of different file.
    [/quote]

    I hope this helps clear the air about include and require... Ripped those right off the php manual. :P

    ------------------
    To alcohol! The cause of, and solution to, all of life's problems. -- Homer Simpson
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Actually, I think that is what is causing the confusion.

    You would use include() inside the loop if you were including seperate files on each iteration. If you are using the same file each time you would use require() to prevent the overhead of accessing the file each time. Granted, that isn't much overhead, but if you loop has several hundred plus iterations.....

Similar Threads

  1. Virtual includes on Apache
    By scottyfinn in forum Apache Development
    Replies: 1
    Last Post: January 18th, 2004, 04:56 AM
  2. inheritance, includes & portability
    By podulator in forum PHP Development
    Replies: 5
    Last Post: January 31st, 2003, 12:16 PM
  3. Variable scope & recursive includes
    By kingromes in forum PHP Development
    Replies: 0
    Last Post: January 21st, 2003, 12:03 AM
  4. using includes
    By twcinpa in forum PHP Development
    Replies: 2
    Last Post: April 10th, 2002, 12:43 PM
  5. problems with includes
    By ecchu79 in forum HTML Programming
    Replies: 5
    Last Post: April 14th, 2000, 01:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo