CodeIgniter Forums
Error: Cannot add element Title number 1 when only 0 such elements exist - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Error: Cannot add element Title number 1 when only 0 such elements exist (/thread-33636.html)



Error: Cannot add element Title number 1 when only 0 such elements exist - El Forum - 09-02-2010

[eluser]mabright[/eluser]
I am looping through a multidimensional array and I receive error "Cannot add element Title number 1 when only 0 such elements exist" when referencing elements.

Here is the array data:
Code:
Array ( [0] => Array ( [Title] => SimpleXMLElement Object ( [0] => Bay 101 Banquet Facilities ) [Address] => SimpleXMLElement Object ( [0] => 1801 Bering Dr ) [City] => SimpleXMLElement Object ( [0] => San Jose ) [State] => SimpleXMLElement Object ( [0] => CA ) [Phone] => SimpleXMLElement Object ( [0] => (408) 451-8888 ) [Latitude] => SimpleXMLElement Object ( [0] => 37.373044 ) [Longitude] => SimpleXMLElement Object ( [0] => -121.912125 ) [Rating] => SimpleXMLElement Object ( [AverageRating] => 4 [TotalRatings] => 2 [TotalReviews] => 2 [LastReviewDate] => 1200040214 [LastReviewIntro] => If youre looking for high limit action Bay 101 is the place to go in San Jose. You can regularly find limit games up to $80-$160 in their spacious high ceiling room. ) [Distance] => SimpleXMLElement Object ( [0] => 4.13 ) [Url] => SimpleXMLElement Object ( [0] => http://local.yahoo.com/info-21599986-bay-101-banquet-facilities-san-jose ) [ClickUrl] => SimpleXMLElement Object ( [0] => http://local.yahoo.com/info-21599986-bay-101-banquet-facilities-san-jose ) [MapUrl] => SimpleXMLElement Object ( [0] => http://maps.yahoo.com/maps_result?q1=1801+Bering+Dr+San+Jose+CA&gid1=21599986 ) [BusinessUrl] => SimpleXMLElement Object ( [0] => http://bay101.com/ ) [BusinessClickUrl] => SimpleXMLElement Object ( [0] => http://bay101.com/ ) [Categories] => SimpleXMLElement Object ( [Category] => Casinos ) ) [1] => Array ( [Title] => SimpleXMLElement Object ( [0] => Palace ) [Address] => SimpleXMLElement Object ( [0] => 22821 Mission Blvd ) [City] => SimpleXMLElement Object ( [0] => Hayward ) [State] => SimpleXMLElement Object ( [0] => CA ) [Phone] => SimpleXMLElement Object ( [0] => (510) 582-1166 ) [Latitude] => SimpleXMLElement Object ( [0] => 37.669642 ) [Longitude] => SimpleXMLElement Object ( [0] => -122.081868 ) [Rating] => SimpleXMLElement Object ( [AverageRating] => NaN [TotalRatings] => 0 [TotalReviews] => 0 [LastReviewDate] => SimpleXMLElement Object ( ) [LastReviewIntro] => SimpleXMLElement Object ( ) ) [Distance] => SimpleXMLElement Object ( [0] => 19.40 ) [Url] => SimpleXMLElement Object ( [0] => blah ) [ClickUrl] => SimpleXMLElement Object ( [0] => blahblah ) [MapUrl] => SimpleXMLElement Object ( [0] => http://maps.yahoo.com/maps_result?q1=22821+Mission+Blvd+Hayward+CA&gid1=21429152 ) [Categories] => SimpleXMLElement Object ( [Category] => Casinos ) ) [2] => Array ( [Title] => SimpleXMLElement Object ( [0] => Lucky Buck Card Club ) [Address] => SimpleXMLElement Object ( [0] => 1620 Railroad Ave ) [City] => SimpleXMLElement Object ( [0] => Livermore ) [State] => SimpleXMLElement Object ( [0] => CA ) [Phone] => SimpleXMLElement Object ( [0] => (925) 455-6144 ) [Latitude] => SimpleXMLElement Object ( [0] => 37.68132 ) [Longitude] => SimpleXMLElement Object ( [0] => -121.776778 ) [Rating] => SimpleXMLElement Object ( [AverageRating] => 3.5 [TotalRatings] => 4 [TotalReviews] => 4 [LastReviewDate] => 1274219444 [LastReviewIntro] => might have been a good place but no owned by john parks and his casinos are soley money.. no personality and very generic. but if you like 101 casino and the other up in sac then its your place.. if you want some indivisualism and a local typpe card room dont go there. ) [Distance] => SimpleXMLElement Object ( [0] => 18.48 ) [Url] => SimpleXMLElement Object ( [0] => http://local.yahoo.com/info-21442484-lucky-buck-card-club-livermore ) [ClickUrl] => SimpleXMLElement Object ( [0] => http://local.yahoo.com/info-21442484-lucky-buck-card-club-livermore ) [MapUrl] => SimpleXMLElement Object ( [0] => http://maps.yahoo.com/maps_result?q1=1620+Railroad+Ave+Livermore+CA&gid1=21442484 ) [BusinessUrl] => SimpleXMLElement Object ( [0] => http://theluckybuck.com/ ) [BusinessClickUrl] => SimpleXMLElement Object ( [0] => http://theluckybuck.com/ ) [Categories] => SimpleXMLElement Object ( [Category] => Casinos ) ) )

Here is the code from my view
Code:
<?php
    $zebra_id = '';
    $i = 0;    
    foreach($venues as $venue)
    {
        echo '<tr>
                    <td scope="col" id="$zebra_id">'.$venue['Title'][$i].'</td>
                    </tr>';
        $i++;
  }
?&gt;



Error: Cannot add element Title number 1 when only 0 such elements exist - El Forum - 09-02-2010

[eluser]danmontgomery[/eluser]
You only ever have one Title element, why are you incrementing $i?

Also, $venue['Title'] is a simplexmlelement object, not an array, and you can't access integers as object members, eg:

Code:
class my_class {
    var $1 = 'foo';  // Will throw an error

    function my_class() {
        echo $this->1; // As will this
    }
}

You should be using simplexml functions like children(), I believe...

Code:
$node = $venue['Title']->children();
echo $node[0];

http://php.net/manual/en/book.simplexml.php


Error: Cannot add element Title number 1 when only 0 such elements exist - El Forum - 09-02-2010

[eluser]mabright[/eluser]
I must be losing it, I got it working using $venue['<element_name>'], i.e. $venue['address']. I thought I tried this and failed but I guess I didn't.

SOLVED...