| View previous topic :: View next topic |
| Author |
Message |
alex
Joined: 13 Sep 2006 Posts: 72
|
Posted: Tue Sep 18, 2007 9:53 am Post subject: [Solved] Datagrid,getColumns,setColumn:references PHP4vsPHP5 |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Sep 18, 2007 10:12 am Post subject: |
|
|
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 |
|
 |
alex
Joined: 13 Sep 2006 Posts: 72
|
Posted: Tue Sep 18, 2007 11:10 am Post subject: |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Sep 18, 2007 11:18 am Post subject: |
|
|
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 |
|
 |
alex
Joined: 13 Sep 2006 Posts: 72
|
Posted: Tue Sep 18, 2007 1:02 pm Post subject: |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Sep 18, 2007 1:19 pm Post subject: |
|
|
| 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 |
|
 |
alex
Joined: 13 Sep 2006 Posts: 72
|
Posted: Tue Sep 18, 2007 3:32 pm Post subject: |
|
|
| Ok thanks ! |
|
| Back to top |
|
 |
|