SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question building a string from DB results?

    Hi,

    Excuse me if I use the wrong terminology..

    I need to build a string of information that's pulled from a DB in a while loop. I need one piece of data from each row returned, to all be joined together. Is there some sort of append function or something?

    So for example, I retrieve the fields; 'colours' and 'jumper type' from the DB and then I want to insert all of the colours into another DB (all the colours as one entry)

    thx
    coiL
    "cradled in the learning curve"

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Could you explain it a little better, I don't get it How do you want the colours seperated and stuff like that? Like would the resulting query be something like this
    Code:
    insert into newtable
    (
    	jumper_type
    	, colour
    ) values (
    	'jumper1, jumper2, jumper3'
    	, 'blue, red, green'
    )

  3. #3
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I only need the one field to put into the new table, so something like this:


    PHP Code:
    // the while loop here retrieving all of the info from the 1st table
    // results would be something like

    red wool jumper
    blue wool jumper
    purple cotton jumper
    purple wool jumper
    etc 
    ....

    // want to build a string that simply contains all of the colours
    $colours "red, blue,purple, purple, etc.."
    coiL
    "cradled in the learning curve"

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    while ( $row mysql_fetch_array($rs) ) {
        
    $colours .= $row['colour'] . ', ';
    }
    $colours substr($colours0strlen($colours) - 2); 
    Should do the trick

  5. #5
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, I just wanted to check that I understand exactly what all that does..

    PHP Code:
    while ( $row mysql_fetch_array($rs) ) {

    // Puts all the data from $row['colour'] into the variable $colours like red, purple, blue, etc

        
    $colours .= $row['colour'] . ', ';
    }

    // returns each colour minus the , and the space?
    $colours substr($colours0strlen($colours) - 2); 
    by the way what is the ' .= ' called?

    coiL
    "cradled in the learning curve"

  6. #6
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    .= is called (forgive the spelling) concatenation, joining of strings. I'll comment the code, you got it fairly close though
    PHP Code:
    while ( $row mysql_fetch_array($rs) ) { // <-- duh
        // adds the value of colour in the db plus a comma and a space
        
    $colours .= $row['colour'] . ', ';
    }
    // strips the comma and space off the very end because the loop adds it but it's unnecessary after the final vaue
    $colours substr($colours0strlen($colours) - 2); 
    Basically, the last line removes the end comman and space, without it, $colours would contain this
    Code:
    blue, red, purple, purple,_ <!-- note space (underscore) -->
    ..whereas with the last line, it'll contain this..
    Code:
    blue, red, purple, purple


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
  •