SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Rochester, NY
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined Index problem?

    Hey guys, got this project due tomorrow for class that my group's been working hard on, but we've come upon a roadblock. My friend wrote some PHP code to read all of these professor's names from a file, and then construct them into a table. You can view this here:

    http://www.rankitnow.com/409/final/t...professors.php

    When I run this code locally, there are no errors. However, upon uploading, I get those four errors that you are probably viewing now. Here is the code used:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style type="text/css">
    .heavy {
        font-weight: bold;
    }
    </style>
    </head>

    <body>

    <h1>Information Technology Faculty</h1>

    <p>
        A listing of all current full-time faculty in the Information Technology Department. 
        This listing is sortable by any of the categories at the top. Under concentration, 
        the abbreviations stand for:
    </p>

    <ul>
        <li><span class="heavy">CME:</span> Computer Mediated Experience</li><br />
        <li><span class="heavy">EC:</span> Enterprise Computing</li>
        <li><span class="heavy">NSA:</span> Networking and System Administration</li>
    </ul>

    <?
        
    echo "<table cellspacing='0' cellpadding='4' border='1'>";
        echo 
    "<tr>";
        echo 
    "<td class='heavy'>Last Name</td>";
        echo 
    "<td class='heavy'>First Name</td>";
        echo 
    "<td class='heavy'>Title</td>";
        echo 
    "<td class='heavy'>Focus Area</td>";
        echo 
    "</tr>";
        
        
    $handle fopen("professors.txt","r");
        while(!
    FEOF($handle))
        {
            
    $line fgets($handle1000);
            
    $pieces explode(":"$line);
            echo 
    "<tr>";
            echo 
    "<td><a
    href='http://it.rit.edu/detail.maml?t_Link=
    $pieces[0]&t_RenderAs=output'>$pieces[1]</a></td>";
            echo 
    "<td>$pieces[2]</td>";
            echo 
    "<td>$pieces[3]</td>";
            echo 
    "<td>$pieces[4]</td>";
            echo 
    "</tr>"
        }
        
        
    fclose($handle);
        
        echo 
    "</table>";
    ?>
    </body>
    </html>
    If anyone can help me before the 6pm eastern deadline tomorrow, I'd greatly appreciate it! Sorry for not posting this earlier, but I just was starting to add his content into the site when I noticed this a few minutes ago. Thanks and I hope that someone could help!

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Try changing the line(s):
    PHP Code:
            echo "<td>$pieces[2]</td>"
    to....
    PHP Code:
            echo "<td>"$pieces[2]. "</td>"
    See if that makes a difference.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast mullen's Avatar
    Join Date
    Jul 2004
    Location
    Durham, UK
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    As you can see, the rest of your page appears fine. This is because the messages you are getting are actually notices, rather than warnings or errors. Basically, the first time round the loop that $pieces array doesn't seem to contain anything. Maybe the first line in your data file is blank, or a header row or title?

    The best way around this would be to add a check to ensure the $pieces array has been set up correctly, (i.e. all 5 parts are there) - maybe using count(). If not, you could skip that iteration of the loop - continue().

    Alternatively, you could set error reporting so that it doesn't display notices...
    PHP Code:
    error_reporting(E_ALL E_NOTICE); 
    Hope this helps,
    Dan

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Rochester, NY
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys! Well, I turned the error reporting off, but I am curious as to how to integrate the other fixes that you mentioned Mullen. I'm still, unfortunately, a newb to PHP (thank gosh this trimester is over! Get to study PHP now during my small break, heh ).

  5. #5
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    those messages were only because you did not initialise $pieces.

    $pieces = array(); would've solved it I guess....umm..yeh

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php is pretty liberal on when it requires you to initialize an array before using it. from what i've seen, php rarely requires that you initialize the array first, so what dannov is doing, while not great structure, won't cause any errors or warnings by php

    spikeZ has the correct answer. array references cannot be included inline-style, even if they are inside of double-quotes...you either have to break out of the array or use {} (if i remember correctly)

    example:
    PHP Code:
    $holder=array('string');

    echo 
    "This is a $holder[0]";  // bad

    echo 'This is a '.$holder[0];  // good

    echo "This is a ${holder[0]}";  // good 
    Last edited by jinkas; Nov 19, 2004 at 10:31.

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Rochester, NY
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I did originally perform jinkas' suggestion, however, I still was getting the warning messages! I also tried what you said after reading php.net, astericks, but that didn't seem to work ( unless I was initializing it in the wrong place, I initialized it right below the echoes & between the $handle).

  8. #8
    SitePoint Enthusiast mullen's Avatar
    Join Date
    Jul 2004
    Location
    Durham, UK
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you still get the notices when you add that error_reporting line I posted? The data is actually displaying correctly in the table, so it isn't down to the array references in the echo statements.

    If the notices have disappeared following the addition of that error_reporting line, I wouldn't worry too much about it. Although not good style, it's not incorrect to echo an array element that doesn't exist.

    You could look up those other commands (count, continue, etc.) in the PHP manual for more info if you want to improve that section of code.

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Rochester, NY
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply Dan! The notices did go away, thanks much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •