PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 21st, 2010, 03:44 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
[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.

Reply With Quote
  #2  
Old December 21st, 2010, 04:47 AM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,490 srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 19 h 21 m 45 sec
Reputation Power: 831
Quote:
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?

Reply With Quote
  #3  
Old December 21st, 2010, 06:49 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
Sorry don't know why that didn't copy over editing now.

Reply With Quote
  #4  
Old December 21st, 2010, 08:17 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
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.

Reply With Quote
  #5  
Old December 21st, 2010, 08:58 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
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.

Reply With Quote
  #6  
Old December 21st, 2010, 10:45 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
So then echo the query you're running in Task->__construct($taskID);

Reply With Quote
  #7  
Old December 22nd, 2010, 07:42 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
Quote:
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

Reply With Quote
  #8  
Old December 22nd, 2010, 09:13 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
Objects are assigned by reference, take the $task out and assign directly:
PHP Code:
 $tasks[] = new Task($taskID); 
-Dan

Reply With Quote
  #9  
Old December 22nd, 2010, 09:23 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
Quote:
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

Reply With Quote
  #10  
Old December 22nd, 2010, 09:31 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
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

Reply With Quote
  #11  
Old December 22nd, 2010, 10:14 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 21 m 39 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
What's the code for Task?

Reply With Quote
  #12  
Old December 22nd, 2010, 10:16 AM
angrypanda's Avatar
angrypanda angrypanda is offline
Why so angry?
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2004
Posts: 1,939 angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)angrypanda User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 40558 Folding Title: Beginner FolderFolding Points: 40558 Folding Title: Beginner FolderFolding Points: 40558 Folding Title: Beginner Folder
Time spent in forums: 1 Month 4 Days 6 h 6 m 13 sec
Reputation Power: 897
edit somehow I edited panda's post instead of replying to it, he originally said something insightful which included:
Quote:
for all we know $val->getInfo() outputs the most recently created ID or something.
Sorry Panda.
__________________
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.


Last edited by ManiacDan : December 28th, 2010 at 01:04 PM.

Reply With Quote
  #13  
Old December 23rd, 2010, 05:18 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
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

Reply With Quote
  #14  
Old December 27th, 2010, 04:34 AM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,490 srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level)srisa User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 19 h 21 m 45 sec
Reputation Power: 831
php Code:
Original - php Code
  1. public function getInfo() {
  2.     global $task;
  3.     return $task;
  4. }   

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

Reply With Quote
  #15  
Old December 27th, 2010, 05:06 AM
alex905 alex905 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 193 alex905 User rank is Private First Class (20 - 50 Reputation Level)alex905 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 21 h 1 m 53 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > [PHP5] Adding objects to a array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap