Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with unexpected T_VAR

    I'm having the following error every time that I run this code:
    Parse error: parse error, unexpected T_VAR in /home/yeddish/public_html/mindmap/mindmapTree.php on line 135
    I'm not an amateur coder, but I am kinda new to PHP, so it's probably something obvious.

    Also, I cut out a big chunk of comments in the beginning.

    Here's the code:
    PHP Code:
    001, <?php
    030
    // Set up DB connection here
    031$username="----";
    032$password="-----";
    033$database="yeddish_mindmap";
    034//$array[];
    035$depth 0;
    036
    037mysql_connect("localhost"$username$password)
    038,     or die ('I cannot connect to the database because: ' mysql_error());
    039mysql_select_db($database)
    040,     or die ("Unable to select database");
    041
    042// Query to grab all the data and toss it into $result
    043//    with the number of nodes in $numRows
    044$query   'SELECT * FROM nodes ORDER BY id';
    045$result  mysql_query($query);
    046$numRows mysql_numrows($result);    // Um... Gets the number of rows from the SQL query result, duh.
    047
    048// Now iterate the results and toss it into an array temporarily for easy access
    049, for ($i=0$i $numRows$i++) {
    050
    051,     // Turn each item into a PHP variable
    052,     $id       mysql_result($result,$i,"id");
    053,     $name     mysql_result($result,$i,"name");
    054,     $link     mysql_result($result,$i,"link");
    055,     $parental mysql_result($result,$i,"parental");
    056,     $child1   mysql_result($result,$i,"child1");
    057,     $child2   mysql_result($result,$i,"child2");
    058,     $child3   mysql_result($result,$i,"child3");
    059,     $child4   mysql_result($result,$i,"child4");
    060,     $child5   mysql_result($result,$i,"child5");
    061,     
    062,     $array[i] = new ArrayNode($id$name$link$parental$child1$child2$child3$child4$child5);
    063, }
    064
    065// Terminate DB connection
    066mysql_close();
    067
    068// Put the data into XML here
    069$xml_output "<?xml version=\"1.0\"?>\n";    // Make Flash play nice!
    070
    071// Create the XMl tree with a (nerdy) recursive algorithm
    072makeNode(0);
    073
    074// Send the data back to Flash here and we be done, homie.
    075, echo $xml_output;
    076
    077, function makeNode($id) {
    078
    079,     $xml_output .= indent() . "<node children=\"" $array[$id]->numChildren() . "\">\n";
    080,     $depth++;            // Let indent() know how much to indent by
    081,     $xml_output .= indent() . "<name>" $array[$id]->getName() . "</name>\n";
    082,     $xml_output .= indent() . "<link>" $array[$id]->getLink() . "</link>\n";
    083
    084,     // For each child, run this same function on them
    085,     for ($tempNumChildren $array[$id]->numChildren(); $tempNumChildren 0$tempNumChildren--) {
    086,         if      ($tempNumChildren 5) { makeNode($array[$array[$id]->getChild5()]) ; }
    087,         else if ($tempNumChildren 4) { makeNode($array[$array[$id]->getChild4()]) ; }
    088,         else if ($tempNumChildren 3) { makeNode($array[$array[$id]->getChild3()]) ; }
    089,         else if ($tempNumChildren 2) { makeNode($array[$array[$id]->getChild2()]) ; }
    090,         else if ($tempNumChildren 1) { makeNode($array[$array[$id]->getChild1()]) ; }
    091,     }
    092
    093,     $depth--;            // Done indenting
    094,     $xml_output .= indent() . "</node>\n";
    095, }
    096
    097, function indent() {
    098,     //var $output .= "";
    099,     
    100,     for ($tempDepth $this->depth$tempDepth 0$tempDepth--) { 
    101,         $output .= "\t";
    102,     }
    103,     
    104,     return($output);
    105, }
    106?>
    107, 
    108, <?php
    109
    , class ArrayNode
    110
    , {
    111,   var $id;
    112,   var $name;
    113,   var $link;
    114,   var $parental;
    115,   var $child1;
    116,   var $child2;
    117,   var $child3;
    118,   var $child4;
    119,   var $child5;
    120
    121,   // Constructors
    122,   function Node ($id$name$link$parental$child1$child2$child3$child4$child5)  {
    123,         $this->id       $id;
    124,         $this->name     $name;
    125,         $this->link     $link;
    126,         $this->parental $parental;
    127,         $this->child1   $child1;
    128,         $this->child2   $child2;
    129,         $this->child3   $child3;
    130,         $this->child4   $child4;
    131,         $this->child5   $child5;
    132,   }
    133
    134,   function numChildren() {
    135,         var $j 0;
    136,         if ($this->child1 >= 1) { $j++ }
    137,         if ($this->child2 >= 1) { $j++ }
    138,         if ($this->child3 >= 1) { $j++ }
    139,         if ($this->child4 >= 1) { $j++ }
    140,         if ($this->child5 >= 1) { $j++ }
    141,         
    142,         return($j);
    143,   }
    144,   
    145,  
    146,   // Getters
    147,   function getID() {
    148,         return($this->id);
    149,   }
    150
    151,   function getName() {
    152,         return($this->name);
    153,   }
    154
    155,   function getLink() {
    156,         return($this->link);
    157,   }
    158
    159,   function getparental() {
    160,         return($this->parental);
    161,   }
    162
    163,   function getChild1() {
    164,         return($this->child1);
    165,   }
    166,   
    167,   function getChild2() {
    168,         return($this->child2);
    169,   }
    170,   
    171,   function getChild3() {
    172,         return($this->child3);
    173,   }
    174,   
    175,   function getChild4() {
    176,         return($this->child4);
    177,   }
    178,   
    179,   function getChild5() {
    180,         return($this->child5);
    181,   }
    182, }
    183
    184
    185
    186?>
    Thanks for any help.
    -yeddish
    Last edited by yeddish; 09-02-2006 at 06:13 PM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I think but not sure that vars need to be declared outside of your functions in the class. So on line 135 you have
    PHP Code:
    var $j 0
    change that to
    PHP Code:
    $j 0
    and where you have the rest of your vars after you being your class add
    PHP Code:
    var $j
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you're right about not being able to use var inside class methods, but from the code, I don't think $j wants to be a class method, so just remove 'var'

  • #4
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys, I'll try that out and post my results.

    That's one of those lang-specific quirks that I wasn't aware of.

    -yeddish

  • #5
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK. Those suggestions took care of that problem, but now I get another error that's even more odd. This kindof amkes me think that there's some underlying issue. It kinda smells like a left out bracket or quote, but I checked the whole thing.

    Here's the error:
    Fatal error: Call to a member function on a non-object in /home/yeddish/public_html/mindmap/mindmapTree.php on line 74
    And, here's the new code:
    PHP Code:
    001:  <?php
    023
    :  
    024:  // Set up DB connection here
    025:  $username="----";
    026:  $password="------";
    027:  $database="yeddish_mindmap";
    028:  //$array[];
    029:  $depth 0;
    030:  $output;
    031:  
    032:  mysql_connect("localhost"$username$password)
    033:      or die ('I cannot connect to the database because: ' mysql_error());
    034:  mysql_select_db($database)
    035:      or die ("Unable to select database");
    036:  
    037:  // Query to grab all the data and toss it into $result
    038:  //    with the number of nodes in $numRows
    039:  $query   'SELECT * FROM nodes ORDER BY id';
    040:  $result  mysql_query($query);
    041:  $numRows mysql_numrows($result);    // Um... Gets the number of rows from the SQL query result, duh.
    042:  
    043:  // Now iterate the results and toss it into an array temporarily for easy access
    044:  for ($i=0$i $numRows$i++) {
    045:  
    046:      // Turn each item into a PHP variable
    047:      $id       mysql_result($result,$i,"id");
    048:      $name     mysql_result($result,$i,"name");
    049:      $link     mysql_result($result,$i,"link");
    050:      $parental mysql_result($result,$i,"parent");
    051:      $child1   mysql_result($result,$i,"child1");
    052:      $child2   mysql_result($result,$i,"child2");
    053:      $child3   mysql_result($result,$i,"child3");
    054:      $child4   mysql_result($result,$i,"child4");
    055:      $child5   mysql_result($result,$i,"child5");
    056:      
    057:      $array[i] = new ArrayNode($id$name$link$parental$child1$child2$child3$child4$child5);
    058:  }
    059:  
    060:  // Terminate DB connection
    061:  mysql_close();
    062:  
    063:  // Put the data into XML here
    064:  $xml_output "<?xml version=\"1.0\"?>\n";    // Make Flash play nice!
    065:  
    066:  // Create the XMl tree with a (nerdy) recursive algorithm
    067:  makeNode(0);
    068:  
    069:  // Send the data back to Flash here and we be done, homie.
    070:  echo $xml_output;
    071:  
    072:  function makeNode($id) {
    073:  
    074:      $xml_output .= indent() . "<node children=\"" $array[$id]->numChildren() . "\">\n";
    075:      $depth++;            // Let indent() know how much to indent by
    076:      $xml_output .= indent() . "<name>" $array[$id]->getName() . "</name>\n";
    077:      $xml_output .= indent() . "<link>" $array[$id]->getLink() . "</link>\n";
    078:  
    079:      // For each child, run this same function on them
    080:      for ($tempNumChildren $array[$id]->numChildren(); $tempNumChildren 0$tempNumChildren--) {
    081:          if      ($tempNumChildren 5) { makeNode($array[$array[$id]->getChild5()]) ; }
    082:          else if ($tempNumChildren 4) { makeNode($array[$array[$id]->getChild4()]) ; }
    083:          else if ($tempNumChildren 3) { makeNode($array[$array[$id]->getChild3()]) ; }
    084:          else if ($tempNumChildren 2) { makeNode($array[$array[$id]->getChild2()]) ; }
    085:          else if ($tempNumChildren 1) { makeNode($array[$array[$id]->getChild1()]) ; }
    086:      }
    087:  
    088:      $depth--;            // Done indenting
    089:      $xml_output .= indent() . "</node>\n";
    090:  }
    091:  
    092:  function indent() {
    093:      $output "";
    094:      
    095:      for ($tempDepth $this->depth$tempDepth 0$tempDepth--) { 
    096:          $output .= "\t";
    097:      }
    098:      
    099:      return($output);
    100:  }
    101:  ?>
    102:  
    103:  <?php
    104
    :  class ArrayNode
    105
    :  {
    106:    var $id;
    107:    var $name;
    108:    var $link;
    109:    var $parental;
    110:    var $child1;
    111:    var $child2;
    112:    var $child3;
    113:    var $child4;
    114:    var $child5;
    115:    var $j;
    116:  
    117:    // Constructors
    118:    function Node ($id$name$link$parental$child1$child2$child3$child4$child5)  {
    119:          $this->id       $id;
    120:          $this->name     $name;
    121:          $this->link     $link;
    122:          $this->parental $parental;
    123:          $this->child1   $child1;
    124:          $this->child2   $child2;
    125:          $this->child3   $child3;
    126:          $this->child4   $child4;
    127:          $this->child5   $child5;
    128:    }
    129:  
    130:    function numChildren() {
    131:          $j 0;
    132:          if ($this->child1 >= 1) { $j++; }
    133:          if ($this->child2 >= 1) { $j++; }
    134:          if ($this->child3 >= 1) { $j++; }
    135:          if ($this->child4 >= 1) { $j++; }
    136:          if ($this->child5 >= 1) { $j++; }
    137:          
    138:          return($j);
    139:    }
    140:    
    141:   
    142:    // Getters
    143:    function getID() {
    144:          return($this->id);
    145:    }
    146:  
    147:    function getName() {
    148:          return($this->name);
    149:    }
    150:  
    151:    function getLink() {
    152:          return($this->link);
    153:    }
    154:  
    155:    function getparental() {
    156:          return($this->parental);
    157:    }
    158:  
    159:    function getChild1() {
    160:          return($this->child1);
    161:    }
    162:    
    163:    function getChild2() {
    164:          return($this->child2);
    165:    }
    166:    
    167:    function getChild3() {
    168:          return($this->child3);
    169:    }
    170:    
    171:    function getChild4() {
    172:          return($this->child4);
    173:    }
    174:    
    175:    function getChild5() {
    176:          return($this->child5);
    177:    }
    178:  }
    179:  
    180:  
    181:  
    182:  ?>
    Thanks again for the help!

    -yeddish
    Last edited by yeddish; 09-02-2006 at 06:14 PM.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    "array" is a reserved word.

    http://us3.php.net/manual/en/reserved.php

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    well it is, but he's using $array, which is fine.

    PHP Code:
     function makeNode($id) {
    073:  
    074:      $xml_output .= indent() . "<node children=\"" $array[$id]->numChildren() . "\">\n";
    075:      $depth++;            // Let indent() know how much to indent by 
    inside the function, $array hasn't been defined, so you're calling numChildren() on nothing, hence the error. You probably want to pass $array along with $id to the function

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's true... but it messes with my brain! I like good descriptive variable names! <pouts>

  • #9
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Y'know...I was thinking that after I posted and I tried passing $array into the function and the exact same error message popped up.

    I'll go ahead and do that and post up again.

    Thanks for the responses!
    -yeddish

  • #10
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok. here's the code with $array bring passed into the recursive function.

    I still get this error:
    Fatal error: Call to a member function on a non-object in /home/yeddish/public_html/mindmap/mindmapTree.php on line 74
    PHP Code:
    001:  <?php
    023
    :  
    024:  // Set up DB connection here
    025:  $username="yeddish_mindmap";
    026:  $password="mindmap1";
    027:  $database="yeddish_mindmap";
    028:  $array;
    029:  $depth 0;
    030:  $output;
    031:  
    032:  mysql_connect("localhost"$username$password)
    033:      or die ('I cannot connect to the database because: ' mysql_error());
    034:  mysql_select_db($database)
    035:      or die ("Unable to select database");
    036:  
    037:  // Query to grab all the data and toss it into $result
    038:  //    with the number of nodes in $numRows
    039:  $query   'SELECT * FROM nodes ORDER BY id';
    040:  $result  mysql_query($query);
    041:  $numRows mysql_numrows($result);    // Um... Gets the number of rows from the SQL query result, duh.
    042:  
    043:  // Now iterate the results and toss it into an array temporarily for easy access
    044:  for ($i=0$i $numRows$i++) {
    045:  
    046:      // Turn each item into a PHP variable
    047:      $id       mysql_result($result,$i,"id");
    048:      $name     mysql_result($result,$i,"name");
    049:      $link     mysql_result($result,$i,"link");
    050:      $parental mysql_result($result,$i,"parent");
    051:      $child1   mysql_result($result,$i,"child1");
    052:      $child2   mysql_result($result,$i,"child2");
    053:      $child3   mysql_result($result,$i,"child3");
    054:      $child4   mysql_result($result,$i,"child4");
    055:      $child5   mysql_result($result,$i,"child5");
    056:      
    057:      $array[i] = new ArrayNode($id$name$link$parental$child1$child2$child3$child4$child5);
    058:  }
    059:  
    060:  // Terminate DB connection
    061:  mysql_close();
    062:  
    063:  // Put the data into XML here
    064:  $xml_output "<?xml version=\"1.0\"?>\n";    // Make Flash play nice!
    065:  
    066:  // Create the XMl tree with a (nerdy) recursive algorithm
    067:  makeNode(0$array);
    068:  
    069:  // Send the data back to Flash here and we be done, homie.
    070:  echo $xml_output;
    071:  
    072:  function makeNode($id$array) {
    073:  
    074:      $xml_output .= indent() . "<node children=\"" $array[$id]->numChildren() . "\">\n";
    075:      $depth++;            // Let indent() know how much to indent by
    076:      $xml_output .= indent() . "<name>" $array[$id]->getName() . "</name>\n";
    077:      $xml_output .= indent() . "<link>" $array[$id]->getLink() . "</link>\n";
    078:  
    079:      // For each child, run this same function on them
    080:      for ($tempNumChildren $array[$id]->numChildren(); $tempNumChildren 0$tempNumChildren--) {
    081:          if      ($tempNumChildren 5) { makeNode($array[$array[$id]->getChild5()], $array) ; }
    082:          else if ($tempNumChildren 4) { makeNode($array[$array[$id]->getChild4()], $array) ; }
    083:          else if ($tempNumChildren 3) { makeNode($array[$array[$id]->getChild3()], $array) ; }
    084:          else if ($tempNumChildren 2) { makeNode($array[$array[$id]->getChild2()], $array) ; }
    085:          else if ($tempNumChildren 1) { makeNode($array[$array[$id]->getChild1()], $array) ; }
    086:      }
    087:  
    088:      $depth--;            // Done indenting
    089:      $xml_output .= indent() . "</node>\n";
    090:  }
    091:  
    092:  function indent() {
    093:      $output "";
    094:      
    095:      for ($tempDepth $this->depth$tempDepth 0$tempDepth--) { 
    096:          $output .= "\t";
    097:      }
    098:      
    099:      return($output);
    100:  }
    101:  ?>
    102:  
    103:  <?php
    104
    :  class ArrayNode
    105
    :  {
    106:    var $id;
    107:    var $name;
    108:    var $link;
    109:    var $parental;
    110:    var $child1;
    111:    var $child2;
    112:    var $child3;
    113:    var $child4;
    114:    var $child5;
    115:    var $j;
    116:  
    117:    // Constructors
    118:    function Node ($id$name$link$parental$child1$child2$child3$child4$child5)  {
    119:          $this->id       $id;
    120:          $this->name     $name;
    121:          $this->link     $link;
    122:          $this->parental $parental;
    123:          $this->child1   $child1;
    124:          $this->child2   $child2;
    125:          $this->child3   $child3;
    126:          $this->child4   $child4;
    127:          $this->child5   $child5;
    128:    }
    129:  
    130:    function numChildren() {
    131:          $j 0;
    132:          if ($this->child1 >= 1) { $j++; }
    133:          if ($this->child2 >= 1) { $j++; }
    134:          if ($this->child3 >= 1) { $j++; }
    135:          if ($this->child4 >= 1) { $j++; }
    136:          if ($this->child5 >= 1) { $j++; }
    137:          
    138:          return($j);
    139:    }
    140:    
    141:   
    142:    // Getters
    143:    function getID() {
    144:          return($this->id);
    145:    }
    146:  
    147:    function getName() {
    148:          return($this->name);
    149:    }
    150:  
    151:    function getLink() {
    152:          return($this->link);
    153:    }
    154:  
    155:    function getparental() {
    156:          return($this->parental);
    157:    }
    158:  
    159:    function getChild1() {
    160:          return($this->child1);
    161:    }
    162:    
    163:    function getChild2() {
    164:          return($this->child2);
    165:    }
    166:    
    167:    function getChild3() {
    168:          return($this->child3);
    169:    }
    170:    
    171:    function getChild4() {
    172:          return($this->child4);
    173:    }
    174:    
    175:    function getChild5() {
    176:          return($this->child5);
    177:    }
    178:  }
    179:  
    180:  
    181:  
    182:  ?>
    Thanks, guys. I really do appreciate this.
    -yeddish

  • #11
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    when you're adding to $array, you're using $array[i], when you mean $array[$i]

    (if you were displaying notice-level errors, this would have been pointed out to you...)

  • #12
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hehehhe. Thanks. I'm a bonehead.
    I'm not getting any more errors, but I'm also not getting any output except for the XML declaration line. I'll have to look into this some more, but you got me past the frustrating part.

    Thanks again.
    -yeddish

  • #13
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice...
    It's finally working great!

    You guys rock!
    -yeddish


  •  

    Posting Permissions

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