Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9

    [PHP5] Adding objects to a array


    Hi

    Here is the code I am haveing trouble with:

    PHP Code:
    public function listAssosiatedTasks(){
        global 
    $conn$changeRequest;
        
    $tasks = array();
        
    $task;
        
    $conn = &ADONewConnection('oci8');
        
    // Attempt connection
        
    if ($c $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID)){
            
    $s $conn->Prepare("SELECT task_id
            , status
            , owner
            , date_created
            , action_to
            , due_date
                                                FROM task
                                                WHERE crid = :crid"
    );
                        
    $conn->InParameter($s,$changeRequest['CRID'],"crid");
            
    // Execute the query
            
    if ($rs = @$conn->Execute($s)){
                while(
    $arr $rs->FetchRow()){
                                    
    $taskID $arr['TASK_ID'];
                    
    $task = new Task($taskID);
                    
    $tasks[] = $task;
                }
                return 
    $tasks;
            }
        }

    what it returns is a array containing copies of the last object to be added. so if i was looping though 3 task objects i would have a array of 3 objects all of which would be the last object repeated. I have added a var dump to the loop and each time a object is added it overwrites (it seems) the other objects in the array.
    Last edited by alex905; December 21st, 2010 at 06:58 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2006
    Location
    I'm sneaking up behind you.
    Posts
    1,490
    Rep Power
    832
    Code:
    ....
    $task = new Task($taskID); 
    ...........
    What is $taskID and where is it coming from?
    Why do we always seek someone, something or some thought? Are we afraid of ourselves?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    Sorry don't know why that didn't copy over editing now.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Debugging this on your own will be trivial. Never assume a variable is what you think it is. Echo $taskID inside the loop, see if you're getting 3 different ones.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    Yeah I did that I check all the variables at diffrent stanges in this function and the array(in each repition of the loop and outside of it). The task id is unique every time.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    So then echo the query you're running in Task->__construct($taskID);
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    Originally Posted by ManiacDan
    So then echo the query you're running in Task->__construct($taskID);
    About time I posted up some debugging info I think.

    Here is the function with with a echo for the task id:

    PHP Code:
    public function listAssosiatedTasks(){
        global 
    $conn$changeRequest;
        
    $tasks = array();
        
    $task;
        
    $conn = &ADONewConnection('oci8');
        
    // Attempt connection
        
    if ($c $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID)){
            
    $s $conn->Prepare("SELECT task_id
                                               , status
                                               , owner
                                               , date_created
                                               , action_to
                                               , due_date
                                            FROM task
                                            WHERE object_table_ref = :crid
                          AND object_table_name = 'change_req'"
    );
            
    $conn->InParameter($s,$changeRequest['CHANGE_REQ_ID'],"crid");
            
    // Execute the query
            
    if ($rs $conn->Execute($s)){
                while(
    $arr $rs->FetchRow()){
                    
    $taskID $arr['TASK_ID'];
                    echo 
    $taskID."<br />";
                    
    $task = new Task($taskID);
                    
    $tasks[] = $task;
                }
                return 
    $tasks;
            }
        }

    Here is the output:

    1640
    1641
    1642

    so far so good the tasks its getting from the database are correct.

    So onto creating a Task object for each id.

    I will retrieve the objects id before it goes into the array with a
    echo.

    Here is the effected code:

    PHP Code:
    $task = new Task($taskID);
    $taskInfo $task->getInfo();
    echo 
    $taskInfo['TASK_ID'];
    $tasks[] = $task
    The result is:

    1640
    1641
    1642

    Ok so this show that for this bit of functionality (all we need to diagnose this) everything is working in the task class.

    Ok now I have the task object it needs to go into the array. I will echo out the above but while in the loop on each iteration.

    Here is my code now:

    PHP Code:
        public function listAssosiatedTasks(){
            global 
    $conn$changeRequest;
            
    $tasks = array();
            
    $task;
            
    $conn = &ADONewConnection('oci8');
            
    // Attempt connection
            
    if ($c $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID)){
                
    $s $conn->Prepare("SELECT task_id
                                           , status
                                           , owner
                                           , date_created
                                           , action_to
                                           , due_date
                                        FROM task
                                        WHERE object_table_ref = :crid
                      AND object_table_name = 'change_req'"
    );
                
    $conn->InParameter($s,$changeRequest['CHANGE_REQ_ID'],"crid");
                
    // Execute the query
                
    if ($rs $conn->Execute($s)){
                    while(
    $arr $rs->FetchRow()){
                        
    $taskID $arr['TASK_ID'];
                        
    //echo $taskID."<br />";
                        
    $task = new Task($taskID);
                        
    //$taskInfo = $task->getInfo();
                        //echo $taskInfo['TASK_ID']."<br />";
                        
    $tasks[] = $task;
                        foreach(
    $tasks as $r => $val){
                            
    $me $val->getInfo();
                            echo 
    $me['TASK_ID']."<br />";
                        }
                        echo 
    "end loop<br />";
                    }
                    return 
    $tasks;
                }
            }
        } 
    So now we can see whats happening on each run of the loop.

    Here it is:

    1640
    end loop
    1641
    1641
    end loop
    1642
    1642
    1642
    end loop

    very weird

    Any ideas or tests you think might be use full would be appreciated
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Objects are assigned by reference, take the $task out and assign directly:
    PHP Code:
    $tasks[] = new Task($taskID); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    Originally Posted by ManiacDan
    Objects are assigned by reference, take the $task out and assign directly:
    PHP Code:
    $tasks[] = new Task($taskID); 
    -Dan
    Just tried that and got the same result.

    1640
    end loop
    1641
    1641
    end loop
    1642
    1642
    1642
    end loop
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Change:
    //echo $taskID."<br />";
    To
    PHP Code:
    echo "Creating new task: " $taskID."<br />"
    That will show us what it's creating, and then what the array contains.

    There must be some kind of reference problem with your code. Try doing:
    PHP Code:
                        $taskID $arr['TASK_ID'];
                        echo 
    "Creating new task: " $taskID."<br />"
                        
    $task = new Task($taskID);
                        
    $tasks[] = $task
                        unset(
    $task); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
  22. #12
  23. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    edit somehow I edited panda's post instead of replying to it, he originally said something insightful which included:
    for all we know $val->getInfo() outputs the most recently created ID or something.
    Sorry Panda.
    Last edited by ManiacDan; December 28th, 2010 at 01:04 PM.
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    PHP Code:
    function Task($id null) {
        global 
    $conn$task;
        
    $conn ADONewConnection('oci8');
        
    $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID);
        if (!
    $id == null){
            
    $s $conn->Prepare("SELECT task_id
                                  , object_table_name
                                  , object_table_key
                                  , object_table_ref
                                  , description                             
                                            , status
                                            , owner
                                  , action_to
                                  , due_date
                                  , date_created
                                        FROM task
                                        WHERE task_id = :id"
    );
            
    $conn->InParameter($s,$id,"id");
            
    // Execute the query
            
    if ($rs $conn->Execute($s)){
                
    $task $rs->FetchRow();
                
    //var_dump($task);
                
    return true;
            }else{
                return 
    false;
            }
        }
        return 
    true;

    That's the constructor

    PHP Code:
    public function getInfo() {
        global 
    $task;
        return 
    $task;

    and getInfo

    PHP Code:
    public function listAssosiatedTasks(){
            global 
    $conn$changeRequest;
            
    $tasks = array();
            
    $task;
            
    $conn = &ADONewConnection('oci8');
            
    // Attempt connection
            
    if ($c $conn->Connect(CCC_DB_HOST,CCC_DB_SCHEMA,CCC_DB_PW,CCC_DB_SID)){
                
    $s $conn->Prepare("SELECT task_id
                                           , status
                                           , owner
                                           , date_created
                                           , action_to
                                           , due_date
                                        FROM task
                                        WHERE object_table_ref = :crid
                      AND object_table_name = 'change_req'"
    );
                
    $conn->InParameter($s,$changeRequest['CHANGE_REQ_ID'],"crid");
                
    // Execute the query
                
    if ($rs $conn->Execute($s)){
                    while(
    $arr $rs->FetchRow()){
                     
    $taskID $arr['TASK_ID'];
             echo 
    "Creating new task: " $taskID."<br />"
             
    $task = new Task($taskID);
             
    $tasks[] = $task
             unset(
    $task);  
                        foreach(
    $tasks as $r => $val){
                            
    $me $val->getInfo();
                            echo 
    $me['TASK_ID']."<br />";
                        }
                        echo 
    "end loop<br />";
                    }
                    return 
    $tasks;
                }
            }
        } 
    and the suggested changes implemented

    Here are the results:

    Creating new task: 1640
    1640
    end loop
    Creating new task: 1641
    1641
    1641
    end loop
    Creating new task: 1642
    1642
    1642
    1642
    end loop
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2006
    Location
    I'm sneaking up behind you.
    Posts
    1,490
    Rep Power
    832
    php Code:
    public function getInfo() {
        global $task;
        return $task;
    }

    The problem part is the "global $task; ". When you are creating a task, you are doing it like so,
    Code:
     $task = new Task($taskID);
    And in your getInfo function, you are declaring $task as global and returning it. So, the most recently created Task object will be returned and that is reflected in the output.

    Some other things:
    - It is not a great idea to connect to the database in every single function that has to query the database. Just connect once and reuse that connection.
    - The purpose of OOP is to get rid of this "global" business.

    Comments on this post

    • angrypanda agrees
    • alex905 agrees : First one to spot it :) Thanks
    Why do we always seek someone, something or some thought? Are we afraid of ourselves?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    193
    Rep Power
    9
    Not sure I got this right. The global task is in the task class and so is the function that I posted at the top of my last post. In fact its the constructor (yep I just learnt its the wrong way to declare it) I include the global in the function so that when I retrieve the details of task it can be placed in the global. The other function I have (where I create new instances of task) is not in the same file as the task class.

    Have I missed something on how globals work? I am new to php.

    Can't get to the code again till jan 5th or so.

    But I will look over what I posed and see if i can figure out what you meant.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo