SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: Undefined Index

  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined Index

    I am getting the Undefined index error message but I dont know why. I need to know whats going on and why this is happening...I am trying to display records stored in MySQL....Here is a sample of my code.....


    <table>
    <tr>
    <th>Team Name</th>
    <th>HomeTown</th>
    <th>Wins</th>
    <th>Losses</th>
    <th>Ties</th>
    <th>Division</th>

    </tr>

    <?php foreach($teams as $team): ?>

    <tr>
    <td><?php echo $teams['0']['Team Name']; ?></td>
    <td><?php echo $teams['1']['HomeTown']; ?></td>
    <td><?php echo $teams['2']['Wins']; ?></td>
    <td><?php echo $teams['3']['Losses']; ?></td>
    <td><?php echo $teams['4']['Ties']; ?></td>
    <td><?php echo $teams['5']['Division']; ?></td>

    print_r($teams)

    What is going on here? Any feedback would be much appreciated!

  2. #2
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you paste your mysql query and table structure?

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT `Team`.`Teamid`, `Team Name`, `Team`.`Hometown`, `Team`.`Wins`, `Team`.`Losses`, `Team`.`Ties`, `Team`.`Division` FROM `teams` AS `Team` WHERE 1 = 1 ......




    Teamid Team Name Hometown Wins Losses Ties Division
    1 Parkview Pirates Mexia 5 2 1 TBall
    2 Groesbeck Tire Groesbeck 8 7 2 TBall
    3 Marlin Bulldogs Marlin 2 8 0 TBall
    4 Hyden's Hustlers Teague 1 8 1 TBall
    5 Rally Boys Riesel 6 4 3 TBall

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Make sure your query is valid and records were returned from your query (SELECT). Also you need to store the values in an appropriated array.

    See this page for more info:
    http://us2.php.net/manual/en/function.mysql-query.php

  5. #5
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT `Team`.`Teamid`, `Team Name`, `Team`.`Hometown`, `Team`.`Wins`, `Team`.`Losses`, `Team`.`Ties`, `Team`.`Division` FROM `teams` AS `Team` WHERE 1 = 1 ......

    Team Name doesnt have Team`. in front of it and all the rest do, that could be your issue.

    Just check your query that is where the problem lies.

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just guessing as there is not much data to debug.

    shouldn't <?php echo $teams['0']['Team Name']; ?
    be
    <?php echo $team['0']['Team Name']; ?

    "team" rather than "teams"

    It should be and if still error then do a print_r($team) and see if it maths that structure you are using; team[0][column]
    ---------------------------
    Errors = Improved Programming.
    My Site

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what is going on here. I have tried just about everything people have suggest today! Lets start from the beginning. What does the undefined index error imply?

  8. #8
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, you chamged terms to term?
    And what is the output of print_r($term)?

    You have to provide more info. And you can google that error.
    ---------------------------
    Errors = Improved Programming.
    My Site

  9. #9
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well actually I chnaged a few things! Im not getting the error mesage anymore...its just that my records appear above my columns! I dont know where i went south! I need to figure out what is causing my records to appear above my columns!

  10. #10
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We will need to see your code for that but I think that issue is not HTML related. You are missing a tag somewhere
    ---------------------------
    Errors = Improved Programming.
    My Site

  11. #11
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <table>
    <tr>
    <th>Team Name</th>
    <th>Hometown</th>
    <th>Wins</th>
    <th>Losses</th>
    <th>Ties</th>
    <th>Division</th>

    </tr>


    <?php foreach($teams as $Team): ?>

    {
    <?php echo $Team['Team'] ['Teamid']; ?>
    <?php echo $Team['Team'] ['Team Name']; ?>
    <?php echo $Team['Team'] ['Hometown']; ?>
    <?php echo $Team['Team'] ['Wins']; ?>
    <?php echo $Team['Team'] ['Losses']; ?>
    <?php echo $Team['Team'] ['Ties']; ?>
    <?php echo $Team['Team'] ['Division']; ?>
    }







    <?php endforeach; ?>

  12. #12
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There should <tr> after foreach starts and </tr> before foreach ends. That's just malformed HTML issue. And ofcourse you need to close table after foreach
    ---------------------------
    Errors = Improved Programming.
    My Site

  13. #13
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks rana....that did help! It had my tr tags earlier but between all my editing I deleted my tags! Now after I put my tags in the proper place....my records are still on top of my columns....I dont understand why my records arent under my columns.

  14. #14
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You need to add the appropriate <tr>, </tr>, <td> and </td>s. Without those, it will indeed just put the content above the table.
    Also, it's either [php]foreach ($a as $b): ... endforeach;[/samp] or [php]foreach($a as $b) { ... }[/samp], not both at the same time

    PHP Code:
    <table>
    <thead>
      <tr>
        <th>Team Name</th>
        <th>Hometown</th>    
        <th>Wins</th>
        <th>Losses</th>
        <th>Ties</th>
        <th>Division</th> 
      </tr>
    </thead>

    <tbody>
      <?php foreach($teams as $Team): ?>
        <tr>
          <td><?php echo $Team['Team'] ['Teamid']; ?></td>
          <td><?php echo $Team['Team'] ['Team Name']; ?></td>
          <td><?php echo $Team['Team'] ['Hometown']; ?></td>
          <td><?php echo $Team['Team'] ['Wins']; ?></td>
          <td><?php echo $Team['Team'] ['Losses']; ?></td>
          <td><?php echo $Team['Team'] ['Ties']; ?></td>
          <td><?php echo $Team['Team'] ['Division']; ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
    </table>
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  15. #15
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just confirm, you also wrapped EACH row with "td" tags, right? It has to be, to be considered part of that table and row.
    ---------------------------
    Errors = Improved Programming.
    My Site

  16. #16
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rana you are swell!! I don't know if you're a male or female...but if u a dude "HIGH FIVE"!!......AND if you a woman....I swear I would kiss your feet right now!! Thanks!!


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
  •