<td><?php echo $teams['team']['Team Name']; ?></td> <td><?php echo $teams['team']['HomeTown']; ?></td> <td><?php echo $teams['team']['Wins']; ?></td> <td><?php echo $teams['team']['Losses']; ?></td> <td><?php echo $teams['team']['Ties']; ?></td> <td><?php echo $teams['team']['Division']; ?></td>
32 Replies - 1592 Views - Last Post: 26 April 2012 - 02:56 PM
#1
Problem: Undefined Index
Posted 26 April 2012 - 08:51 AM
I am getting a error message...it reads Undefined index: team [APP\views\teams\index.ctp lines 17-22. Now I think I know what is going on I just want to be sure.. I believe the reason I am getting this error is because I dont have a SQL statement. Is this correct? Here is a sample of the code
Replies To: Problem: Undefined Index
#2
Re: Problem: Undefined Index
Posted 26 April 2012 - 08:57 AM

POPULAR
Quote
Undefined index: team
Break down the error. Undefined index.. that means some collection or array is having problems with the index you are feeding it... then it indicates the word 'team'... my guess is that your collection "$teams" does not like having an index referenced by the string 'team'.
Much like if I had an array with no items in it and then tried accessing item 23 it would not like that.. or if I added three items to that array and attempted to access the item at index "jayz". The array would throw up it's hands and bitch that there is no index called "jayz"... there are index 0, 1, and 2... but no "jayz".
#3
Re: Problem: Undefined Index
Posted 26 April 2012 - 09:19 AM
How about putting print_r($teams) in your page?
PHP Debugging 101.
PHP Debugging 101.
#4
Re: Problem: Undefined Index
Posted 26 April 2012 - 09:25 AM
Jack that didn't work...when I inserted that in my code it printed print_r($teams) five times and under that I still see the undefined index message.
#5
Re: Problem: Undefined Index
Posted 26 April 2012 - 10:19 AM
Not echo print_r($teams), just <?php print_r($teams); ?>. ONCE.
#6
Re: Problem: Undefined Index
Posted 26 April 2012 - 10:35 AM
this is the end result Jack.....Array ( [0] => Array ( [Team] => Array ( [Teamid] => 1 [Team Name] => Parkview Pirates [Hometown] => Mexia [Wins] => 5 ...Its did this with all my records....Don't know why....I am going to try to continue to hammer this out though!
This post has been edited by BarNunBoi: 26 April 2012 - 10:36 AM
#7
Re: Problem: Undefined Index
Posted 26 April 2012 - 10:45 AM
Do I need to open a connection to my DB and have Select statements included?
#8
Re: Problem: Undefined Index
Posted 26 April 2012 - 10:56 AM
hm, let’s format this output to make the structure visible
Array (
[0] => Array (
[Team] => Array (
[Teamid] => 1
[Team Name] => Parkview Pirates
[Hometown] => Mexia
[Wins] => 5
#9
Re: Problem: Undefined Index
Posted 26 April 2012 - 11:31 AM
So it's an array within an array. You need to iterate over the outer array.
And, if you've got spaces in your database column names, it's a generally a bad idea.
foreach($teams as $team)
{
echo $team['Team']['Teamid'];
}
And, if you've got spaces in your database column names, it's a generally a bad idea.
#10
Re: Problem: Undefined Index
Posted 26 April 2012 - 11:38 AM
I don't think its an array within a array....I had used some code you gave me and that's how it displayed.
#11
Re: Problem: Undefined Index
Posted 26 April 2012 - 11:47 AM
Look at post#8. What do you think lines 1 and 2 mean?
#12
Re: Problem: Undefined Index
Posted 26 April 2012 - 12:15 PM
Im not talking about post#8......I see its an array within a array...but that only happened because I used <?php print_r($teams); ?> like Jack suggested and
Array (
[0] => Array (
[Team] => Array (
[Teamid] => 1
[Team Name] => Parkview Pirates
[Hometown] => Mexia
[Wins] => 5
was the end result.
Array (
[0] => Array (
[Team] => Array (
[Teamid] => 1
[Team Name] => Parkview Pirates
[Hometown] => Mexia
[Wins] => 5
was the end result.
#13
Re: Problem: Undefined Index
Posted 26 April 2012 - 12:28 PM
BarNunBoi, on 26 April 2012 - 03:15 PM, said:
Im not talking about post#8.
Start talking about it. It's the key to your problem. What does it mean? Why did Jack ask you to use print_r? You need to know the answers to these questions, but more importantly, you need to know how to find the answers and why it's important to ask them.
This post has been edited by CTphpnwb: 26 April 2012 - 12:28 PM
#14
Re: Problem: Undefined Index
Posted 26 April 2012 - 12:37 PM
BarNunBoi, on 26 April 2012 - 01:15 PM, said:
Im not talking about post#8......I see its an array within a array...but that only happened because I used <?php print_r($teams); ?> like Jack suggested...
I think you're misunderstanding the function print_r(). It doesn't manipulate your array in any way. It just prints the structure of it, which is why you should use it when debugging.
From what I see though, all you have to do to fix your problem is to turn:
<td><?php echo $teams['team']['Team Name']; ?></td>
into
<td><?php echo $teams['Team']['Team Name']; ?></td>.
But that isn't really going to help you if you don't understand why... so here goes:
You had a multi-dimensional associative array called $teams.
The first value of this array is called $teams['Teams'], but you were using $teams['teams'] to access it.
You then used print_r() to find out that you had to capitalize the index of your $teams array. This is because array keys are case sensitive.
As far as I can tell, that's your entire problem summed up...
#15
Re: Problem: Undefined Index
Posted 26 April 2012 - 12:51 PM
Thanks Cbeppe! Unfortunately this fix didnt help. Im still getting the undefined index notice!
|
|

New Topic/Question
Reply




MultiQuote










|