PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Looping through fields in a row
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Looping through fields in a row

Réponse
 
LinkBack Outils de la discussion
Vieux 13/09/2007, 15h28   #1
stacey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Looping through fields in a row

Hi All,

I am trying to display a mysql record on the screen. I would rather
not use specific field names in case the fields change, etc. So, I
just want to create a simple table with the field names down the first
column and the corresponding values in the second. I've tried several
different snippets of code that I found, but I can't seem to get it
working right.

This is what I've been working off of:

$fieldNames=array_keys($myrow);

But this array have every other value as, what seems to be, a row id.
Looks like this:

Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
[5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
business_city)

Any suggestions would be appreciated.

  Réponse avec citation
Vieux 13/09/2007, 15h44   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Looping through fields in a row

On 13 Sep, 15:28, stacey <monkeym...@gmail.com> wrote:
> Hi All,
>
> I am trying to display a mysql record on the screen. I would rather
> not use specific field names in case the fields change, etc. So, I
> just want to create a simple table with the field names down the first
> column and the corresponding values in the second. I've tried several
> different snippets of code that I found, but I can't seem to get it
> working right.
>
> This is what I've been working off of:
>
> $fieldNames=array_keys($myrow);
>
> But this array have every other value as, what seems to be, a row id.
> Looks like this:
>
> Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> business_city)
>
> Any suggestions would be appreciated.


use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus

$rows = mysql_fetch_assoc($res);
foreach ($rows[0] as $field_name => $field_value)
echo "<tr><td>{$field_name}</td><td>{$field_value}</td></tr>";

  Réponse avec citation
Vieux 14/09/2007, 13h16   #3
C.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Looping through fields in a row

On 13 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 13 Sep, 15:28, stacey <monkeym...@gmail.com> wrote:
>
>
>
> > Hi All,

>
> > I am trying to display a mysql record on the screen. I would rather
> > not use specific field names in case the fields change, etc. So, I
> > just want to create a simple table with the field names down the first
> > column and the corresponding values in the second. I've tried several
> > different snippets of code that I found, but I can't seem to get it
> > working right.

>
> > This is what I've been working off of:

>
> > $fieldNames=array_keys($myrow);

>
> > But this array have every other value as, what seems to be, a row id.
> > Looks like this:

>
> > Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> > [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> > business_city)

>
> > Any suggestions would be appreciated.

>
> use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus
>
> $rows = mysql_fetch_assoc($res);
> foreach ($rows[0] as $field_name => $field_value)
> echo "<tr><td>{$field_name}</td><td>{$field_value}</td></tr>";


Thanks for playing Captain, unfortunately the correct answer was:

while ($row = mysql_fetch_assoc($res)) {
foreach ($row as $name => $val) {
echo "<tr><td>{$name}</td><td>{$val}</td></tr>\n";
}
}

C.

  Réponse avec citation
Vieux 14/09/2007, 14h42   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Looping through fields in a row

On Thu, 13 Sep 2007 16:28:15 +0200, stacey <monkeymynd@gmail.com> wrote:

> Hi All,
>
> I am trying to display a mysql record on the screen. I would rather
> not use specific field names in case the fields change, etc. So, I
> just want to create a simple table with the field names down the first
> column and the corresponding values in the second. I've tried several
> different snippets of code that I found, but I can't seem to get it
> working right.
>
> This is what I've been working off of:
>
> $fieldNames=array_keys($myrow);
>
> But this array have every other value as, what seems to be, a row id.
> Looks like this:
>
> Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4]=> 2
> [5] => business_name [6] => 3 [7] => business_address [8] => 4[9] =>
> business_city)


Don't fetch the result with mysql_fetch_array(), use mysql_fetch_assoc()
instead. mysql_fetch_array() will return both a numerical as named array
by default.
--
Rik Wasmus
  Réponse avec citation
Vieux 14/09/2007, 15h38   #5
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Looping through fields in a row

On 13 Sep, 15:44, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 13 Sep, 15:28, stacey <monkeym...@gmail.com> wrote:
>
>
>
>
>
> > Hi All,

>
> > I am trying to display a mysql record on the screen. I would rather
> > not use specific field names in case the fields change, etc. So, I
> > just want to create a simple table with the field names down the first
> > column and the corresponding values in the second. I've tried several
> > different snippets of code that I found, but I can't seem to get it
> > working right.

>
> > This is what I've been working off of:

>
> > $fieldNames=array_keys($myrow);

>
> > But this array have every other value as, what seems to be, a row id.
> > Looks like this:

>
> > Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> > [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> > business_city)

>
> > Any suggestions would be appreciated.

>
> use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus
>
> $rows = mysql_fetch_assoc($res);
> foreach ($rows[0] as $field_name => $field_value)
> echo "<tr><td>{$field_name}</td><td>{$field_value}</td></tr>";- Hide quoted text -
>
> - Show quoted text -


Yes of course it was!

I have spent so long using mysql through a framework, that I totally
forgot what the raw functions do!

  Réponse avec citation
Vieux 15/09/2007, 00h15   #6
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Looping through fields in a row

Rik Wasmus wrote:
> On Thu, 13 Sep 2007 16:28:15 +0200, stacey <monkeymynd@gmail.com> wrote:
>
>> Hi All,
>>
>> I am trying to display a mysql record on the screen. I would rather
>> not use specific field names in case the fields change, etc. So, I
>> just want to create a simple table with the field names down the first
>> column and the corresponding values in the second. I've tried several
>> different snippets of code that I found, but I can't seem to get it
>> working right.
>>
>> This is what I've been working off of:
>>
>> $fieldNames=array_keys($myrow);
>>
>> But this array have every other value as, what seems to be, a row id.
>> Looks like this:
>>
>> Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
>> [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
>> business_city)

>
> Don't fetch the result with mysql_fetch_array(), use mysql_fetch_assoc()
> instead. mysql_fetch_array() will return both a numerical as named array
> by default.
> --Rik Wasmus


Or just:

mysql_fetch_array($result,MYSQL_ASSOC); // MYSQL_ASSOC, MYSQL_NUM,
MYSQL_BOTH are allowed. Default is MYSQL_BOTH.

Norm
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h13.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12936 seconds with 14 queries