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 
[Solved] Datagrid,getColumns,setColumn:references PHP4vsPHP5

 
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: Tue Sep 18, 2007 9:53 am    Post subject: [Solved] Datagrid,getColumns,setColumn:references PHP4vsPHP5 Reply with quote

Hi !

I want to change the name of my column from "id_user" to "#" with the following code. It works in PHP5 but fails in PHP4. I think it is a reference problem. I wish it could work in PHP4 as getColumns returns a array of references to the columns :

Code:
// Instantiate the DataGrid
      $datagrid =& new Structures_DataGrid();
      
      // Setup your database connection
      $options = array('dsn' => 'mysql://root:@localhost/mydb');
      
      // Bind a basic SQL statement as datasource
      $test = $datagrid->bind('SELECT * FROM users', $options);
      if (PEAR::isError($test)) {
          echo $test->getMessage();
      }
      $datagrid->addColumn(new Structures_DataGrid_Column('id_user', 'id_user', null, array('width' => '40'), null));
      
      foreach($datagrid->getColumns() as $column)
      {
         $column->setLabel('#');
      }
      

      // Create a HTML_Table
      $table = new HTML_Table();
      $test = $datagrid->fill($table);
      if (PEAR::isError($test)) {
         echo $test->getMessage();
      }

      // Output table and paging links
      echo $table->toHtml();

      // Display paging links
      $test = $datagrid->render(DATAGRID_RENDER_PAGER);
      if (PEAR::isError($test)) {
         echo $test->getMessage();
      }


This is a example code. My final aim is to "strip_tags()" (strip HTML tags) from the header ("<br />" e.g.) like this :

Code:
foreach($datagrid->getColumns() as $column)
{
   $column->setLabel(strip_tags($column->getLabel()));
}


If you can tell me what is wrong ... Thanks.


Last edited by alex on Tue Sep 18, 2007 1:04 pm; edited 1 time in total
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1051

PostPosted: Tue Sep 18, 2007 10:12 am    Post subject: Reply with quote

Without having read everything of your post, I guess that this is the solution:

Code:
foreach($datagrid->getColumns() as &$column)


(note the "&" before $column)
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Tue Sep 18, 2007 11:10 am    Post subject: Reply with quote

Thanks Mark.

I had ever tested this but it returns a parse error for the modified line :

Quote:
Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in ...
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1051

PostPosted: Tue Sep 18, 2007 11:18 am    Post subject: Reply with quote

Ah, this is only valid in PHP 5 ...

Maybe a normal for loop would help you? This is also the way getColumns() works internally.
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Tue Sep 18, 2007 1:02 pm    Post subject: Reply with quote

Thanks Mark ; this works :


Code:
$a = $datagrid->getColumns();
for($i=0;$i<count($a);$i++)
{
    $a[$i]->setLabel('#');
}


Do you know what the difference between two codes (foreach vs for) ?

I think internaly "foreach" loop makes a implicit (if PHP4) copy of the object instead using a reference, correct ?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1051

PostPosted: Tue Sep 18, 2007 1:19 pm    Post subject: Reply with quote

alex wrote:
I think internaly "foreach" loop makes a implicit (if PHP4) copy of the object instead using a reference, correct ?


Right. And if you modify the copy, the original object won't change. That's why I first had the "&" in mind (but wasn't aware that this was new in PHP 5).
Back to top
View user's profile Send private message
alex



Joined: 13 Sep 2006
Posts: 72

PostPosted: Tue Sep 18, 2007 3:32 pm    Post subject: Reply with quote

Ok thanks !
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