[PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Baloo :0\)

How can I assign automatically all fields of a database to a variable of
the same name?

Instead of having to manually do
$user_id=$row[user_id];
etc

Then how could I know the number of fields in the table so I can do a
loop to print them all in html?

In advance, thanks for your help.

Alfredo


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Rick Emery

This isn't exactly what you want, but it is close:

list($user_id,$field2,$field3,$field4) = $row;

where $row was retrieved via mysql_fetch_array()
Just list each of the variable names in the list() function; include ALL
fields' names included in $row
-Original Message-
From: Baloo :0) [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 19, 2002 3:41 PM
To: [EMAIL PROTECTED]
Subject: [PHP] explode? (table field to a variable of same name)


How can I assign automatically all fields of a database to a variable of
the same name?

Instead of having to manually do
$user_id=$row[user_id];
etc

Then how could I know the number of fields in the table so I can do a
loop to print them all in html?

In advance, thanks for your help.

Alfredo


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Simon Willison

First grab an associative array of the variables from the database with 
mysql_fetch_array()

Then use extract($array); to extract all of the variables in the array 
to the symbol table:

www.php.net/extract

Simon

Baloo :0) wrote:

How can I assign automatically all fields of a database to a variable of
the same name?

Instead of having to manually do
$user_id=$row[user_id];
etc

Then how could I know the number of fields in the table so I can do a
loop to print them all in html?

In advance, thanks for your help.

Alfredo






-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Christopher William Wesley

This may not be what you want to do, but should give you some hints.
(This is my code which I use to simply dump any SQL table into an HTML
 table I can view in a browser ... for small tables, of course.)

Using MySQL as an example:
// assuming you ran a query and stored results in $mysql_result_set

// Get the number of fields to work with
$num_fields = mysql_num_fields( $mysql_result_set );
// http://www.php.net/manual/en/function.mysql-num-fields.php

// Print out a row of column headers
print( tr );
for( $i = 0; $i  $num_fields; $i++ ){
$fieldName = mysql_fetch_field( $mysql_result_set, $i );
// http://www.php.net/manual/en/function.mysql-fetch-field.php
print( td . $fieldName-name . /td );
}
print( /tr );

// Print out the data from the result records
while( $record = mysql_fetch_row( $mysql_result_set ) ){
// http://www.php.net/manual/en/function.mysql-fetch-row.php
print( tr );
foreach( $record as $field ){
${$field} = $field;
// The above is unnecessary, but answers your question.
// It assigns a variable, named after a table column,
//the value of that column (in this record).
print( td${field}/td );
}
print( tr );
}

Hope that gives you something to work with.

g.luck,
~Chris

On Tue, 19 Feb 2002, Baloo :0) wrote:

 How can I assign automatically all fields of a database to a variable of
 the same name?

 Instead of having to manually do
 $user_id=$row[user_id];
 etc

 Then how could I know the number of fields in the table so I can do a
 loop to print them all in html?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Rasmus Lerdorf

This creates all the variables you asked for and also prints each one in a
column of a table.

foreach($row as $key=$val) {
$$key = $val;
echo td$val/td;
}

-Rasmus

On Tue, 19 Feb 2002, Baloo :0) wrote:

 How can I assign automatically all fields of a database to a variable of
 the same name?

 Instead of having to manually do
 $user_id=$row[user_id];
 etc

 Then how could I know the number of fields in the table so I can do a
 loop to print them all in html?

 In advance, thanks for your help.

 Alfredo


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] explode? (table field to a variable of same name)

2002-02-19 Thread Christopher William Wesley

oi ... typo!  see below.  sorry :(

~Chris

On Tue, 19 Feb 2002, Christopher William Wesley wrote:

 This may not be what you want to do, but should give you some hints.
 (This is my code which I use to simply dump any SQL table into an HTML
  table I can view in a browser ... for small tables, of course.)

 Using MySQL as an example:
 // assuming you ran a query and stored results in $mysql_result_set

 // Get the number of fields to work with
 $num_fields = mysql_num_fields( $mysql_result_set );
 // http://www.php.net/manual/en/function.mysql-num-fields.php

 // Print out a row of column headers
 print( tr );
 for( $i = 0; $i  $num_fields; $i++ ){
   $fieldName = mysql_fetch_field( $mysql_result_set, $i );
   // http://www.php.net/manual/en/function.mysql-fetch-field.php
   print( td . $fieldName-name . /td );
 }
 print( /tr );

 // Print out the data from the result records
 while( $record = mysql_fetch_row( $mysql_result_set ) ){
 // http://www.php.net/manual/en/function.mysql-fetch-row.php
   print( tr );

I screwed it up here ... this foreach() loop should be as follows

   foreach( $record as $fieldName=$field ){
   ${$fieldName} = $field;
   // The above is unnecessary, but answers your question.
   // It assigns a variable, named after a table column,
   //the value of that column (in this record).
   print( td${field}/td );
   }
   print( tr );
 }

Again ... sorry about that.  Oi ... and I see that someone else, wrote
that bit of code correctly to the list just now :)  (thx, Rasmus).


 Hope that gives you something to work with.

   g.luck,
 ~Chris

 On Tue, 19 Feb 2002, Baloo :0) wrote:

  How can I assign automatically all fields of a database to a variable of
  the same name?
 
  Instead of having to manually do
  $user_id=$row[user_id];
  etc
 
  Then how could I know the number of fields in the table so I can do a
  loop to print them all in html?





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php