[eluser]pser[/eluser]
Hi,
I have a challenge which I can find a solution for so I thought posting here might help. I use PHP and codeigniter and this is what i am trying to do.
I run a query towards a database and have the result in a array like this:
[records] => Array (
[0] => stdClass Object (
[OPID] => 5
[OPName] => Red Hat
[Created] => Oct 7 2009 1:51PM )
[1] => stdClass Object (
[OPID] => 3
[OPName] => Windows 7
[Created] => Oct 7 2009 12:00AM )
[2] => stdClass Object (
[OPID] => 4
[OPName] => Windows Server 2003
[Created] => Oct 7 2009 11:17AM )
)
I then use a foreach loop going through the array, so to typical write for example OPID I would write like this
foreach $records as $row
echo $row->OPID;
endforeach;
so far so good and this is working. But I would like to reuse the code and get it more dynamic that is I would love to write something like this:
foreach $records as $row
echo $row[$field]; // something echo $row->$field;
endforeach;
In this case the $field would contain 'OPID' which I get from a $_POST in one example and 'APID' in another, all depending of what data the user do.
So my question, is it possible to have a dynamic value in a $row field like this to get it dynamic?
In the mean time I've found a temporary solution by using reset($row) to get the first element of the array, then next($row) to get the next. I can do this because I know that the unique identifier is always first. But I would prefer to have a better solution.
The reason why I do it is because I want to reuse the code on several tables and then in the link which the user click specify which table to fetch data from. In that case I have to code less webpages, but of course it is getting a more tricky.
Any suggestions are welcome :-)
Cheers
Paal
Cheers
Paal