Web Analytics Made Easy -
StatCounter Splitting field data into multiple strings - CodingForum

Announcement

Collapse
No announcement yet.

Splitting field data into multiple strings

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Splitting field data into multiple strings

    Hi, im practically new to PHP, but I have managed to code the majority of my site by myself... however, Im trying to create a music database within the site, and I cant find how to extract data from a single MySQL field, which has been separated by a comma.

    For Example:

    In the 'BandMembers' field, I Have the following data 'YoungFools, The DarkHorse'

    I would like to show them on the page as automatically separated into links.

    so on the page would be shown as:

    Members: YoungFools, The Darkhorse (the names being Links)

    Can anyone help?

    **EDIT**

    ok. I found this line of code on these forums

    PHP Code:
    // get the name from the old db
    $new_name explode(' '$old_name); // be sure that there is only one white space in the full name!
    // insert $new_name[0] in the first name field and $new_name[1] in the last name field 
    However, there are an unlimited amount of band members... how would I print it, while adding hyperlink codes around them?
    Last edited by YoungFools; Oct 4, 2006, 05:04 PM.

  • #2
    The result of the explode() function is an array. To loop through an array, you can use foreach(), like this:

    PHP Code:
    $memberArray explode(' '$bandMembers);
    foreach (
    $memberArray as $memberName) {
        print 
    "<a href=\"urltolink.html\">$memberName</a><br />\n";

    You would of course want to format the html code around the links to create the page layout the way you like.

    Comment


    • #3
      Ah! ok thanks, will give that a go now!

      Comment


      • #4
        Yep! it works tweaked it a little though... but one question... how would I add a comma in between the names(results) without having to add a comma at the end of the last name?

        Comment


        • #5
          The explode() function doesn't need to see the separator string at the end. In other words, explode(", ", "a, b, c, d, e") will return an array with elements 'a', 'b', 'c', 'd', and 'e'.

          Comment


          • #6
            Na, what I meant was, instead of the print showing:

            YoungFools The DarkHorse (after being EXPLODEd)

            it shows (link)YoungFools(/link), (link)The DarkHorse(/link)

            (I know (link) isnt a proper code, its just to show where the links are.

            Comment


            • #7
              Ah, sorry I misunderstood. Here's a fairly recent post that addresses the issue.

              Comment


              • #8
                Sorry, was my fault.. but many thanks for your help!

                Comment


                • #9
                  PHP Code:
                  <?php
                  $memberArray 
                  explode(' '$bandMembers);
                  $output '';
                  foreach (
                  $memberArray as $memberName)
                      
                  $output .= '<a href="urltolink.html">'.$memberName.'</a>, ';

                  # Chop of last ', '
                  $output substr($output,0,-2);

                  echo 
                  $output;
                  ?>
                  Active PHP/MySQL application developer available for immediate work.
                  syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

                  Comment


                  • #10
                    PHP Code:
                    <?php
                    $memberArray 
                    explode(' '$bandMembers);

                    foreach (
                    $memberArray as $key => $memberName)
                    {
                        
                    $memberArray[$key] = '<a href="urltolink.html">'.$memberName.'</a>';
                    }
                    $output implode(', '$memberArray);

                    echo 
                    $output;
                    ?>
                    Imploding is nice as you don't have to worry about the separator.
                    I'm not sure if this was any help, but I hope it didn't make you stupider.

                    Experience is something you get just after you really need it.
                    PHP Installation Guide Feedback welcome.

                    Comment

                    Working...
                    X