| Author |
Message |
Jerry Stuckle *nix forums Guru
Joined: 24 Feb 2005
Posts: 1515
|
Posted: Thu Jul 20, 2006 4:59 pm Post subject:
Re: converting array $row values to $string
|
|
|
Rik wrote:
| Quote: | Jerry Stuckle wrote:
list($name, $address1, $address2) = mysql_fetch_array($result);
Better:
list($name, $address1, $address2) = mysql_fetch_row($result);
Grtz,
|
True.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
================== |
|
| Back to top |
|
 |
Rik *nix forums Guru Wannabe
Joined: 16 Nov 2005
Posts: 291
|
Posted: Thu Jul 20, 2006 2:12 pm Post subject:
Re: converting array $row values to $string
|
|
|
Jerry Stuckle wrote:
| Quote: | list($name, $address1, $address2) = mysql_fetch_array($result);
|
Better:
list($name, $address1, $address2) = mysql_fetch_row($result);
Grtz,
--
Rik Wasmus |
|
| Back to top |
|
 |
Jerry Stuckle *nix forums Guru
Joined: 24 Feb 2005
Posts: 1515
|
Posted: Thu Jul 20, 2006 12:33 pm Post subject:
Re: converting array $row values to $string
|
|
|
monomaniac21 wrote:
| Quote: | hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc
|
Why not just
list($name, $address1, $address2) = mysql_fetch_array($result);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
================== |
|
| Back to top |
|
 |
Markus Ernst *nix forums addict
Joined: 22 Feb 2005
Posts: 61
|
Posted: Thu Jul 20, 2006 11:41 am Post subject:
Re: converting array $row values to $string
|
|
|
monomaniac21 schrieb:
| Quote: | Cheers Nyoka I'm sure it does work.
but i dont understand it. what is the double $$ for and "global"?
|
manual_exists() == true!
www.php.net/global
www.php.net/variables
 |
|
| Back to top |
|
 |
Kimmo Laine *nix forums Guru Wannabe
Joined: 25 Mar 2005
Posts: 209
|
Posted: Thu Jul 20, 2006 11:37 am Post subject:
Re: converting array $row values to $string
|
|
|
"Hans 'pritaeas' Pollaerts" <pritaeas@hotmail.com> wrote in message
news:e9nmo0$6un$1@news3.zwoll1.ov.home.nl...
| Quote: | Maybe this works (untested):
$row = mysql_fetch_array($result);
foreach ($row as $name => $value) {
$eval = "${$name} = $value";
eval ($eval);
}
|
No reason to get eval involved.
This:
foreach($row as $name => $value)
$$name = $value;
works just fine.
--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg) |
|
| Back to top |
|
 |
Kimmo Laine *nix forums Guru Wannabe
Joined: 25 Mar 2005
Posts: 209
|
Posted: Thu Jul 20, 2006 11:34 am Post subject:
Re: converting array $row values to $string
|
|
|
"monomaniac21" <mcyi2mr3@googlemail.com> wrote in message
news:1153393402.467503.199960@i42g2000cwa.googlegroups.com...
| Quote: | What i cant see is how the function finds out the name given to each
row of the array?
Can anyone explain this to me?
|
It's using a feature of php called variable variable.
let's say you have a couple of variables:
$name = "fred flintstone";
$pointer = "name";
You can point to variable $name with the $pointer using the variable
variable syntax: $$pointer is evaluated as $"name", and since $pointer
contains the string "name".
echo $$pointer; // This will print "fred flintstone"
The very same mechanisms is used in the example function.
--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg) |
|
| Back to top |
|
 |
ImOk *nix forums beginner
Joined: 07 Jul 2006
Posts: 29
|
Posted: Thu Jul 20, 2006 11:25 am Post subject:
Re: converting array $row values to $string
|
|
|
After you do the fetch run
extract($row);
This will cretae memory variables of the same name as the keys and
containing the values.
Study the example from docs:
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
monomaniac21 wrote:
| Quote: | hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc |
|
|
| Back to top |
|
 |
monomaniac21 *nix forums addict
Joined: 03 Sep 2005
Posts: 70
|
Posted: Thu Jul 20, 2006 11:03 am Post subject:
Re: converting array $row values to $string
|
|
|
What i cant see is how the function finds out the name given to each
row of the array?
Can anyone explain this to me?
monomaniac21 wrote:
| Quote: | Cheers Nyoka I'm sure it does work.
but i dont understand it. what is the double $$ for and "global"?
:-)
Nyoka wrote:
You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}
Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.
monomaniac21 wrote:
hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc |
|
|
| Back to top |
|
 |
monomaniac21 *nix forums addict
Joined: 03 Sep 2005
Posts: 70
|
Posted: Thu Jul 20, 2006 10:45 am Post subject:
Re: converting array $row values to $string
|
|
|
Cheers Nyoka I'm sure it does work.
but i dont understand it. what is the double $$ for and "global"?
:-)
Nyoka wrote:
| Quote: | You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}
Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.
monomaniac21 wrote:
hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc |
|
|
| Back to top |
|
 |
Nyoka *nix forums beginner
Joined: 20 Jul 2006
Posts: 2
|
Posted: Thu Jul 20, 2006 10:42 am Post subject:
Re: converting array $row values to $string
|
|
|
You could write your own function similar to:
function convertArray ($array) {
foreach ($array AS $name => $value) {
global $$name;
$$name = $value;
}
}
Make sure you do a print_r ($array) before using this so you are away
of what variables are going to be used.
monomaniac21 wrote:
| Quote: | hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc |
|
|
| Back to top |
|
 |
Hans 'pritaeas' Pollaerts *nix forums beginner
Joined: 18 Jul 2006
Posts: 3
|
Posted: Thu Jul 20, 2006 10:38 am Post subject:
Re: converting array $row values to $string
|
|
|
Maybe this works (untested):
$row = mysql_fetch_array($result);
foreach ($row as $name => $value) {
$eval = "${$name} = $value";
eval ($eval);
}
"monomaniac21" <mcyi2mr3@googlemail.com> wrote in message
news:1153390678.605323.145130@h48g2000cwc.googlegroups.com...
| Quote: | hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc
|
|
|
| Back to top |
|
 |
monomaniac21 *nix forums addict
Joined: 03 Sep 2005
Posts: 70
|
Posted: Thu Jul 20, 2006 10:17 am Post subject:
converting array $row values to $string
|
|
|
hi all
is there a function to convert all values of array $row into seperate
variables:
$row = mysql_fetch_array($result);
// i dont want to have to do this anymore :-)
$name = $row['name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
etc etc etc
Regards
marc |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|