Announcement

Collapse
No announcement yet.

How would I return values from a comma delimited string

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

  • BeatificFaith
    replied
    I figured it out, thank you all for your help!

    Leave a comment:


  • BeatificFaith
    replied
    Hi! Okay, I've got it to explode and managed to get the correct ID numbers to display, but not the corresponding titles. So now it displays as:
    jj
    2
    Position #2

    jj
    5
    Position #2
    Here is the updated code:
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {    
    $position explode(",",$row['positions'])  ;

        echo 
    "<hr>";
        echo 
    "<b>" .$row['first_name']."</b> "$position[0]." <br/>";
        echo 
    $row['positiontitle']."<br/>";
            echo 
    "<hr>";
        echo 
    "<b>" .$row['first_name']."</b> "$position[1]." <br/>";
        echo 
    $row['positiontitle']."<br/>";
        }
    //end while 
    Any Suggestions?

    Leave a comment:


  • BeatificFaith
    replied
    I figured as much. I just can't seem how to exactly do that. Would I add it in the query?

    Leave a comment:


  • Marco van Herwaarden
    replied
    You can use explode() to create an array out of the comma seperated list again.

    Leave a comment:


  • How would I return values from a comma delimited string

    I don't have a tip, but a question rather.

    I'm developing a script that will allow a user to input information into a form, select positions, and those positions will go into a comma delimited string. It would work similar to the way Vbulletin does this:
    Usergroups are ID numbers. When you look at a members profile however, it does not show the ID number, but the usergroup name. In the database, the usergroup IDs are entered as a comma delimited string.
    The only way I could find to insert them into the database was using implode. It goes into the database just great, comma separated (although I would like each to go into it's own field, but it's okay for now).

    The problem comes when I try to display it. Since it's comma delimited it display only the first item in the array. But there are multiple rows that should be displayed. I'm not sure how to explode it so that it will display them all. I've tried a loop but that just looped the first item in the field (the first part of the array).


    Here is the code of the form:
    PHP Code:
              <?php 
              
    $query 
    sprintf("SELECT position_id, positiontitle,positiondes,qualifications,exp_time, activated FROM positions");
    $result mysql_query($query);
    if (!
    $result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $query;
       die(
    $message);
    }
    while (
    $row mysql_fetch_assoc($result)) {    
        if  (
    $row['activated']==1){
        echo  
    "<table width=\"100%\" align=\"center\" cellpadding=\"5\" cellspacing=\"5\">";
               echo 
    "<tr style=\"background-color:#f8f8f8; border: thin solid #000066; \"><td rowspan=\"3\"> <input type=\"checkbox\" value=\"".$row['position_id']."\" name=\"applying_for[]\" />";
               echo 
    "</td>";
               echo 
    "<td rowspan=\"3\"><b>".$row['positiontitle']."</b></td>";
               echo 
    "<td><b>Description:</b>".$row['positiondes']."<br /></td></tr>";
               echo 
    "<tr><td><b>Qualifications:</b>".$row['qualifications']."<br /></td></tr>";
               echo 
    "<tr><td><b>Expected time:</b>".$row['exp_time']."<br /><br /></td></tr>";
               echo 
    "</table>";
       }
    }
             
              
    ?>
    This is the display page:
    PHP Code:
         <?php 
    $query 
    sprintf("SELECT a.position_id, a.positiontitle, u.first_name FROM positions a JOIN volunteers u ON position_id = positions ");
              
    $result mysql_query($query);

    if (!
    $result) {
       
    $message  'Invalid query: ' mysql_error() . "\n";
       
    $message .= 'Whole query: ' $query;
       die(
    $message);
    }
    while (
    $row mysql_fetch_assoc($result)) {    
        echo 
    $row['first_name']."<br/>";
        echo 
    $row['position_id']."<br/>";
        echo 
    $row['positiontitle']."<br/>";
    }

    ?>
    This is what puts it into the database:
    PHP Code:
           <?php

    $positions 
    implode(','$_POST['applying_for']);

         
    $sql mysql_query("INSERT INTO volunteers (positions) VALUES ('$positions')") or die (mysql_error());
            

                
    ?>
    *It's more than that in the last php code, I just took out everything that didn't relate to the question which was nothing but more variables.

    After the query I receive:
    jj
    2
    Position #2
    Depending on what the number two position title is. That is only good if one position is selected though. But if a user select 2,5 it still displays only the above (the first id in the comma delimited string). Instead I would like it to display the first name, all id's in the string on seperate rows along with their title, and the title position. So it should display as:
    jj
    2
    Position #2

    jj
    5
    Position #5
    What further step should I take?
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Working...
X