Results 1 to 6 of 6

Thread: list/each [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    list/each [Resolved]

    I'm using the following:
    PHP Code:
    while(list($key) = each($row))
            {
                echo 
    $key "<br>";
            } 
    I'm just trying to get the titles of the columns in the table (I'm building a web-based query tool). However, when I echo, I get the following results:
    0
    Field0
    1
    Field1
    2
    Field2
    etc...
    I just want the field names and I don't understand why those indexes are in there. Help!? I guess I could just only echo the odd result, but that seems like a pain.
    Last edited by ober0330; Jul 15th, 2004 at 02:42 PM.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I think ForEach would be better suited to your problem. Each returns an array including the value and the keys. Whereas you can use ForEach to separate each pair.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are two ways:
    Code:
    foreach($array as $key => $value)
    Code:
    while(list($key, $value) = each($array))
    I think the first is much clearer. You also don't have to reset the array if you want to go through it twice.

    In both cases, you simply ignore $value if you don't want it. If you only want $value and not $key, you can write the first loop as
    Code:
    foreach($array as $value)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Helpful, but neither of you answered the question.

    The code is spitting out extra keys duplicates with the same value assigned to them, but with an integer index. That's the problem I want to get rid of. I've done a work-around using a simple boolean variable to only print out the odd ones, but I want to understand why it is printing out those duplicates in the first place.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I assume you are using the mysql_fetch_array () function. This will return an array in the form:
    Code:
    Array (
    [0] => 'value1'
    ['field1'] => 'value1'
    [1] => 'value1'
    ['field2'] => 'value2'
    [2] => 'value1'
    ['field3'] => 'value3'
    )
    Istead use the mysql_fetch_assoc () function which will just return the indexes with the field names.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Actually, it's mssql (MS SQL 2K), but the same idea applies! You just saved me about 8 lines of code Thanks!
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width