Re: Getting fieldname and value from assoc array?
| From: | Steve Lawson | Date: | Sat, 07 Oct 2000 11:52:58 +0000 |
| Subject: | Re: Getting fieldname and value from assoc array? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19024@lists.php.net to get a copy of this message | ||
Yo!
Did you use each() with list()?
Here is an example:
<?php
$bleh["a"] = "a whatever";
$bleh["b"] = "b whatever";
$bleh["c"] = "c whatever";
$bleh["d"] = "d whatever";
$bleh["e"] = "e whatever";
reset ($bleh);
while(list($key, $val) = each($bleh))
{
echo "$key => $val<br>";
}
?>
The output is:
a => a whatever
b => b whatever
c => c whatever
d => d whatever
e => e whatever
With a query you would just need a nested while...like so.
while($row = mysql_fetch_array($result))
{
reset ($row); // just to be safe
while(list($key, $val) = each($row))
{
displayRow($key , $val);
}
}
Hope that helped!
SL.
----- Original Message -----
From: "Vincent - D. Ertner" <vincent@eec.de>
To: "php" <php-general@lists.php.net>
Sent: Saturday, October 07, 2000 12:08 AM
Subject: [PHP] Getting fieldname and value from assoc array?
> Hi php,
>
> I put a result into an associative array ... now I need to get
> the value and the fieldname from a function without using implode
> or anything.
>
> As far as I understood assoc arrays, after retrieving it via
> mysql_fetch_array($result, MYSQL_ASSOC) it looks something like
> this:
>
> "Fieldname1", "Value1"
> "Fieldname2", "Value2"
> etc.
>
> Thus, I thought something like $assoc[x][y] would work ...
> suddenly recognizing that $assoc[x] delivers the values, but
> $assoc[x][0] delivers only the first character.
>
> Then I fiddled around with key() ... no success.
>
> Finally I tried something like
>
> for ($i=0; $i<count($detail); $i++) {
> $field = mysql_fetch_field($result, $i);
> $value = $detail[$i];
> $form->displayText($field->name, $value);
> echo "<br>";
> }
>
> That doesn't really work either ... list($key, $val) does the
> same as $assoc[x] ... it ought to it, I guess ;-)
>
> BTW: The purpose of the whole thingy is to get a single record
> from a database and to display it row by row with looking up some
> values in additional tables (therefore a function is needed).
>
> The function will look something like this:
>
> function displayRow($row, $table="") {
> echo "<tr>";
> echo "<td valign=top>[$i] -
> ".$row[0]."</td>";
> echo "<td>[".$row[1]."]</td>";
> echo "</tr>\n";
> MySql::lookup($field, $table);
> }
>
> So, to put a long story short: How do I get a result from mySql
> in a form, where I can easily pass key and value pairs to a
> function without using two methods (s. a.)?
>
> Any help is highly appreciated!
>
> Cheers,
>
> Vince
>
> '''
> ô¿ô
> -
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>