How to display array data from database?

Discussion in 'PHP' started by kcm4web, Apr 13, 2010.

  1. #1
    hi there,

    i jus need ur a help . thing is that i have a category page where admin will make categories. there are two fields category_id and category_name. there is another form member directory where admin will make directory list. there are some clients whose multiple business so i am sending the category in array to the database they are saving like( 2,7,9 etc). but the thing is that i want to display in the user side where client A has this type of category business.
    e.g
    name- client name
    category of business - a,b,c
    (note that: a,b,c is the category id of 2,7,9)

    how will i display it from the database. i have tried a lot but cud nt get success. pls help me
     
    kcm4web, Apr 13, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    What you do is correct. All you need to do to display the category names is something like this.


    
    
    $cats = explode(',',$data['catOfBusiness']);
    
    foreach ($cats as $cat) {
    
       $query = mysql_query('SELECT `name` FROM `categorieOfBusiness` WHERE `id`='.$cat.' LIMIT 1';
       $data = mysql_fetch_assoc($query);
       echo $data['name'];
    
    }
    
    
    PHP:

    hope that helps;
     
    stephan2307, Apr 13, 2010 IP
  3. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Don't send category as array.
    Use normalization. Maybe this better
    So, user can have one or more category.
     
    guardian999, Apr 14, 2010 IP