Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

field value through variable name 1

Status
Not open for further replies.

zianole

Programmer
Jan 9, 2007
16
How can i access a field value through variable name

for example
if id is database table column then through

$row->id;

i can get its value but if i want to use a variable like

$var="id";
$row->anyfunction($var);
is there a way to to this?

 
PHP supports something called "variable variables" and they can, at least with PHP 5, be used with object instantiations, too.

This script:

Code:
<?php

class foo
{
	var $bar = 'baz';
}

$a = new foo();

$b = 'bar';

print $a->{$b};
?>

Outputs:

[tt]baz[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks sleipnir214!
I tested, its working in my php version.

Can I ask, how can i loop all variables (properties)?

I tried following code but an error raised

for($b=0; $b<=5; $b++;){
echo $a->{$b};
}

instead of providing name ($b = 'bar';) I tried to use number ($b = 1;) but php raised an error on ($a->{$b};)

thanks






 
I doubt what you are trying to do will work, simply because [tt]$1[/tt] is not a valid PHP label. The PHP manual entry which introduces variables reads, in part:
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I'd do something like...

$query = "SELECT id, whatever";
$result = mysql_query($query) or die( mysql_error());

$i=0;
while ($rows = mysql_fetch_row($result)){
$id[$i] = $rows[0];
$i++;
}

You may be using a different database, or you may use...
$i=0;
foreach ($rows as $row){
$id[$i] = $row['id'];
$i++;
}

Play with it and see what works best.

Mark
 
Actually i was looking for to loop every column (field) of feched database row without directly pointing the name of field. for example in above Kozusnik example,

$i=0;
foreach ($rows as $row){
$id[$i] = $row['id'];
$i++;
}

Is it possible to loop every field of every row (nested loop).

$i=0;
foreach ($rows as $row){
// assumed
// somethin like this
foreach ($column in $row){
echo $row[$column];
$i++;
}
}

and output should be each column value of each row. a tabular form.


I think I am unable to explain my problem. I write it in different way.

I have a query.
$query = "SELECT * From table12";

Now I want to display all rows and values of table2, but I don't know exactly how many columns/fields (and their names) are in table2




 
I would do something like:

$a = mysql_query("SELECT * FROM some_table");

while ($row = mysql_fetch_assoc($a))
{
foreach ($row as $value)
{
print $value;
print "\t";
}
print "\r\n";
}


You can easily change this snippet to output the necessary HTML tags to format the output in a table.

You could also, between the mysql_fetch_assoc() invocation the the beginning of the while-loop add an initial row fetch and loop to output column headers that match column names:

$a = mysql_query(".....");

$row = mysql_fetch_assoc();
foreach ($row as $columnname => $columnvalue)
{
print $columnname;
print "\t";
}
print "\r\n";

foreach ($row as $value)
{
print $value;
print "\t";
}

while (......)
.
.
.

Again, it's not too hard to hang the output of HTML tags to format this as an HTML table.



Want the best answers? Ask the best questions! TANSTAAFL!
 
thanks again sleipnir214!
That was the actual solution i was looking for. and Sorry! I could not explain actual problem in start of thread.

also thanks Kozusnik! his example helped me, how to put values of single column in array, for all rows.


Zianole
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top