SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fieldname and array

    I know my code is wrong below, but I cannot seem to be able to get it right, just having one of those days and its doing my head in.
    PHP Code:
    $iFields=mssql_num_fields($tmpData);
        while(
    $r=mssql_fetch_array($tmpData)) {
            for(
    $i=0;$i<$iFields;$i++) {
                
    $sFields=mssql_field_name($tmpData,$i);
                
    $arr[$sFields]=$r[$sFields];
            }
        } 
    I have a small function that I want to be able to pass any select SQL statement across and the number of fields in these statements are of varying length. I need my function to be able to loop through the number of records and also the fields so it builds my array with the key as the fieldname so an array would look something like

    Example
    [staffID] => 1
    [staffName] => Fred
    [staffID] => 2
    [staffName] => Joe

    And so on, not sure if my array would look something like this, but I hope so.

    (there could be more fields in the array some could have just one and others could have 10+)

    the main reason for this is that I'm using the function within a webservice and need to pass the results back to javascript (which I can do no problem) I just cannot get the return array correct

    Help!!!!!!!!!!!!!!!!!!!

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $sFields=mssql_field_name($r,$i); ??
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Failing an array output, I can live with something like

    fieldname,value|fieldname,value

    Example
    staffID,1|staffName,Fred~staffID,2|staffname,Joe

    And so on

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Supplementary thought. $r is already an array. Why not just copy it?

    PHP Code:
    while($r=mssql_fetch_array($tmpData)) {
        
    $arr[] = $r;
    }

    echo 
    "<pre>";
    print_r ($arr);
    echo 
    "</pre>"
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because I need the fieldnames, not up to scratch with these array yet fairly new to php

  6. #6
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Supplementary thought. $r is already an array. Why not just copy it?
    Sorry that code of yours worked, but is there any way to break that down to something like

    staffID,1|staffName,Fred~staffID,2|staffname,Joe

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try my example. The print_r() displays the array contents.

    You should already have the fieldnames because mssql_fetch_array() returns the fieldnames and ordinals. Better still, to avoid any potential confusion, you could use mssql_fecth_assoc() which will only return an array with associated fieldnames and values. (mysql_fetch_array() returns both names and ordinals).
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trekmp
    Sorry that code of yours worked, but is there any way to break that down to something like

    staffID,1|staffName,Fred~staffID,2|staffname,Joe
    Why do you want to do that? Does the data to/from the webservice need to be string data? So your question is really "How can I take this array of data and convert it to a string of the form field1name,value|field2name,value|fieldXname,value~repeat for next row?"
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you want to do that?
    Yes I'd hoped that the array would get passed across in a useable format but it doesn't so it looks like I'll have to break this down into a string, which I should be able to do, but any suggestions are welcome.

    Thanks for your help, its been an education, these arrays are a lot more flexiable than the ones i've used in asp (never going back to asp if I can help it)

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    UNTESTED!!
    PHP Code:
    $str "";
    while ( 
    $r mssql_fetch_assoc ($results) ) {
        
    $arr $r;
        foreach ( 
    $arr as $k => $v ) {
            
    $str .= $k .",".$v."|";
        }
        
    $str substr ($str0strlen ($str 1)); // remove final |
        
    $str .= "~"// add the ~ for the new row
    }
    $str substr ($str0strlen ($str 1)); // remove final ~

    echo $str
    Ian Anderson
    www.siteguru.co.uk

  11. #11
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help so far, this is the array it returns

    Array ( [0] => Array ( [StaffEmail] => me@here.com [StaffName] => Fred ) [1] => Array ( [StaffEmail] => you@here.com [StaffName] => Joe ) [2] => Array ( [StaffEmail] => them@here.com [StaffName] => Jane ) )

    This is the string that gets returned

    0,Array|0,~0,Array|1,Array|0,~0,Array|1,Array|2,Array|0,~0,

  12. #12
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks siteguru managed to get it sorted, you've helped alot

    PHP Code:
    while($r=mssql_fetch_assoc($tmpData)) {
        
    $arr[] = $r
        foreach (
    $arr[$i] as $key => $value ) { 
            
    $str.= $value.","
        } 
        
    $str=rtrim($str,","); // remove final ,
        
    $str.= "|"// add the | for the new row
        
    $i++;

    $str=rtrim($str,"|"); // remove final | 

  13. #13
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    rtrim() ... forgot about that function! Glad you got it sorted.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

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