0

I want to create an object of class inside for loop but i can't figure out what wrong i am doing. This is my select function.

   public function admin_select_all ($table, $rows = '*', $where = null, $order = null)
{
   if(empty($table))
   {
    return false;
   }

   $q = 'SELECT '.$rows.' FROM '.$table;
    if($where != null)
        $q .= ' WHERE '.$where;
    if($order != null)
        $q .= ' ORDER BY '.$order;

    if($this->admin_tableExists($table))
   {
    $stmt = $this->admin_connection->prepare($q);
    $stmt->execute();
    $results = $stmt->fetchAll();
    if( (!$results) or (empty($results)) ) { 
        return false;
    }
    return $results;
   }


}

This is a constructor of class admin_element

    public function __construct($itemId)
{
    global $db_operate;
    $info = $db_operate->admin_select_all ("my_table" ,"id=".$itemId);
    $this->id = $info[0]['id'];
    $this->title = $info[0]['title'];
    $this->seoUrl = $info[0]['seo_url'];
    $this->parentId = $info[0]['parent_id'];
    $this->isMenuItem = $info[0]['menu_item'];
    $this->order = $info[0]['menu_order'];
    $this->htmlId = $info[0]['anchor_unique_html_id'];
}

//This is the function inside admin_element where i want to create an object inside for loop

    public static function getRootItems()
{
    global $db_operate;

    $parents = $db_operate->admin_select_all ("my_table");
    $listOfItems = array();
    for($i=0;$i<count($parents);$i++)
    {
        $listOfItems[$i] = new admin_element($parents[$i]['id']);
    }
    //var_dump($listOfItems);
    return $listOfItems;
   }
}

Now when i create an object with this then i am getting the error undefined id in the constructor, and when i var_dump the array then i get the error's equal to the total length $parent which is my returned array. I don't know what wrong i am doing . I also try it with foreach but still no success.

//With foreach i try it but not able to create and object

foreach($parents as $row) {
        $listOfItems[] = array(

        'id' => $row['id']


        );

    }

Here when i var_dump the $listOfItems array then it's working well but when i try to instantiate the object then it also fail please some help.If this question is asked before i apologize.

8
  • $q .= ' WHERE '.$where; Don't you know Little Booby Drop Table? Commented Nov 2, 2015 at 20:51
  • I know that check the function parameter's the $where is coming from there Commented Nov 2, 2015 at 20:52
  • You have a syntax error with id=".$itemId in $info = $db->select("my_table" ,id=".$itemId); Commented Nov 2, 2015 at 20:58
  • in foreach you can create object same way (without $i only). Show us full error code Commented Nov 2, 2015 at 20:59
  • What does var_dump($info) show? Commented Nov 2, 2015 at 21:04

1 Answer 1

3

Try to replace this:

$listOfItems = array();
for($i=0;$i<count($parents);$i++)
{
    $listOfItems[$i] = new itrom_element($parents[$i]['id']);
}

With this:

foreach($parents as $parent)
    $listOfItems[]= new itrom_element($parent['id']);
Sign up to request clarification or add additional context in comments.

3 Comments

he didnt mention, that there is a syntax error, so its propably editing problem
No that's not the problem, while pasting my question here i was changing some table names etc so the double quote remove mistakenly. That's ok i have edited it you can check it now
Main question is how can i create an object of the class inside of the for or foreach loop and what i am doing is it worng or irihgt..some suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.