Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: explode problem

  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    explode problem

    Hi guys

    i am trying to explode a comma seperated list of IDs, however sometimes there may only be one ID.

    once i have the ID(s) i need to be able to run a query and select the NAME related to the ID and echo them.

    i have the following buyt doesn't seem to work.

    any help would be great. thanks

    PHP Code:

    if(!empty($row['bas']))
                        {
                            echo 
    '<TR>';
                                echo 
    '<TD class="subheading">BAS</TD>';
                                echo 
    '<TD class="maintext">'
                                
                                
    $bas = ($row["bas"]); 
                                
    $bas_array explode(",",$bas); 
                                foreach (
    $bas_array as $key
                                { 
                                
    $sql3 "SELECT bas,bas_name FROM tools.tools1 WHERE bas='$key'";
                                
    $rs3 mysql_query ($sql3) or die (mysql_error());
                                if (
    mysql_num_rows($rs3)>0)     
                                {
                                while(
    $row=mysql_fetch_array($rs3))    
                                {
                                echo 
    $row['bas_name'];
                                }
                                }

                                }  
                                
                                
    '</TD>';
                            echo 
    '</TR>';
                        }
                        else
                        {
                            
    '';
                        } 

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Can you be more specific about the problem you are having....

    right off the bat I'm noticing that you have more than one array with the name $row....each $row array is probably referencing a different MysQL result source, so you will have to give them different names, i.e.: $row1 and $row2
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    If you're selecting multiple names by ID, it is better to do it like this (assuming $bas is a comma separated list of IDs):

    PHP Code:
    $sql "SELECT name FROM table WHERE id IN (" $bas ")";

    $result mysql_query($sql);

    while (
    $row mysql_fetch_assoc($result)) {
        echo 
    $row['name'];



  •  

    Posting Permissions

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