PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Structures_DataGrid - One fetch for 2 columns

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Structures
View previous topic :: View next topic  
Author Message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Mon Jul 16, 2007 4:39 pm    Post subject: Structures_DataGrid - One fetch for 2 columns Reply with quote

I use a callback function for a column (A). This function fetches 2 fields in the DB and return the first field to be printed in the column (A).
The problem is I need to add another column (B) with the second field. I could do it with another call back function but I will then fetch again for nothing (I would loose time of execution) as the result has ever been fetched. Is this possible to set the result in the callback function then use it in another one without need of fetching ? Or something similar ? I wanted to use references but I don't know if it is a good idea and args are in an array ...
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Mon Jul 16, 2007 7:20 pm    Post subject: Reply with quote

I had to read three times to get your idea, but then I realized that I also had the problem some time ago. Wink

There is nothing implemented in SDG for such cases, and I can't think of anything that we could do here as a reasonable implementation.

My solution is to store the data that I fetch (in one formatter callback) in a property of a class that I already have in use. Identied by the ID value of the record, I can access the fetched value also in two other formatter callbacks.

Not the best approach maybe, but accessing the database multiple times also isn't the best approach.
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Wed Jul 18, 2007 11:32 am    Post subject: Reply with quote

Thanks Mark.
I'm afraid not to understand how you proceed ... sorry :\

Here are 2 formatters feching the same data, can you show me how you could save the fetched results in formatter1 ($list var) and reuse it in the formatter2 please ? (Note I code in PHP4)

Code:
function formatter1($p)
{
     $list = array();
     $temp = array();
     $myobject = new object;
     $myobject->fk_id = $p['record']['id'];
     $myobject->find();
     while($myobject->fetch())
     {
         $list[] = $myobject;
         $temp[]= $myobject->item1.' '.$myobject->item2;
     }
     return implode('<br />', $temp);
}

function formatter2($p)
{
     //How to reuse $list var of formatter1() ?
     $temp = array();
     $myobject = new object;
     $myobject->fk_id = $p['record']['id'];
     $myobject->find();
     while($myobject->fetch())
     {
         $temp[]= $myobject->item3.' '.$myobject->item4;
     }
     return implode('<br />', $temp);
}
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Jul 18, 2007 11:50 am    Post subject: Reply with quote

In your case, you could define $list outside of your formatters, and then use
Code:
global $list;
in both formatters. In the first formatter, you would fill $list, in the second formatter you could access the values of $list.

In my code (and that is what I wrote about) I use a class, and put something like $list into a property of the class.
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Wed Jul 18, 2007 12:09 pm    Post subject: Reply with quote

Oh yes Mark, I thought about references but forgot global vars even if I know it is not "good" to use them. But I still don't understand your code with the class ... Is this class global too ?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Jul 18, 2007 12:57 pm    Post subject: Reply with quote

alex wrote:
Oh yes Mark, I thought about references but forgot global vars even if I know it is not "good" to use them. But I still don't understand your code with the class ... Is this class global too ?


The class could be global, yes. But back to your problem: If you use the IDs of the records as keys in $list, you can access the right value in the other formatter.
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Wed Jul 18, 2007 3:27 pm    Post subject: Reply with quote

Quote:
The class could be global

Do you mean it could be not global ? So how would you access its properties if it is not global ?

Quote:
If you use the IDs of the records as keys in $list, you can access the right value in the other formatter.

In fact I don't use the IDs of the records as keys in $list. I think it uses less ressource to empty the $list var in the first formatter ( => beginning of a new row) like this :

Code:
//... Formatter 1 to n using same "fetch" ...

function formatter_1($p)
{
     global $list;
     $list = array(); //empty for new current row
     $temp = array();
     $myobject = new object;
     $myobject->fk_id = $p['record']['id'];
     $myobject->find();
     while($myobject->fetch())
     {
         $list[] = $myobject;
         $temp[]= $myobject->item1.' '.$myobject->item2;
     }
     return implode('<br />', $temp);
}

//...

function formatter_n($p)
{
     global $list; //Reuse the $list fetched for the current row
     $temp = array();
      foreach($list as $myobject)
     {
         $temp[]= $myobject->item3.' '.$myobject->item4;
     }
     return implode('<br />', $temp);
}


What do you think of that ? Is it a good approach ?

Using IDs did you mean that :

Code:
//... Formatter 1 to n using same "fetch" ...

function formatter_1($p)
{
     global $list;
     $temp = array();
     $myobject = new object;
     $myobject->fk_id = $p['record']['id'];
     $myobject->find();
     while($myobject->fetch())
     {
         $list[$p['record']['id']][] = $myobject;
         $temp[]= $myobject->item1.' '.$myobject->item2;
     }
     return implode('<br />', $temp);
}

//...

function formatter_n($p)
{
     global $list;
     $temp = array();
     foreach($list[$p['record']['id']] as $myobject)
     {
         $temp[]= $myobject->item3.' '.$myobject->item4;
     }
     return implode('<br />', $temp);
}
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Jul 18, 2007 3:53 pm    Post subject: Reply with quote

The class does not need to be global, one can also pass it via the parameters of the formatter.

Anyway, I got your while() loop in the formatter a little bit wrong, and we were probably writing about different things, sorry. Your first example from the last post (<= the one I'm replying to) should work in the expected way, doesn't it?
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Wed Jul 18, 2007 4:45 pm    Post subject: Reply with quote

Quote:
The class does not need to be global


Well then I don't know how you can save the property value as the following code "doesn't know" $test :

Code:
$test = new stdClass;
$test->s= 'Hello World';

function formatter()
{
     echo 'Value : '.$test->s;
}



Quote:
, one can also pass it via the parameters of the formatter.


Well, second parameter of a formatter is an array of args. One should pass it by reference to keep the fetched thing. But how, as args are in an array ?

Quote:
Your first example from the last post (<= the one I'm replying to) should work in the expected way, doesn't it?


Yes it works with global but I am interested by the class approach.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Jul 18, 2007 6:13 pm    Post subject: Reply with quote

alex wrote:
Quote:
The class does not need to be global


Well then I don't know how you can save the property value as the following code "doesn't know" $test :

Code:
$test = new stdClass;
$test->s= 'Hello World';

function formatter()
{
     echo 'Value : '.$test->s;
}


If you use
Code:

function formatter($params, $args = array())

here, then you can pass via the array that you define as the last parameter of the column constructor a reference to your class, which will be available in $args['whatever'].
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Tue Jul 31, 2007 7:07 pm    Post subject: Reply with quote

Thanks Mark ; I will try to pass the class property in this array. I just wonder if the value of the property will be changed outside of the formatter if it is not passed as reference ...
I am in PHP4 and objects are not pass by reference as in PHP5 I think.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Tue Jul 31, 2007 8:29 pm    Post subject: Reply with quote

alex wrote:
Thanks Mark ; I will try to pass the class property in this array. I just wonder if the value of the property will be changed outside of the formatter if it is not passed as reference ...
I am in PHP4 and objects are not pass by reference as in PHP5 I think.


You can force the reference in PHP 4, e.g.:
$array['index'] =& $object;
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Structures All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software